Reputation: 181
Here is my problem. I have this data.frame :
df = data.frame("group" = c("a", "a", "b", "b", "a", "a", "b", "b"),
"jour" = c(rep(2, 4), rep(8, 4)) + rep(c(- 0.25, - 0.25, 0.25, 0.25), 2),
"value" = runif(8))
which gives :
from this I want to make a plot that look like this :
but with a legend for group a and group b (corresponding to red and blue colors).
If I do this :
ggplot(df, aes(x = jour, y = value, color = group)) + geom_line(size = 1.5) +
theme(legend.text = element_text(size = 16), legend.title = element_text(size = 16), axis.text = element_text(size = 16), axis.title = element_text(size = 16))
I get this :
So I created a new variable pasting group and jour to remove the link between different jour, but now the legend is bad :
df$group_jour = paste(df$group, df$jour)
ggplot(df, aes(x = jour, y = value, color = group_jour)) + geom_line(size = 1.5) +
theme(legend.text = element_text(size = 16), legend.title = element_text(size = 16), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
scale_color_manual(values = c("red", "red", "blue", "blue"))
How can I overwrite the legend to get only group a and group b and not group + jour ?
This is a toy example, in practice I have a 10 different group and 4 different jour.
Upvotes: 1
Views: 235
Reputation: 125133
Simply map jour
on the group
aes:
library(ggplot2)
ggplot(df, aes(x = jour, y = value, color = group, group = jour)) + geom_line(size = 1.5) +
theme(legend.text = element_text(size = 16), legend.title = element_text(size = 16), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
scale_color_manual(values = c("red", "blue"))
Upvotes: 3