在图例中为多个图层显示适当的颜色 [英] Displaying proper colors in legend for multiple layers

查看:141
本文介绍了在图例中为多个图层显示适当的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个绝对的初学者,从我开始使用 ggplot2 大约2-3天。到目前为止,我一直使用Excel来绘制图形。 ggplot2真的让我失望,所以我想在这里发布我的查询。



昨天晚上,我讨论了如何绘制 geom_smooth() code>与另一层,比如 geom_point()这里讨论:它se EMS我可以手动覆盖颜色。



正如我们所看到的,第三层仍然覆盖第二层的颜色(在图例中)。



  ggplot(mpg,aes(displ,hwy))+ 
geom_point (aes(color = class))+
geom_smooth(method =loess,se = FALSE,color =123,aes(linetype =loes))+
geom_smooth(method = lm,se = FALSE,color =345,aes(linetype =lm,color =green))+
scale_colour_manual(values = c(coral,chocolate,cornsilk ,papayawhip,blanchedalmond,red,black,yellow,pink))+
labs(color =Method)

第三层仍然覆盖第二层的颜色(在图例中)。我会很感激你的帮助。



我有两个问题:

问题1:我上面发布的问题?我会很感激任何想法。有没有解决这个问题?



问题2:我注意到有时候人们会用 aes(linetype =lm )和其他时候,他们只需在 geom_smooth()中使用(linetype =lm) 。我们为什么要做这个?我相信如果我们使用 aes(..)这里我没有明确的假设,所以我会避免猜测。我会很感激你的想法。






更新:我的问题是关于发布的解决方案。 b

散点图不能使用任何其他形状吗?发布的解决方案建议将形状更改为size = 21,这是我有点不舒服。



我更改了其他形状的代码(在下面的解决方案中),如下所示:

  huron < -  data.frame(year = 1875:1972,level = as.numeric(LakeHuron))
ggplot(mpg,aes(displ,hwy))+
#将geom_point类映射到'fill'
geom_point(shape = 5,aes(color = class))+
#颜色和线型geom_smooth
geom_smooth(method =loess,se = FALSE,
aes(linetype =loess,color ='loess'))+
geom_smooth(method = lm,se = FALSE,
aes(linetype =lm,color =lm))+
#通过赋予它们相同的名称来合并线型和颜色图例
scale_linetype_discrete(name =Method)+
scale_color_manual(name =Method,values = c(red,black,coral,chocolate,cornsilk,papayawhip,blanchedalmond 红,黑))

但是,运行此代码后,我们将看到lm和黄土的颜色已重置为蓝色散点图的传说不再是固体类型。我能够改变形状,但不是颜色问题和图例问题。任何想法?



解决方案

使用 fill 和geom_point的空心形状,并为geom_smooth使用 color 。 b

  huron < -  data.frame(year = 1875:1972,level = as.numeric(Lake Huuron))
ggplot(mpg,aes )
#将geom_point类映射到'fill'
geom_point(shape = 21,aes(fill = class),color = NA)+
#使用color和linetype geom_smooth
geom_smooth(method =loess,se = FALSE,
aes(linetype =loess,color ='loess'))+
geom_smooth(method =lm,se = FALSE,
aes(linetype =lm,color =lm))+
#通过赋予它们相同的名称来合并线型和颜色图例
scale_linetype_discrete(name =Method )+
scale_color_manual(name =Method,valu es = c('red','black'))

然而,我还要指出,如果您想要使用颜色信息来区分点,那么流畅线条的不同颜色会让人分心。类。我认为留下这两条流畅的线条会更好 - 线型足以区分它们


I am an absolute beginner and it's been about 2-3 days since I have started using ggplot2. So far, I have always used Excel for graphs. ggplot2 is really killing me, so I thought of posting my query here.

Last night, I discussed how we can plot geom_smooth() with another layer, say geom_point() This is discussed here: Scale for aesthetics used in the plot | ggplot2

In continuation to this, I thought of trying out multiple geom_smooth().

Here's what I did:

   ggplot(mpg, aes(displ, hwy)) +
     geom_point(aes(color = class)) +
     geom_smooth(method = "loess", se = FALSE, color = "black", aes(linetype = "loes")) +
     geom_smooth( method = "lm", se = FALSE, color = "red", aes(linetype = "lm",color = "green")) +
     labs(colour = "Method")

It's similar code to the previous one except that I have added another geom_smooth().

The output is:

I also looked at Format legend for multiple layers ggplot2 It seems I could manually override colors.

As we can see, the third layer still overrides the colors of the second layer (in the legend).

So, here's what I did:

   ggplot(mpg, aes(displ, hwy)) +
     geom_point(aes(color = class)) +
     geom_smooth(method = "loess", se = FALSE, color = "123", aes(linetype = "loes")) +
     geom_smooth( method = "lm", se = FALSE, color = "345", aes(linetype = "lm",color = "green")) +
    scale_colour_manual(values=c("coral", "chocolate", "cornsilk", "papayawhip", "blanchedalmond","red","black","yellow","pink")) +
     labs(colour = "Method") 

The third layer still overrides the colors of the second layer (in the legend). I'd appreciate your help.

I have two questions:

Question 1: Is there any fix for the questions I have posted above? I'd appreciate any thoughts. Is there any fix for this? I'd appreciate any thoughts.

Question 2: I noticed that sometimes people use aes(linetype = "lm") and other times they simply use (linetype = "lm") inside geom_smooth(). Why do we do this? I believe if we use aes(..) I don't have a clear hypothesis here, so I would avoid speculating. I'd appreciate your thoughts.


Update: My question is about the posted solution.

Can we not use any other shape for scatter plot ? The posted solution recommends changing the shape to size = 21, which is something I am a little uncomfortable.

I changed the code (in solution below) for other shape as below:

 huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
   ggplot(mpg, aes(displ, hwy)) +
     # map geom_point class to 'fill'
     geom_point(shape=5, aes(color = class)) +
     # use color and linetype for geom_smooth
     geom_smooth(method = "loess", se = FALSE,
                 aes(linetype = "loess", color = 'loess')) +
     geom_smooth(method = "lm", se = FALSE, 
                 aes(linetype = "lm", color = "lm")) +
     # merge linetype and color legends by giving them the same name
     scale_linetype_discrete(name = "Method") +   
     scale_color_manual(name = "Method", values = c("red", "black","coral", "chocolate", "cornsilk", "papayawhip", "blanchedalmond","red","black"))

However, after running this code, we will see that the color for lm and loess has got reset to blue and legend for scatter plot is no more solid-type. I was able to change the shape, but not the color issue and legend issue. Any thoughts?

解决方案

Use fill and a hollow shape for geom_point, and color for geom_smooth.

huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
ggplot(mpg, aes(displ, hwy)) +
  # map geom_point class to 'fill'
  geom_point(shape=21, aes(fill = class), color = NA) +
  # use color and linetype for geom_smooth
  geom_smooth(method = "loess", se = FALSE,
              aes(linetype = "loess", color = 'loess')) +
  geom_smooth(method = "lm", se = FALSE, 
              aes(linetype = "lm", color = "lm")) +
  # merge linetype and color legends by giving them the same name
  scale_linetype_discrete(name = "Method") +   
  scale_color_manual(name = "Method", values = c('red', 'black'))

However, I would also point out that the different colors for the smooth lines is sort of distracting, if you want color information to serve to differentiate the point classes. I think it would be better to leave both smooth lines black -- linetype is enough to distinguish them

这篇关于在图例中为多个图层显示适当的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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