0

I'm looking for help and inspiration as of how to illustrate a point in a graph of mine:

enter image description here

The data can be divided into 3 time periods. Period1: day 0, Period2: day 4 - day 66 and Period 3: day 81 - day 116. I would like to illustrate this in my graph in some way but I'm not sure how. I was thinking of making 3 horizontal brackets below the x-axis but I'm not sure how to do this and I don't think it is a super good method.

Any ideas?

The dataset looks like this:

df<-structure(list(days_incubated = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L), .Label = c("0", "4", "10", "17", "24", "66", "81", "94", 
"116"), class = "factor"), rel_soil = c(0.79, 0.57, 0.32, 0.31, 
0.38, 2.23, 0.56, 1.33, 0.97, 0.05, 0.05, 0.04, 0.03, 0.03, 0.09, 
0.02, 0.25, 0.17, 92.68, 93.52, 91.96, 89.64, 89.55, 86.76, 94.73, 
84.74, 79.99, 6.4, 5.75, 7.59, 9.92, 9.91, 10.44, 4.56, 12.92, 
18.17, 0.01, 0.02, 0.02, 0.02, 0.02, 0.1, 0.03, 0.22, 0.3, 0.07, 
0.09, 0.08, 0.09, 0.1, 0.37, 0.1, 0.54, 0.39), group2 = c("CxHy", 
"CxHy", "CxHy", "CxHy", "CxHy", "CxHy", "CxHy", "CxHy", "CxHy", 
"CxHyNz", "CxHyNz", "CxHyNz", "CxHyNz", "CxHyNz", "CxHyNz", "CxHyNz", 
"CxHyNz", "CxHyNz", "CxHyOz", "CxHyOz", "CxHyOz", "CxHyOz", "CxHyOz", 
"CxHyOz", "CxHyOz", "CxHyOz", "CxHyOz", "mz<137", "mz<137", "mz<137", 
"mz<137", "mz<137", "mz<137", "mz<137", "mz<137", "mz<137", "mz>137", 
"mz>137", "mz>137", "mz>137", "mz>137", "mz>137", "mz>137", "mz>137", 
"mz>137", "Terpenoids", "Terpenoids", "Terpenoids", "Terpenoids", 
"Terpenoids", "Terpenoids", "Terpenoids", "Terpenoids", "Terpenoids"
)), row.names = c(NA, -54L), groups = structure(list(days_incubated = structure(1:9, .Label = c("0", 
"4", "10", "17", "24", "66", "81", "94", "116"), class = "factor"), 
    .rows = structure(list(c(1L, 10L, 19L, 28L, 37L, 46L), c(2L, 
    11L, 20L, 29L, 38L, 47L), c(3L, 12L, 21L, 30L, 39L, 48L), 
        c(4L, 13L, 22L, 31L, 40L, 49L), c(5L, 14L, 23L, 32L, 
        41L, 50L), c(6L, 15L, 24L, 33L, 42L, 51L), c(7L, 16L, 
        25L, 34L, 43L, 52L), c(8L, 17L, 26L, 35L, 44L, 53L), 
        c(9L, 18L, 27L, 36L, 45L, 54L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

The code for the plot looks like this

(
  p1 <- df %>% 
    ggplot()+
    geom_bar(aes(x=days_incubated, y=rel_soil, 
                 fill = factor(group2, levels=c("CxHyOz", "CxHy","CxHyNz", "Terpenoids", "mz>137" ,"mz<137"))),
                 stat="identity",
                 color="black",
                 width=1)+
    scale_fill_brewer(palette="Dark2") +
    labs(x = "Time (day)", y = "Relative BVOC emission contribution (%)", title = "Relative BVOC emissions", fill = "") +
    scale_x_discrete(expand = c(0,0))+
    scale_y_discrete(expand = c(0,0))+
    theme_bw() 
    
)

Tiptop
  • 533
  • 5
  • 19
  • 2
    Maybe facets on the period variable would be a place to start? There is a lot of flexibility to change the look of the facets, where the strips are, how far apart panes are, etc. – aosmith May 07 '21 at 14:33
  • 1
    any of this helpful? https://stackoverflow.com/questions/10525957 – brian avery May 07 '21 at 15:43

1 Answers1

1

As suggested above, a handy way can be to assign a new 'period' variable and use facet_grid (with settings to ensure it doesn't expand each part to the same size):

library(tidyverse)

df %>%
  mutate(period = case_when(
    days_incubated == "0" ~ 1,
    days_incubated %in% c("4", "10", "17", "24", "66") ~ 2,
    TRUE ~ 3,
  )) %>%
  # Your code from example
  ggplot() +
  geom_bar(
    aes(
      x = days_incubated,
      y = rel_soil,
      fill = factor(
        group2,
        levels = c("CxHyOz", "CxHy", "CxHyNz", "Terpenoids", "mz>137" , "mz<137")
      )
    ),
    stat = "identity",
    color = "black",
    width = 1
  ) +
  scale_fill_brewer(palette = "Dark2") +
  labs(x = "Time (day)",
       y = "Relative BVOC emission contribution (%)",
       title = "Relative BVOC emissions",
       fill = "") +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  theme_bw() +
  # Adding a facet to space out periods
  facet_grid( ~ period, scales = "free_x", space = "free_x")

This is just using a simple integer (1, 2, 3) to make period facets. You'd likely want to use a named, ordered vector to display "Period 1" etc., but that should be straightforward in the pre-processing mutate parts.

Created on 2021-05-07 by the reprex package (v2.0.0)

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22