0

I have this scatterplot at the moment.

ggplot(pl, mapping = aes(x, y))+
  geom_point()+
  scale_x_continuous(limits = c(0,100))+
  scale_y_continuous(limits = c(0,100))+
  geom_abline(slope = 1, intercept = 1, color = "red")

which produces this

plot

I want to colour any points below the diagonal red and any above the diagonal green. So basically colour on the condition x < y. Thank you.

  • 1
    Does this answer your question? [Changing the shape of one point or few points in a scatter plot in R](https://stackoverflow.com/questions/67652479/changing-the-shape-of-one-point-or-few-points-in-a-scatter-plot-in-r) – user438383 Jul 05 '21 at 11:50

2 Answers2

1

You can add the color argument in the ggplot() function with a condition x<y, and adding scale_color_identity() :

 ggplot(pl, mapping = aes(x, y,color=ifelse(x<y,"green","red")))+
  
  geom_point()+
  scale_x_continuous(limits = c(0,100))+
  scale_y_continuous(limits = c(0,100))+
  geom_abline(slope = 1, intercept = 1, color = "red")+
  
  scale_color_identity()
Basti
  • 1,703
  • 3
  • 10
  • 25
0

You can add a column with the condition, and use that to color code your plot:

library(dplyr)
library(ggplot2)

ggplot(pl %>% mutate(threshold = case_when(
  x < y ~ TRUE,
  x >= y ~ FALSE),
mapping = aes(x, y, color = threshold))+
  geom_point()+
  scale_x_continuous(limits = c(0,100))+
  scale_y_continuous(limits = c(0,100))+
  geom_abline(slope = 1, intercept = 1, color = "red")
Rosalie Bruel
  • 1,423
  • 1
  • 10
  • 22