Home > Blockchain >  How to use geom_rect() based on different y value?
How to use geom_rect() based on different y value?

Time:09-04

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)

CodePudding user response:

Use geom_rect to draw the background areas, each with its color. To do this, summarise the data computing values for ymin and ymax.

Color "lightred" doesn't exist as you can see with function colors(). From its documentation:

Description
Returns the built-in color names which R knows about.

colors() |> grep("red$", x = _, value = TRUE)
#> [1] "darkred"         "indianred"       "mediumvioletred" "orangered"      
#> [5] "palevioletred"   "red"             "violetred"

Created on 2022-09-03 by the reprex package (v2.0.1)

I have substituted "orangered" for "lightred".
(Note the new pipe operator introduced in R 4.1 and the new placeholder introduced in R 4.2.)

suppressPackageStartupMessages(
  library(tidyverse)
)

set.seed(10)

dat <- data.frame(
  var1 = round(rnorm(100, 0.200, 0.400), 3),
  label = rep("Category 1", times = 100)
)

colors_vec <- c("darkgreen", "white", "orange", "orangered", "red", "darkred")
colors_vec <- setNames(colors_vec, colors_vec)

dat <- dat %>%
  mutate(var1_scaled = scale(var1),
         t_score = round( ( var1_scaled * 10 )   50)) %>%
  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 ~ "orangered",
                                 t_score >= 20 & t_score <= 29 ~ "red",
                                 t_score < 20 ~ "darkred"))

dat_rect <- dat %>%
  group_by(label, rect_colour) %>%
  summarise(ymin = min(var1), .groups = "drop") %>% 
  arrange(ymin) %>%
  mutate(ymax = lead(ymin, default = Inf))

ggplot(dat, aes(x = label, y = var1))  
  geom_rect(data = dat_rect,
            mapping = aes(ymin = ymin, ymax = ymax,
                          fill = rect_colour, colour = rect_colour),
            xmin = -Inf, xmax = Inf,
            inherit.aes = FALSE)  
  geom_jitter(size = 4, alpha = 0.5, width = 0.15)  
  scale_fill_manual(values = colors_vec)  
  scale_color_manual(values = colors_vec)

Created on 2022-09-03 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related