Highlight a line in ggplot with multiple lines

前端 未结 3 596
长情又很酷
长情又很酷 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 09:17

    One approach to achieve this is to make use of named vectors for color, size, .. which you can then use inside scale_xxxx_manual to set the values as you like.

    library(tidyverse)    
    # Data in wide format
    df_wide <- data.frame(
      Horizons = seq(1,10,1),
      Country1 = c(2.5, 2.3, 2.2, 2.2, 2.1, 2.0, 1.7, 1.8, 1.7, 1.6),
      Country2 = c(3.5, 3.3, 3.2, 3.2, 3.1, 3.0, 3.7, 3.8, 3.7, 3.6),
      Country3 = c(1.5, 1.3, 1.2, 1.2, 1.1, 1.0, 0.7, 0.8, 0.7, 0.6)
    )
    
    # Convert to long format
    df_long <- df_wide %>%
      gather(key = "variable", value = "value", -Horizons)
    
    colors <- c(Country1 = "red", Country2 = "grey50", Country3 = "grey50")
    sizes <- c(Country1 = 2, Country2 = .5, Country3 = .5)
    # Plot the lines
    plotstov <- ggplot(df_long, aes(x = Horizons, y = value)) + 
      geom_line(aes(colour = variable, size = variable, group = variable)) +
      scale_color_manual(values = colors) +
      scale_size_manual(values = sizes) +
      theme_bw()
    plotstov
    

提交回复
热议问题