Custom grouping for legend in ggplot
In R, I'm trying to create a custom line chart where all the rows are shown in the chart, but the legend for the chart is configured to only show two things.
My code now:
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 the chart to display all 4 lines (1,2,3 and avg works), but for the legend I want it to read "Avg" and "Individual Runs" where "Avg" is any color I have I choose and each of the "Custom Runs" is gray or neutral. This way, when I have a lot of launches, I can see the data visually, but the legend doesn't come out of the screen. How to do it?
source to share
We can use a function subset
and specify colors using two different calls 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'))
source to share
You can make a grouping variable to match with color
that only has two levels. It's pretty simple using a function fct_other
from the forcats packages.
This holds the "Avg" group, but brings all the other girders together into one level, which can be set with other_level
.
library(forcats)
df$variable2 = fct_other(df$variable, keep = "Avg", other_level = "Individual Runs")
Use the new variable for color
while 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") )
source to share