Highlight a line in ggplot with multiple lines

前端 未结 3 625
长情又很酷
长情又很酷 2020-12-11 08:23

I want to change the size, linetype, color etc. for one line in ggplot. Here is a minimal reproducible example:

library         


        
3条回答
  •  孤街浪徒
    2020-12-11 08:59

    Following this duplicate here is another answer to this question.
    It uses a simple trick, to tell the highlighted variable by comparing it with the target value. This dichotomyzes it, becoming a logical FALSE/TRUE value.

    variable == "Country1"
    

    The plot legend now needs more care to be taken, in order to have its title and text right.

    g <- ggplot(df_long, aes(x = Horizons, y = value)) + 
      geom_line(aes(colour = variable == "Country1", size = variable == "Country1", group = variable)) +
      scale_color_manual(name = "variable", labels = c("Other", "Country1"), values = c("grey50", "red")) +
      scale_size_manual(name = "variable", labels = c("Other", "Country1"), values = c(0.5, 2)) +
      theme_bw()
    
    g
    

提交回复
热议问题