0

I have a line graph and I am trying to shade the area below six separate geom_line() in two different colors. Red color before treatment and green color after treatment.

head(my_data)
  control control_x treat_1 treat_1_x post_treat_1 treat_1_1 treat_1_1_x treat_2 treat_2_x post_treat_2 treat_2_2 treat_2_2_x treat_3 treat_3_x
1     1.0         1     5.0         1          6.5       8.5           4     8.5         7         10.0      12.0          10    14.5         9
2     1.5         2     5.5         2          7.0       9.0           5     9.0         8         10.5      12.5          11    15.0        10
3     2.0         3     6.0         3          7.5       9.5           6     9.5         9         11.0      13.0          12    15.5        11
4     2.5         4     6.5         4          8.0      10.0           7    10.0        10         11.5      13.5          13    16.0        12
5     3.0         5      NA        NA           NA        NA          NA      NA        NA           NA        NA          NA      NA        NA
6     3.5         6      NA        NA           NA        NA          NA      NA        NA           NA        NA          NA      NA        NA
  post_treat_3 treat_3_3 treat_3_3_x xaxis
1         16.0      18.0          12     0
2         16.5      18.5          13     1
3         17.0      19.0          14     2
4         17.5      19.5          15     3
5           NA        NA          NA     4
6           NA        NA          NA     5

I have can add shaded areas using annotate("rect" ... ) However, I am not able to follow the geom_line() so the code results in the plot below.

ggplot(my_data, aes(x = xaxis)) +
  geom_line(aes(x = control_x, y = control)) +
  
  geom_line(aes(x = treat_1_x, y = treat_1), color = "#7e7e7e") +
  geom_line(aes(x = treat_1_1_x, y = treat_1_1), color = "#7e7e7e") +
  geom_line(aes(x = treat_1_1_x, y = post_treat_1), linetype = "dashed", color = "lightgrey") +
  geom_line(data = data.frame(x = c(4, 4), y = c(0, 9)), 
            aes(x = x, y = y), linetype = "dotted") +
  annotate("text", x = 4, y = -0.5, label = "t[1]", parse=TRUE) +
  annotate("rect", xmin = 1, xmax = 4, ymin = 0, ymax = 6.5, 
           alpha = .5, fill = "red") +
  annotate("rect", xmin = 4, xmax = 7, ymin = 0, ymax = 10, 
           alpha = .5, fill = "green") +
  
  geom_line(aes(x = treat_2_x, y = treat_2), color = "#5f5f5f") +
  geom_line(aes(x = treat_2_2_x, y = treat_2_2), color = "#5f5f5f") +
  geom_line(aes(x = treat_2_2_x, y = post_treat_2), linetype = "dashed", color = "lightgrey") +
  geom_line(data = data.frame(x = c(10, 10), y = c(0, 12.5)), 
            aes(x = x, y = y), linetype = "dotted") +
  annotate("text", x = 10, y = -0.5, label = "t[2]", parse=TRUE) +
  annotate("rect", xmin = 7, xmax = 10, ymin = 0, ymax = 10, 
             alpha = .5, fill = "red") +
  annotate("rect", xmin = 10, xmax = 13, ymin = 0, ymax = 13.5, 
             alpha = .5, fill = "green") +
  
  geom_line(aes(x = treat_3_x, y = treat_3), color = "#3f3f3f") +
  geom_line(aes(x = treat_3_3_x, y = treat_3_3), color = "#3f3f3f") +
  geom_line(aes(x = treat_3_3_x, y = post_treat_3), linetype = "dashed", color = "lightgrey") +
  geom_line(data = data.frame(x = c(12, 12), y = c(0, 18.5)), 
            aes(x = x, y = y), linetype = "dotted") +
  annotate("text", x = 12, y = -0.5, label = "t[3]", parse=TRUE) +
  annotate("rect", xmin = 9, xmax = 12, ymin = 0, ymax = 16, 
             alpha = .5, fill = "red") +
  annotate("rect", xmin = 12, xmax = 15, ymin = 0, ymax = 19.5, 
             alpha = .5, fill = "green") +
  
  geom_hline(yintercept = 0, linetype = "solid",
             color = "black", size = 1) +
  theme_minimal() +
  labs(x = "", y = "")

enter image description here

The desired result would be something similar to this where the shaded area is below the lines.

enter image description here

  • Marco, your code suggests that you've just begun with ggplot - the magic of using geom layers and aesthetics is that, given an appropriate data shape, you can bascially call geom only once instead of several times. THis requires typically a "long" shape of your data. You can find examples in the famous thread https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph, in which this same principle applies – tjebo Mar 18 '22 at 16:08

0 Answers0