首页 > 解决方案 > 将图例添加到组合的线和条形图 ggplot

问题描述

所以我知道很多人都问过类似的问题,但其他人使用的代码似乎不适用于我的图表,因此为什么我想知道我是否做错了什么。

我有这个代码:

ggplot(dfMonth) 
+ geom_col(aes(x=Month, y=NumberMO), size=.7, colour="black", fill="white") 
+ geom_line(aes(x=Month, y=NumberME), size=1, colour="black", group=1)  
+ xlab("Month") 
+ ylab("No. of birds observed") 
+ theme_bw() 
+ geom_point(x=Month, y=NumberME) 
+ scale_colour_manual("" ,values =c("NumberME"="black"), labels=c("Expected No. of birds")) 
+ theme(legend.key=element_blank(),legend.title=element_blank(), legend.box="horizontal") 
+ theme(axis.title.x = element_text(margin = unit(c(5, 0, 0, 0), "mm")),
                  axis.title.y = element_text(margin = unit(c(0,3 , 0, 0), "mm"))) 

生成此图:

生成图

如您所见,即使我输入了代码,也没有将显示带有点的黑线表示的图例添加到我的图表中。没有出现错误,因此为什么我会迷失在哪里出错了。关于我没有包含的任何想法?

谢谢

标签: rggplot2bar-chartlegend

解决方案


In order for ggplot to know to draw a legend, you need to include one of the aesthetics for a geom within aes(). In this case, if you want a legend to be drawn for your line, you need to include within the aes() in the geom_line() call one of the aesthetics that you have identified for the line: linetype or color works. We'll use color here.

Oh... and in the absence of OP sharing their dataset, here's a made-up example:

set.seed(1234)
dfMonth <- data.frame(
  Month=month.name,
  NumberMO=sample(50:380, 12),
  NumberME=sample(50:380, 12)
)

Now the code to make the plot and ensure the legend is created.

p <- ggplot(dfMonth, aes(x=Month)) +
  geom_col(aes(y=NumberMO), size=0.7, color="black", fill="white") +
  geom_line(aes(y=NumberME, color='black'), size=1, group=1)
p

enter image description here

We have a legend, but there's some problems. You get the default title of the legend (which is the name of the aesthetic), and the default label (which is whatever text you put inside aes(color=.... Since we put "black" as the value there, it's applied as the label, and not the actual color. The actual color of the line is to default to the first level of the standard colorset used by ggplot2, which in this case is that light red color.

To set the color, name of the legend, and name of the label, we should specify the value. There's only one item in the legend, so there's no need to specify, but if you were to send a named vector to indicate the name for our single line explicitly, you end up with the somewhat strange-looking c('black'='black'). I also included a line break in the label name to make the look a bit better. Also, the months were running into each other, so I also changed the angle of the x axis labels.

Finally, you might notice the months were out of order. That's because default ggplot2 behavior is to factor a column of discrete values, which uses alphabetical ordering for the levels. To fix that, you specify the column as a factor before plotting with the correct levels.

dfMonth$Month <- factor(dfMonth$Month, levels=month.name)

p + scale_color_manual(
        name=NULL, values=c('black'='black'),
        labels='Expected No.\nof birds') +
      theme(axis.text.x=element_text(angle=30, hjust=1))

enter image description here


推荐阅读