rggplot2bar-chartaxis-labelsstacked-bar-chart

Nested grouping variables for stacked bar graph


I'm trying to improve the visualization of this figure by separating the x-axis into nested subcategories of samples. I'm not able to use ggplot fill for this since I've already used pivot_longer() to include 6 types of output. The solution also has to allow a free scale view using facet_wrap() to examine trends between individual protein components.

Have: Current figure

Want: Desired figure

Attempt: facet_wrap with free y-axis (pardon the x-axis labels, I've tried to conceal my actual data)

Data frame:

>head(data)
>  ShortName  Mod Growth Day    Group        Conc
>1  CTRL_D02 CTRL     CC   2 Protein1 0.013072917
>2  CTRL_D02 CTRL     CC   2 Protein2 0.000000000
>3  CTRL_D02 CTRL     CC   2 Protein3 0.004661458
>4  CTRL_D02 CTRL     CC   2 Protein4 0.000000000
>5  CTRL_D02 CTRL     CC   2 Protein5 0.000000000
>6  CTRL_D02 CTRL     CC   2 Protein6 0.025885417

>CC <- data %>% slice(1:144)
>LG <- data %>% slice(145:210)

ggplot, stacked bar graph to view broad quantity of all proteins

p0 <- ggplot(CC) + 
  geom_col(aes(x = ShortName, y = Conc, fill = Group)) + 
  theme_clean() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
  labs(y = "Concentration", x = "Sample", fill = "Composition")

ggplot, view trends of individual protein components

p0.1 <- p0 + 
  facet_wrap(. ~ Group, scales = "free_y") + 
  theme(legend.position="none")

Solution

  • You can use the ggh4x package to do the nesting of the x-axis tick labels.

    library(ggplot2); library(ggh4x)
    
    ggplot(data, aes(x=interaction(Day,Mod), Conc, fill = Group)) + 
      geom_col() + 
      scale_x_discrete(guide="axis_nested") +  # *** ggh4x *** #
      facet_grid(~Mod, scales = 'free') +
      labs(x="Sample", y="Concentration", fill="Composition", 
           title="Stacked bar graph of convoluted timecourse") +
      #theme_clean() +
      theme_minimal() +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
            panel.grid.major.x = element_blank(), 
            panel.grid.minor.x = element_blank(),
            strip.text = element_blank(),
            axis.line.x = element_line(),
            axis.line.y = element_line(),
            legend.box.background = element_rect())
    

    enter image description here


    data <- tibble(Day=sample(c('D02','D04','D07','D11','D02','D14'), 210, TRUE),
                       Mod=sample(c('CTRL','P2E8','P2G7','poly'), 210, TRUE),
                       Growth=sample(c('CC','LG'), 210, TRUE),
                       Group=sample(paste0("Protein", 1:6), 210, TRUE, prob=c(10,1,1,1,1,1)),
                       Conc=runif(210, 0, 0.3))