2

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:

Graph

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?

New image

Community
  • 1
  • 1
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • _"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."_ You can use use any shape which takes a `fill` property, values 21 to 25 I believe – arvi1000 Aug 26 '16 at 22:34
  • @arvi1000 Thanks again for your comment. I think it will be great if you can help me with any shape other than`[21, 25]`. I am comfortable with your specific solution when the shape is "fillable" i.e. when it belongs to `[21,25]`..I'd appreciate your help... – watchtower Aug 26 '16 at 22:38

1 Answers1

9

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

enter image description here

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

arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • I have 2 follow-up questions, if you don't mind: **Q1** What is the difference between aes(linetype) vs. linetype = "x". I'd appreciate your thoughts. **Q2** I modified your post (just to check my concepts) to see whether I can do what you did by changing the shape to 5 (non-fill-able color). I was successful except that the two legends (geom_smooth()) and scatter plot have the same name. If you want I can add my code and graph above. I did `geom_point(shape=5, aes(color = class)) + ...` Then, I manually added colors `scale_color_manual(name = "Method", values = c("red",...))` – watchtower Aug 26 '16 at 17:44
  • 1) When you use `aes(linetype = x)` you are _mapping_ linetype to data element x (even if x is a literal value). `linetype = x` (i.e. outside of `(aes=...)`) is always assigning a literal value without mapping. Only mapped aesthetic are subject to scales and legends. – arvi1000 Aug 26 '16 at 18:01
  • 2) note that in my post i'm using `geom_point(aes(fill = class))` not `aes(color = class)`. We're keeping the `color` aesthetic for `geom_smooth` so that it can having separate legend entris – arvi1000 Aug 26 '16 at 18:02
  • Yes, I did notice that you are using shape = 21 (a fillable shape). Please pardon my ignorance. So, is it fair to say that we **cannot** have two set of legends--one set for the two `geom_smooth()` and one set for `geom_point()` if we use `aes(color = variable)`? Essentially, the only way would be to use a fillable shape such as `shape = 21` and then apply `aes(fill = variable)`. Am I on the right track? I'd appreciate your thoughts. I just want to make sure that I get the concept right. – watchtower Aug 26 '16 at 18:06
  • do you want me to post the modified code ? I am unsure whether my post was clear. Please let me know. – watchtower Aug 26 '16 at 20:29
  • I interpreted your original question as "how to achieve this plot?", which i answered. I'm not sure i understand the last follow up question (using a single aesthetic mapping (color) but have it apply differently to two different geoms and be represented on two different legends? that doesn't make sense to me and isn't how ggplot works). Anyway, generally SO questions should be confined to a single issue. – arvi1000 Aug 26 '16 at 21:54
  • I agree that you have answered the question. However, my question is about your code. I have posted my question on your solution in the question section above so that it's clear. I'd sincerely appreciate your thoughts.... – watchtower Aug 26 '16 at 22:11