r - 如何显示多层图例(geom_point 和 geom_bar)?

标签 r ggplot2 geom-bar legend-properties geom-point

我有两个数据集,我想合并成一个图,一个作为条形图(2 组),一个作为点线图(1 组)。我已经设法合并这些图并显示条形图的图例,但每当我尝试显示点/线的图例时,它都不起作用。

这是我的数据集的摘录(已翻译):

bargroup <- data.frame('Year' = as.numeric(rep(2001:2004, each = 2)),
                     'Group' = as.factor(rep(c('State', 'Country'), 4)),
                     'Share' = as.numeric(c(0.42, 0.41, 0.4, 0.4, 0.42, 0.4, 0.42, 0.38)))

plgroup <- data.frame('Year' = as.numeric(2001:2004),
                             'Group' = as.factor(rep('State', 4)),
                             'Value' = as.numeric(c(4.95, 5.31, 5.29, 4.96)))

这是我当前的代码:

ggplot() +
geom_bar(data = bargroup, aes(x = Year, y = Share, fill = Group, group = Group),
           stat = 'identity', position = position_dodge2(preserve = 'single')) +
  geom_point(data = plgroup, aes(y = Value*0.1, x = Year), size = 4, color = '#875DA3') +
  geom_line(data = plgroup, aes(y = Value*0.1, x = Year), size = 1, color = '#875DA3') +
  labs(x = 'Year') +
  scale_y_continuous(name = 'Share groups', labels = scales::percent,
                     sec.axis = sec_axis(~.*10, name = 'Cost')) +
  scale_fill_manual(labels = c('Share State', 'Share Country'), 
                    values = c('#659B7A', '#8CD7F0')) +
  scale_color_manual(labels = c('Total Cost'), 
                values = c('#875DA3')) +
  theme_minimal() +
  theme(legend.title = element_blank(),
        legend.position = 'bottom',
        plot.title = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x = element_text(size = 18),
        axis.title.y = element_text(size = 18),
        axis.text = element_text(size = 16),
        legend.text = element_text(size = 18)) +
  guides(fill = guide_legend(nrow = 2, byrow = T))
dev.off()

这是我得到的图表:

Graph 1

如您所见,点/线组未显示在图例中。然后我尝试删除参数 scale_color_manual() 并像这样调整 geom_point() 和 geom_line() 参数:

  geom_point(data = plgroup, aes(y = Value*0.1, x = Year, color = Group), 
             size = 4, color = '#875DA3', show.legend = T)
  geom_line(data = plgroup, aes(y = Value*0.1, x = Year, color = Group),
            size = 1, color = '#875DA3', show.legend = T)

我从中得到的图表是:

Graph 2

当我使用 melt() 函数组合两个数据集时,我得到了相同的图表。

我也试过只把它分成 3 个组,而不显示点/线组。这显示了所有 3 个组,但不幸的是,点/线组现在 - 显然 - 标有正方形而不是其点/线符号(在图例中)。

有人知道如何调整我的代码以将点/线组显示为其他两组旁边或下方的单个项目符号点吗?最好还可以选择标签名称(“总成本”)。

提前致谢!

最佳答案

经验法则:aes() 中的所有内容都会产生图例。因此,将 sizeaes() AND color 放入 aes() 中:

ggplot() +
  geom_bar(data = bargroup, aes(x = Year, y = Share, fill = Group, group = Group),
           stat = 'identity', position = position_dodge2(preserve = 'single')) +
  geom_point(data = plgroup, aes(y = Value*0.1, x = Year, color = '#875DA3'),size = 4) +
  geom_line(data = plgroup, aes(y = Value*0.1, x = Year, color = '#875DA3'),size = 1) +
  labs(x = 'Year') +
  scale_y_continuous(name = 'Share groups', labels = scales::percent,
                     sec.axis = sec_axis(~.*10, name = 'Cost')) +
  scale_fill_manual(labels = c('Share State', 'Share Country'), 
                    values = c('#659B7A', '#8CD7F0')) +
  scale_color_manual(labels = c('Total Cost'), 
                     values = c('#875DA3')) +
  theme_minimal() +
  theme(legend.title = element_blank(),
        legend.position = 'bottom',
        plot.title = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x = element_text(size = 18),
        axis.title.y = element_text(size = 18),
        axis.text = element_text(size = 16),
        legend.text = element_text(size = 18)) +
  guides(fill = guide_legend(nrow = 2, byrow = T))

enter image description here

改变图例的顺序:

guides(fill = guide_legend(nrow = 2, byrow = T, order=1))

enter image description here

关于r - 如何显示多层图例(geom_point 和 geom_bar)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72163827/

相关文章:

r - 使用 stat_summary 在线图上的形状

R igraph 将平行边转换为权重属性

R:foreach 不适用于导出图形,例如 png 或 ggsave

使用 R 中的直接标签库重新排列 ggplot 散点图的标签

r - ggplot2 中 x 轴的顺序标签遵循数值

r - ggplot2如何在geom_bar图中创建与分位数相对应的垂直线

r - 当你的数据有多个 "key"变量时,如何使用 spread() ?

r - 为什么 POSIXct 中的秒单位是 0-61

R ggplot2 : labels inside bars, 无堆叠 geom_bar

ggplot2 - GGPlot 组合/叠加柱形图和折线图(甘特图)