【问题标题】:How do I customize the colour scheme in a ggplot by category?如何按类别自定义 ggplot 中的配色方案?
【发布时间】:2021-09-30 18:42:11
【问题描述】:

我正在尝试在 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)

【问题讨论】:

  • 您能否提供必要的信息让我们重现您遇到的问题?
  • 如果您包含一个简单的可重现示例,其中包含可用于测试和验证可能解决方案的样本输入,则更容易为您提供帮助。这是怎么做的 - stackoverflow.com/q/5963269/12382064

标签: r ggplot2 colors categories timeserieschart


【解决方案1】:

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

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2021-05-07
    • 2023-02-20
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多