Using Conditional Statements to Change the Color of Data Points

后端 未结 4 830
攒了一身酷
攒了一身酷 2021-01-13 08:39

I have a data set, which I have used to make a scatter plot and I would like to assign three different colors to the data points within three different regions, based on the

相关标签:
4条回答
  • 2021-01-13 09:08

    Just use nested ifelses:

    plot(...., col=ifelse(x < 3, "red", ifelse(x > 1549, "purple", "black")))
    
    0 讨论(0)
  • 2021-01-13 09:10

    You can define a vector of colors and pass it to the col argument of plot. Something like this :

    colors <- rep("black", length(x))
    colors[x<3] <- "red"
    colors[x>1549] <- "pink"
    
    plot(x, y, xlab="chr X position (Mb)",
               ylab="Diversity",
               pch=16, cex =0.7, 
               col = colors)    
    
    0 讨论(0)
  • 2021-01-13 09:18

    Also, the "classic" findInterval:

    col = c("red", "black", "purple")[findInterval(x, v = c(0,3,1549))]

    0 讨论(0)
  • 2021-01-13 09:27

    I like the cut approach:

    set.seed(1)
    x <- sample(1600)
    col <- c("red", "black", "purple")
    col <- col[cut(x, breaks=c(-Inf, 3, 1549, Inf))]
    
    0 讨论(0)
提交回复
热议问题