ggplot2 : Color One Category Separately

后端 未结 1 1897
鱼传尺愫
鱼传尺愫 2021-01-20 17:12

I\'m generating a scatterplot in which one of my categories is an \"Other\" category. I would like the other categories in my factor variable to be whatever color they are,

相关标签:
1条回答
  • 2021-01-20 17:35

    based on this post you can get ggplot colors with this function:

    gg_color_hue <- function(n) {
      hues = seq(15, 375, length = n + 1)
      hcl(h = hues, l = 65, c = 100)[1:n]
    }
    

    So in your case :

    cols = gg_color_hue(length(levels(iris$Species)))
    

    Then you reprogramm the color you want to change and use it for your plot:

    cols[levels(iris$Species)=="virginica"]="gray"
    p2 <- p1 + scale_color_manual(values=cols)
    

    0 讨论(0)
提交回复
热议问题