ggplot:结合图例中的大小和颜色 [英] ggplot: combining size and color in legend

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

问题描述

我只是最近才开始学习R.现在我想要做的是为同一个情节集成两个传说。换句话说,我希望默认大小的图例根据其大小改变颜色。



我一直在Google上搜索几个似乎都不起作用的解决方案,但再次,我是R新手,所以也许我只是做错了什么。



我的代码:



<$ (ces,aes(x = testscr,y = avginc),color =green)+
geom_point(aes(size = enrltot,color = enrltot))+
geom_smooth(color =blue)+
labs(x =测验分数,y =平均收入,title =加州测验分数数据,color =学生人数nPer District)+
主题(
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color =gray,size = 0.4),
panel.background = element_rect(fill =beige),
axis.line = element_line(size = 1.2,color =black),
plot.title = element_text(size = rel( 2)))+
scale_color_continuous(limits = c(0,30000),breaks = seq(0,30000,by = 2500))+
guides(color = guide _legend(),size = guide_legend())

显然,我不允许发布图片,或者我会显示目前为止的样子。

解决方案

ggplot2 确实可以将大小和颜色的图例合并为一个,但是,只有它们兼容时,它们才能起作用:它们需要具有完全相同的分隔符,否则它们不能组合。



让我举个例子:假设你有0到10之间的值,你想要在大小和颜色上进行映射。你告诉 ggplo2 使用5点以下的小点和更大的值使用小点。如预期的那样,它将绘制一个小而大的图例。现在,您还需要添加颜色,并且您要求3以下的点数为绿色,而上面的点数为蓝色。 ggplot2 也会为此画出一个图例,但是将这两个图例组合起来是不可能的。小点必须是绿色和蓝色。问题可以通过对颜色和大小使用相同的中断来解决。



在您的示例中,您手动更改色标的中断,但不是大小的中断规模。这导致不兼容的传说,无法合并。



我无法用你的日期证明这一点,因为我没有它。所以我会用 mtcars 创建一个例子。具有不相容图例的变体构造如下:

  p < -  ggplot(mtcars,aes(x = mpg,y = (限制= c(2,5),休息= seq(2,5,by = 0.5))+ 
geom_point(aes(size = gear,color = gear))+
scale_color_continuous +
guides(color = guide_legend(),size = guide_legend())

如下图所示:





如果我现在添加尺寸相同,

  p + scale_size_continuous(限制= c(2,5),休息= seq(2,5 ,by = 0.5))

我只看到一个图例:





对于你的代码,这个m你应该添加以下内容到你的情节:

pre $ + scale_size_continuous(limits = c(0,30000),breaks = seq (0,30000,by = 2500))

有点评论:您打算如何使用在 ggplot 的调用中< color =green?我没有看到这个效果,因为你在后面使用的两个geoms中再次设置颜色。也许是一个旧情节的遗物?


I've only very recently started learning R. Now what I'm trying to do is to integrate two legends for the same plot. In other words, I want the default size legend to change color depending on it's size.

I have been Googling several solutions that apparently all don't seem to work, but again, I'm new to R so maybe I'm just doing something wrong.

My code:

ggplot(Caschool, aes(x=testscr, y=avginc), colour="green") +
  geom_point(aes(size=enrltot, color=enrltot)) +
  geom_smooth(colour="blue") +
  labs(x="Test Score", y="Average Income", title="California Test Score Data", color="Number of Students\nPer District") +
  theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major=element_line(colour="grey", size=0.4), 
    panel.background=element_rect(fill="beige"), 
    axis.line=element_line(size = 1.2, colour = "black"),
    plot.title = element_text(size = rel(2))) +
  scale_color_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500)) +
  guides(color= guide_legend(), size=guide_legend())

Apparently, I'm not allowed to post pictures, or I would have shown what this looks like so far.

解决方案

ggplot2 can indeed combine size and colour legends into one, however, this only works, if they are compatible: they need to have exactly the same breaks, otherwise they can not be combined.

Let me make an example: Assume, you have values between 0 and 10 that you want to map on size and colour. You tell ggplo2 to use small points for values below 5 and large points for larger value. It will then plot a legend with a small and a large point, as expected. Now, you also want to add colour and you require points below 3 to be green and points above to be blue. ggplot2 will also draw a legend for this, but it is impossible to combine the two legends. The small point would have to be both, green and blue. The problem can be solved by using the same breaks for colour and size.

In your example, you manually change the breaks of the colour scale, but not those of the size scale. This results in incompatible legends that can not be combined.

I can not demonstrate this using your date, because I don't have it. So I will create an example with mtcars. The variant with incompatible legends is constructed as follows:

p <- ggplot(mtcars, aes(x=mpg, y=drat)) +
   geom_point(aes(size=gear, color=gear)) +
   scale_color_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5)) +
   guides(color= guide_legend(), size=guide_legend())

which gives the following plot:

If I now add the same breaks for size,

p + scale_size_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5))

I get a plot with only one legend:

For your code, this means that you should add the following to your plot:

+ scale_size_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500))

A little side remark: What do you intend by using colour = "green" in your call to ggplot? I don't see that this has any effect at all, because you set the colour again in both geoms that you use later. Maybe a relic from an older variant of the plot?

这篇关于ggplot:结合图例中的大小和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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