ggplot geom_point() with colors based on specific, discrete values

前端 未结 1 1000
情话喂你
情话喂你 2020-12-05 07:23

I am trying to plot data points with three different colors for three value ranges. For example:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point(a         


        
相关标签:
1条回答
  • 2020-12-05 07:40

    You need to cut your values into intervals:

    library(ggplot2)
    ggplot(mtcars, aes(wt, mpg)) + 
      geom_point(aes(colour = cut(qsec, c(-Inf, 17, 19, Inf))),
                 size = 5) +
      scale_color_manual(name = "qsec",
                         values = c("(-Inf,17]" = "black",
                                      "(17,19]" = "yellow",
                                      "(19, Inf]" = "red"),
                         labels = c("<= 17", "17 < qsec <= 19", "> 19"))
    

    resulting plot

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