Reputation: 489
How would one set the color of data points depending if they above or below a 1:1 line?
It's very simple to do this for a vertical or horizontal line (abline), but how would one do this for a 1:1 line?
Here is how to do it for a horizontal line:
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)
How would one do this for a 1:1 line where:
abline(0, 1)
Upvotes: 1
Views: 930
Reputation: 2460
This solution below will plot point colors according to how far they are from the abline.Simply calculate the deviation from the abline, then convert it to a color gradient and use it in the col argument in plot:
x <- c(2,4,6,8,10)
y <- c(2,4.9,5,9,12)
deviation <- abs(y-x)
rbPal <- colorRampPalette(c('blue','red'))
data_col=rbPal(10)[as.numeric(cut(deviation,breaks = 10))]
df <- cbind(x,y,deviation)
plot(x, y, xlim=c(0,15), ylim=c(0,15), pch = 21, col=data_col, bg=data_col)
abline(0, 1)
Upvotes: 1
Reputation: 263362
Just test for x > y
or equivalently 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)
Upvotes: 5