Reputation: 227
In R, I am trying to create a custom line chart where all the lines are shown on the chart, but the legend for chart is customized to only show two things.
Right now my code:
x = data.frame(runif(20),runif(20)*2,runif(20)*3)
names(x) = c("Run 1", "Run 2", "Run 3")
x$Avg = apply(x, 1, mean)
x$Step = row.names(x)
df = melt(x, id=c("Step"))
ggplot(df, aes(x=Step, y=value, group=variable, color=variable)) +
geom_line()
Result:
I would like to have the chart show all 4 lines (runs 1,2,3 and avg), but for the legend, I want it to read "Avg" and "Individual Runs", where "Avg" is any color I choose, and each of the "Individual Runs" is in gray or some neutral color. This way when I have lots of runs, I see visually see the data but the legend doesnt go off the screen. How do I accomplish this?
Upvotes: 3
Views: 2952
Reputation: 36114
You could make a grouping variable to map to color
that has only two levels. This is pretty straightforward using the fct_other
function from package forcats.
This keeps the "Avg" group but lump all other runs together into a single level, which can be set via other_level
.
library(forcats)
df$variable2 = fct_other(df$variable, keep = "Avg", other_level = "Individual Runs")
Use the new variable for color
, keeping the original variable variable
for group
. Set colors with scale_color_manual
.
ggplot(df, aes(x = Step, y = value, group = variable, color = variable2)) +
geom_line() +
scale_color_manual(values = c("red", "grey") )
Upvotes: 1
Reputation: 10781
We can use the subset
function, and specify colors using two different calls to geom_line
:
ggplot()+
geom_line(data = subset(df, variable != 'Avg'),
aes(x = Step, y = value, group = variable, colour = 'Individual Runs'))+
geom_line(data = subset(df, variable == 'Avg'),
aes(x = Step, y = value, colour = 'Avg', group = variable))+
scale_colour_manual(values = c('Avg' = 'red', 'Individual Runs' = 'grey'))
Upvotes: 6