简体   繁体   English

多层geom_bar图的自定义图例

[英]Custom legend for multi layer geom_bar plot

R version 3.1.1 (2014-07-10) Platform: i386-w64-mingw32/i386 (32-bit)

I am working on a barplot with ggplot2 . 我正在使用ggplot2 The aim is to have a combination of a stacked and dodged barplot for the data. 目的是使数据的堆积条形图和闪避条形图组合在一起。 My problem is to include a legend, which includes both layers or shows them separately. 我的问题是要添加图例,图例既包含两个图层,又单独显示它们。

Data: 数据:

df <- structure(list(year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("2008", "2009", "2010", 
"2011", "2012", "2013", "2014"), class = "factor"), product = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", 
"b"), class = "factor"), total = c(1663L, 1344L, 1844L, 444L, 
1336L, 897L, 655L, 3433L, 3244L, 2044L, 3344L, 1771L, 1410L, 
726L), partial = c(1663L, 1344L, 1844L, 444L, 949L, 302L, 5L, 
3433L, 3244L, 2044L, 3344L, 1476L, 1158L, 457L)), .Names = c("year", 
"product", "total", "partial"), row.names = c(NA, -14L), class = "data.frame")

The plan was, to plot two geom_bar layers to combine dodge and stacked. 计划是绘制两个geom_bar图层以合并躲避和堆叠。 The first layer is the total amount, the second layer is the partial amount. 第一层是总量,第二层是部分量。 The alpha value for the first layer is reduced to see the difference between the two layers. 降低第一层的alpha值以查看两层之间的差异。 So far it worked. 到目前为止,它仍然有效。

Example: 例:

ggplot(df, aes(x = year))+
      geom_bar(aes(y = total, fill = product), alpha= 0.3, stat = "identity", position = "dodge",  width = 0.3)+
      geom_bar(aes(y = partial, fill = product), alpha= 1, stat = "identity", position = "dodge", width = 0.3)

在此处输入图片说明

Now the legend is not sufficiant. 现在,传说还不够。 It shows the colour of fill = product and is not sensitive to the alpha value of the first layer. 它显示fill = product的颜色,并且对第一层的alpha值不敏感。

My approach was to use scale_fill_manual and manually add a new lable with a new colour, which did not worked. 我的方法是使用scale_fill_manual并手动添加带有新颜色的新标签,但这种方法无效。

My idea: 我的点子:

ggplot(df, aes(x = year))+
      geom_bar(aes(y = total, fill = product), alpha= 0.3, stat = "identity", position = "dodge",  width = 0.3)+
      geom_bar(aes(y = partial, fill = product), alpha= 1, stat = "identity", position = "dodge", width = 0.3)+
      scale_fill_manual(name = "",
                        values=c("red", "black","blue"),
                        labels=c("a","b","test"))

在此处输入图片说明

Thank you for any help on my problem! 感谢您对我的问题的任何帮助!

Try to use different fill values for total and partial data. 尝试对总数据和部分数据使用不同的填充值。

Quick and dirty solution: 快速而肮脏的解决方案:

ggplot(df, aes(x = year))+
  geom_bar(aes(y = total, fill = factor(as.numeric(product))), alpha= 0.3, stat = "identity", position = "dodge",  width = 0.3) +
  geom_bar(aes(y = partial, fill = factor(as.numeric(product) * 3)), alpha= 1, stat = "identity", position = "dodge", width = 0.3) +
  scale_fill_manual(name = "", values=c("red", "black","blue", "green"), labels=c("A","B","Partial A", "Partial B"))

Tested on 经过测试

R x64 3.2.2

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM