Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I am having a data frame of the following 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)

)

From that, 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 want to know that can facet_grid achieve this?

1 Answer

0 votes
by (107k points)

For that, you can work with facet_wrap and facet_grid both as they perform faceting on the id.

ggplot(df) + 

  geom_line(aes(x = time, y = value, color = as.factor(id))) + 

  gghighlight::gghighlight() +

  facet_wrap(~id)

If you are a beginner ad want to know more about R then do check out the R programming tutorial

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...