我有两个数据集,我想合并成一个图,一个是条形图(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

如您所见,图例中不显示点/线编组.然后我try 移除参数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组,但不幸的是,点/线组现在——显然——用一个正方形标记,而不是其点/线符号(在图例中).

Does anyone have any idea how I can adjust my code to show the point/line group as a single bullet point next to or below the other two groups?最好还可以 Select 标签名称("总成本").

提前非常感谢!

推荐答案

经验法则:aes()以内的一切都会产生一个传奇.因此,将aes()分中的sizeANDcolor分为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

Changing the order of legend:

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

enter image description here

R相关问答推荐

删除具有相同标题的tabPanel(shinly)

根据元素和前一个值之间的差值过滤矩阵的元素

如何使用tryCatch执行语句并忽略警告?

使用rest从header(h2,h3,table)提取分层信息

在使用tidyModels和XGBoost的二进制分类机器学习任务中,所有模型都失败

使用rvest从多个页面抓取时避免404错误

无法定义沿边轨迹的 colored颜色 渐变(与值无关)

基于Key->Value数据帧的基因子集相关性提取

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

R-使用stri_trans_General()将其音译为德语字母

在不重复主题的情况下重新排列组

在GT()中的列之间添加空格

在R中使用ggraph包排列和着色圆

将一个二次函数叠加到一个被封装为facet的ggplot2对象的顶部

将一个字符串拆分成R中的两行或多行,同时复制其他列

组合预定义美学(Ggplot2)

GGPLATE:如何从图例中删除特定的因素级别,同时仍然可以设置其 colored颜色 ?

r mgcv包:如何在使用张量积时设置 node 的位置

`as.trans()`中的ggplot2 ggallin错误:!`Trans`必须是字符向量或转换器对象

R图不同 colored颜色 的回归线和散点图