Color code points based on percentile in ggplot

后端 未结 3 987
醉酒成梦
醉酒成梦 2021-01-25 19:11

I have some very large files that contain a genomic position (position) and a corresponding population genetic statistic (value). I have successfully plotted these values and wo

3条回答
  •  执笔经年
    2021-01-25 19:24

    This is how I would approach it - basically creating a factor defining which group each observation is in, then mapping colour to that factor.

    First, some data to work with!

    dat <- data.frame(key = c("a1-a3", "a1-a2"), position = 1:100, value = rlnorm(200, 0, 1))
    #Get quantiles
    quants <- quantile(dat$value, c(0.95, 0.99))
    

    There are plenty of ways of getting a factor to determine which group each observation falls into, here is one:

    dat$quant  <- with(dat, factor(ifelse(value < quants[1], 0, 
                                      ifelse(value < quants[2], 1, 2))))
    

    So quant now indicates whether an observation is in the 95-99 or 99+ group. The colour of the points in a plot can then easily be mapped to quant.

    ggplot(dat, aes(position, value)) + geom_point(aes(colour = quant)) + facet_wrap(~key) +
      scale_colour_manual(values = c("black", "blue", "red"), 
                          labels = c("0-95", "95-99", "99-100")) + theme_bw()
    

    enter image description here

提交回复
热议问题