将图例添加到组合的线图和条形图ggplot中 [英] Adding a legend to a combined line and bargraph ggplot

查看:126
本文介绍了将图例添加到组合的线图和条形图ggplot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I know many people have asked similar questions but the code others have used does not seem to be working for my graph hence why I'm wondering if I have done something wrong.

我有此代码:

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

产生这张图的人:

如您所见,即使输入了代码,也没有将用于显示带点黑线含义的图例添加到图形中.没有错误出现,所以为什么我迷失了什么.关于我未包括在内的任何想法吗?

so as you can see, the legend to show what the black line with the points mean has not been added to my graph even though I have inputted the code. No error comes up so hence why I'm lost on whats wrong. Any ideas on what i've failed to include?

谢谢

推荐答案

为使 ggplot 知道绘制图例,您需要在中包括一种几何图形的美学特征aes().在这种情况下,如果要为线条绘制图例,则需要在 geom_line()中的 aes()中包括调用一种美学方法,您已经为该行标识了: linetype color 有效.我们将在这里使用颜色.

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.

哦...并且在没有OP共享其数据集的情况下,这是一个虚构的示例:

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

我们有一个传说,但是有一些问题.您将获得图例的默认标题(即美学名称)和默认标签(即您在 aes(color = ... )中放入的任何文本.>"black" 作为值,它用作标签,而不是 actual color .该行的实际颜色默认为标准颜色集的第一级由 ggplot2 使用,在这种情况下为浅红色.

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.

要设置颜色,图例名称和标签名称,我们应该指定值.图例中只有一个项目,因此无需指定,但是如果您要发送一个命名矢量来显式地指出我们单行的名称,则最终会得到看起来有点奇怪的 c('black'='black').我还在标签名称中添加了换行符,以使外观更好看.另外,这几个月彼此相依为命,所以我也改变了x轴标签的角度.

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.

最后,您可能会注意到几个月的故障.这是因为默认的 ggplot2 行为是分解一列离散值,这些值对级别使用字母顺序.要解决此问题,请在以正确的水平进行绘制之前将列指定为因子.

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

这篇关于将图例添加到组合的线图和条形图ggplot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆