Reputation: 4298
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?
Upvotes: 2
Views: 1921
Reputation: 9582
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
Upvotes: 9