如果数据点高于或低于1:1线,如何设置数据点的颜色?
对于垂直或水平线(abline)这样做非常简单,但是如何为1:1线执行此操作?
以下是如何为水平线做到这一点:
x <- c(2,4,6,8,10)
y <- c(2,4.9,5,9,12)
df <- cbind(x,y)
plot(df[,1], df[,2], xlim=c(0,15), ylim=c(0,15), pch = 21,
bg =ifelse(df[,2]<=5,"black","red"), col = "black", cex = 1.5,
cex.axis = 1.4, cex.lab = 1.4, xlab = "x", ylab = "y")
abline(h=c(5.0),lty=2)
Run Code Online (Sandbox Code Playgroud)
如何为1:1线做到这一点,其中:
abline(0, 1)
Run Code Online (Sandbox Code Playgroud)
只是测试x > y或等效y <= x:
plot(df[,1], df[,2], xlim=c(0,15), ylim=c(0,15), pch = 21,
bg =ifelse(df[,2] <= df[,1],"black","red"), col = "black", cex = 1.5,
cex.axis = 1.4, cex.lab = 1.4, xlab = "x", ylab = "y")
abline(0, 1)
Run Code Online (Sandbox Code Playgroud)