Search code examples
rif-statementggplot2highlight

R: How to have conditional geom_rect highlights for only a specific string in a column of strings for geom_point plot


I have the following dataframe and scatterplot

df <-
    setNames(data.frame(
        as.POSIXct(
            c(
            "2022-07-29 00:00:00",
            "2022-07-29 00:00:05",
            "2022-07-29 00:05:00",
            "2022-07-29 00:05:05",
            "2022-07-29 00:10:00",
            "2022-07-29 00:15:00",
            "2022-07-29 00:20:00",
            "2022-07-29 00:20:05"
            )),
        c(1, 2, 3, 4, 5, 6, 7, 8),
        c(0.8, 2.1, 2.5, 4.1, 5, 6.1, 6.9, 8.1),
        c("a", "a", "b", "b", "b", "b", "b", "c")
    ),
    c("timeStamp", "value1", "value2", "text"))

df %>% ggplot(aes(timeStamp, value1, color =text)) + geom_point()

I want to highlight on the graph the areas where a specific text value is in the same row as the data. for example, if we want to highlight with geom_rect the b values, here is my attempt:

df %>% ggplot(aes(timeStamp, value1, color =text)) + geom_point() +
   geom_rect(xmin= -00:00:05, xmax=  00:00:05, ymin=-0.2, ymax=0.2, color = ifelse(text=="b", 
  "yellow", ""), alpha =0.5)

I know this doesnt work but can't seem to find a solution. Also, I know it may seem more prudent to simply change the size of the data points and color of the data points but I need the rectangles for a specific analysis with a much larger dataset. Thank you!


Solution

  • You can try this,

    df %>% ggplot(aes(timeStamp, value1, color =text)) +
           geom_point() +
           annotate("rect", 
               xmin= min(df$timeStamp[df$text=="b"]), 
               xmax= max(df$timeStamp[df$text=="b"]), 
               ymin= min(df$value1[df$text=="b"]), 
               ymax= max(df$value1[df$text=="b"]),
               color = "yellow",fill="yellow", alpha = 0.2 )+
           theme_bw()
    

    or using geom_rect

    df %>% ggplot(aes(timeStamp, value1, color =text)) +
        geom_point() + geom_rect(data = subset(df,text=="b"),
              xmin= df$timeStamp[df$text=="b"]+60,
              xmax= df$timeStamp[df$text=="b"]-60,
              ymin= df$value1[df$text=="b"]+1,
              ymax= df$value1[df$text=="b"]-1,
              color = NA, fill="yellow", alpha = 0.2)+
        theme_light()