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

How can I show legend of multiple layers (geom_point and geom_bar)?

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

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

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()

这是我得到的图表:

如您所见,图例中未显示 point/line 组。然后我尝试删除参数 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)

我从中得到的图表是:

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

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

有谁知道如何调整我的代码以将 point/line 组显示为其他两组旁边或下方的单个项目符号点? 最好还可以选择标签名称 ('Total Cost')。

非常感谢您!

经验法则: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))

改变图例的顺序:

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