I have the following example data where I'm trying to plot var1
using geom_jitter()
and would like to shade the background multiple colours with geom_rect()
based on t_score
. I'm trying to replicate this method which was presented elsewhere where I'm preserving the y axis as the raw value (var1
) but shading the background based on the standardised (t_score
) value. I created a separate column in my data (rect_colour
) with the colours I'd like for certain values, but I'm not sure if this is thr right approach or not. Some help will be greatly appreciated. Thanks!
library(tidyverse)
set.seed(10)
dat <- data.frame(
var1 = round(rnorm(100, 0.200, 0.400), 3),
label = rep("Category 1", times = 100)
)
dat$var1_scaled <- scale(dat$var1)
dat$t_score <- round( ( dat$var1_scaled * 10 ) + 50)
dat <- dat %>%
mutate(rect_colour = case_when(t_score > 80 ~ "darkgreen",
t_score >= 46 & t_score <= 79 ~ "white",
t_score >= 40 & t_score <= 45 ~ "orange",
t_score >= 30 & t_score <= 39 ~ "lightred",
t_score >= 20 & t_score <= 29 ~ "red",
t_score < 20 ~ "darkred"))
ggplot(dat, aes(x = label, y = var1)) +
geom_jitter(size = 4, alpha = 0.5, width = 0.15)