I'm looking to create a scatterplot within R that has points that are split into four quadrants and then applies a color to each quadrant based on group.
I have used the following code which works, but need to change the colours.
x_mid <- mean(c(max(iris$Petal.Length, na.rm = TRUE),
min(iris$Petal.Length, na.rm = TRUE)))
y_mid <- mean(c(max(iris$Petal.Width, na.rm = TRUE),
min(iris$Petal.Width, na.rm = TRUE)))
library(dplyr)
library(ggplot2)
iris %>%
mutate(quadrant = case_when(Petal.Length > x_mid & Petal.Width > y_mid ~ "Q1",
Petal.Length <= x_mid & Petal.Width > y_mid ~ "Q2",
Petal.Length <= x_mid & Petal.Width <= y_mid ~ "Q3",
TRUE ~ "Q4")) %>%
ggplot(aes(x = Petal.Length, y = Petal.Width, color = quadrant)) +
geom_vline(xintercept = x_mid) + # plot vertical line
geom_hline(yintercept = y_mid) + # plot horizontal line
geom_point()