R - gghighlight color for each line

354 Views Asked by At

I have a data frame with this format:

df <- data.frame(
  id = c(1,1,1,2,2,2,3,3,3,4,4,4),
  time = c(1,2,3,1,2,3,1,2,3,1,2,3),
  value = c(1,3,5,2,4,6,3,5,7,1,4,7)
)

I want to create individual plots highlighting each id and with other unhighlighted ids shaded as grey.

ggplot(df) + 
  geom_line(aes(x = time, y = value, color = as.factor(id))) + 
  gghighlight::gghighlight(id == 4)

I have looked into the gghighlight package which can do the highlighting for a single line. However, I am looking to have one plot for each id (1,2,3,4,etc...).

Is there a smart and clever way to do this quickly? Can facet_grid achieve this?

1

There are 1 best solutions below

0
On BEST ANSWER

Both facet_wrap and facet_grid work faceting on id.

ggplot(df) + 
  geom_line(aes(x = time, y = value, color = as.factor(id))) + 
  gghighlight::gghighlight() +
  facet_wrap(~id)