qwer
qwer

Reputation: 227

Custom grouping for legend in ggplot

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:

enter image description here

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

Answers (2)

aosmith
aosmith

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") )

enter image description here

Upvotes: 1

bouncyball
bouncyball

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'))

enter image description here

Upvotes: 6

Related Questions