如何按类别自定义 ggplot 中的配色方案?

时间:2021-07-23 16:41:36

标签: r ggplot2 colors categories timeserieschart

我正在尝试在 R 中的 ggplot 中按类别更改此时间序列的配色方案。我不喜欢默认配色方案,因为类别数量 (9) 对于大量数据,颜色很难区分(414 个数据点)。

g = ggplot(data=NCARsOld,aes(x=`Date Reported`,
                         y=as.numeric(`No. of Days Opened`),
                         colour=factor(NCARsOld$`Subject/ 
    Category`))) + geom_point() +  scale_fill_discrete("Multi word 
    title", breaks=c(1:length(subjects)), labels=subjects)

1 个答案:

答案 0 :(得分:0)

像这样定义一个颜色向量:

color_vector <- c('#9da337',blue,green) #etc

然后添加 scale_color_manual(values = color_vector)

g <- ggplot(data=NCARsOld,aes(x=`Date Reported`,
                         y=as.numeric(`No. of Days Opened`),
                         colour=factor(NCARsOld$`Subject/ 
    Category`))) + 
      scale_color_manual(values = color_vector) +
      geom_point() +  
      scale_fill_discrete("Multi word title", breaks=c(1:length(subjects)), labels=subjects)

这里是我学习如何使用它的地方: https://www.r-bloggers.com/2019/05/a-detailed-guide-to-ggplot-colors/

相关问题