【问题标题】:Changing the first graph line with a standard color (black) in ggplot在ggplot中用标准颜色(黑色)更改第一条图形线
【发布时间】:2021-10-05 22:04:55
【问题描述】:

与此链接change line color in ggplot中发布的问题一致

我怎样才能使 group1 的颜色为黑色,其余为多色?

【问题讨论】:

    标签: r ggplot2 colors aes


    【解决方案1】:

    也许使用scale_color_identity 会有所帮助 -

    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))
    

    数据

    使用链接帖子中的数据 -

    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)
                     )
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2011-07-07
      • 2021-02-07
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2020-08-01
      • 2018-08-25
      • 2021-03-03
      相关资源
      最近更新 更多