Search code examples
rggplot2colorsaes

Changing the first graph line with a standard color (black) in ggplot


In line with the question posted in this link change line color in ggplot

How can I make group1 with black color and the rest multicolor?


Solution

  • Perhaps using scale_color_identity would help -

    library(dplyr)
    library(ggplot2)
    
    dftt %>%
      mutate(color = sample(colors(), n_distinct(group))[group], 
             color = replace(color, group == 1, 'black')) %>%
      ggplot(aes(x=x, y=values, group=color, color=color)) + 
      geom_line() + 
      scale_color_identity(guide = "legend", labels = unique(dftt$group))
    

    data

    Using data from the linked post -

    dftt <- data.frame(values = runif(11*4,0,1),
                     col = c(rep(2,4),
                             rep(1,4),
                             rep(5,9*4)),
                     x= rep(1:4, 11*4),
                     group=rep(factor(1:11), each=4)
                     )