Hia, hopefully simple question:
I'd like to draw some lines over some points, but have a combined legend with the points shown over the lines. I get the desired legend if I draw the lines first, but I can only get the desired graph if I draw the points first. Tweaking the order of the guides seems like the way to go, but ends up showing two separate guides instead of one. Example below.
Can I force the guides to be plotted together?
Thanks,
Andrew
library(ggplot2)
n = 10
df <- data.frame(x = rep(1:n, 2),
y = c(1:n, n:1),
group = c(rep("a", n),
rep("b", n)))
# Desired legend, incorrect graph
ggplot(df, aes(x, y, linetype = group)) +
geom_smooth(method = "lm", se = F,
size = 1.5, color = "black") +
geom_point(aes(color = group), size = 5) +
guides(linetype = guide_legend(keywidth = 5))
# Desired graph, incorrect legend
ggplot(df, aes(x, y, linetype = group)) +
geom_point(aes(color = group), size = 5) +
geom_smooth(method = "lm", se = F,
size = 1.5, color = "black") +
guides(colour = guide_legend(keywidth = 5))
# Changing guide order doesn't help.
ggplot(df, aes(x, y, linetype = group)) +
geom_point(aes(color = group), size = 5) +
geom_smooth(method = "lm", se = F,
size = 1.5, color = "black") +
guides(colour = guide_legend(keywidth = 5, order = 1),
linetype = guide_legend(keywidth = 5, order = 2))