Reputation: 2018
I'm following this links example, as I have a similar situation where I'm trying to graph two data frames in the same plot. I'm only interested in changing the linetypes for one of the data frames, which works in the graph, but doesn't display correctly in the legend.
Example dataset:
set.seed(456)
n <- 20
dfr <- data.frame(
id=rep(1:n, 2),
group=rep(c("1","2"), each=n), value=c(rnorm(n), rnorm(n, sd=1.1))
)
df_95ci <- data.frame(y_values=c(-1,1)*qnorm(0.95))
df_99ci <- data.frame(y_values=c(-1,1)*qnorm(0.99))
require(ggplot2)
Code:
ggplot(data=dfr, mapping=aes(x=id, y=value)) +
geom_line(mapping=aes(colour=group)) +
geom_hline(data= df_95ci, mapping=aes(yintercept=y_values, size= "95% CI"), colour = "orange", linetype="dotdash") +
geom_hline(data= df_99ci, mapping=aes(yintercept=y_values, size= "99% CI"), colour = "darkred", linetype="dotted") +
scale_color_hue("Group") +
scale_size_manual(
"CI horizontal line", values=rep(1,4),
guide=guide_legend(override.aes = list(colour=c("orange", "darkred")))
) +
scale_linetype_identity(guide="legend")
As you can see I have two lines with different linetypes, but they're identical in the legend.
Upvotes: 0
Views: 1201
Reputation: 815
Do you want this?
ggplot(data=dfr, mapping=aes(x=id, y=value)) +
geom_line(mapping=aes(colour=group)) +
geom_hline(data= df_95ci, mapping=aes(yintercept=y_values, linetype= "95% CI"),
colour = "orange", size = 1) +
geom_hline(data= df_99ci, mapping=aes(yintercept=y_values, linetype= "99% CI"),
colour = "darkred", size = 1) +
scale_linetype_manual(
"CI horizontal line", values=c("95% CI" = 4, "99% CI" = 3),
guide=guide_legend(override.aes = list(colour=c("orange", "darkred")))
)
Upvotes: 2