为多个数据集创建ggplot2图例 [英] Create ggplot2 legend for multiple datasets

查看:62
本文介绍了为多个数据集创建ggplot2图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在带有图例的ggplot中以灰色显示背景数据.我的目的是在图例中包括灰色数据点,或使用手动标题制作第二个图例.但是,我没有做这两个中的任何一个.我的数据格式很长.

I am trying to display background data in grey in a ggplot with legend automatically. My aim is to either include the grey datapoints in the legend, or to make a second legend with a manual title. However I fail at doing any of the two. My data is in long format.

require(ggplot2)

xx<-data.frame(observation="all cats",x=1:2,y=1:2)
yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4)

g<-ggplot() + 
  geom_point(aes(x,y, colour=factor(observation)), colour="grey60", size=5, data=xx) +
  geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
  scale_color_discrete(name = "ltitle") 

g

我尝试将data.frames与 rbind.data.frame 合并,这产生了一个很好的图例,但是后来我无法将背景数据着色为灰色,并且无法将ggplot颜色保持在同时.

I tried to merge the data.frames with rbind.data.frame, which produces a nice legend, but then I am not able to colour the background data in grey and keep ggplot colours at the same time.

我还意识到这可以解决问题:

I also realized that this solves the problem:

g<-ggplot(aes(x,y, colour=factor(observation)), colour="grey60", data=xx) + 
  geom_point(size=5) +
  geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
  scale_color_discrete(name = "ltitle") 
g

但是我不能这样做,因为我正在使用一个函数,该函数之前会创建一个复杂的空图,然后在其中添加 geom_points .

however I can't do this, because I'm using a function which creates a complicated empty plot before, in which I then add the geom_points.

推荐答案

假设您的绘图中没有其他需要填充参数的几何图形,以下是解决背景数据颜色的变通方法 geom_point 层而不会影响其他 geom_point 层:

Assuming your plot doesn't have other geoms that require a fill parameter, the following is a workaround that fixes the colour of your background data geom_point layer without affecting the other geom_point layers:

g <- ggplot() + 
  geom_point(aes(x, y, 
                 fill = "label"),                              # key change 1
             shape = 21,                                       # key change 2
             color = "grey50", size = 5, 
             data = xx) +
  geom_point(aes(x, y, colour = factor(observation)), size = 5, data = yy) + 
  scale_color_discrete(name = "ltitle") +
  scale_fill_manual(name = "", values = c("label" = "grey50")) # key change 3
g

shape = 21 为您提供了一个看起来像默认圆点的形状,但是除了color参数外还接受了fill参数.然后,您可以在 scale_fill_manual()中将xx的 geom_point 图层的填充设置为灰色(这将创建填充图例),同时保留 color ="grey50" aes()之外(这不会添加到颜色图例中).

shape = 21 gives you a shape that looks like the default round dot, but accepts a fill parameter in addition to the colour parameter. You can then set xx's geom_point layer's fill to grey in scale_fill_manual() (this creates a fill legend), while leaving color = "grey50" outside aes() (this does not add to the colour legend).

yy的 geom_point 图层的色标不受此影响.

The colour scale for yy's geom_point layer is not affected by any of this.

p.s.刚意识到我使用了"grey50"而不是"grey60" ...但是其他所有内容仍然适用.:)

p.s. Just realized I used "grey50" instead of "grey60"... But everything else still applies. :)

这篇关于为多个数据集创建ggplot2图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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