How to define color groups based on numerical thresholds for the ggplot2 scatterplot

I have a dataset containing 2 variables x= event number and y= analysis amplitude. I am trying to create a scatterplot in ggplot2, where all the points that are are > 3000painted in one color and all the points < 3000have a different color.

I can get a graph and change the color for all data points, but I can’t figure out how to determine the color scheme based on the threshold of the value.

Here is an example of the data I use:

dat <- data.frame(x=c(399, 16022, 14756, 2609, 1131, 12135, 
                                 7097, 12438, 12604, 14912, 11042, 
                                 14024, 7033, 4971, 15533, 4507, 4627, 
                                 12600, 7458, 14557, 3999, 3154, 6073),
                  y=c(3063.40137, 3687.42041, 3911.856, 
                                    4070.91748, 4089.99561, 4095.50317,
                                    4159.899, 4173.117, 4177.78955, 
                                    4186.46875, 4201.874, 4272.022, 
                                    638.615, 649.8995, 668.8346,
                                    688.754639, 711.92, 712.689636, 
                                    721.1352, 737.841, 741.0727, 
                                    755.2549, 756.730652))
+4
source share
1 answer

. @hrbrmstr, cut - ( ).

dat$col <- cut(dat$y,
               breaks = c(-Inf, 3000, Inf),
               labels = c("<=3000", ">3000"))

ggplot(dat, aes(x = x, y = y, color = col)) +
  geom_point()
+5

Source: https://habr.com/ru/post/1535188/


All Articles