Search code examples
rggplot2r-gridgtable

How to create plot with multiple labels on X axis, previous code suggestion doesn't seem to work


I have some data measuring rate of dislodgement for a species with three different variables (exposure, season and site). I would like to create a plot where Season and exposure are listed on the X axis and site is created in a legend. I have completed this easily enough in Excel, and would like to replicate the same type in R. At the moment, I'm using a piece of code which seemed to work for another user with a similar question on, but this doesnt seem to work with mine?

SCRIPT:

dput(Data2)
structure(list(Season = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L, 
1L, 1L, 4L, 4L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 4L, 4L, 
4L), .Label = c("Autumn", "Spring", "Summer ", "Winter"), class = "factor"), 
Exposure = structure(c(1L, 3L, 2L, 4L, 3L, 2L, 4L, 3L, 2L, 
4L, 3L, 2L, 1L, 3L, 2L, 4L, 3L, 2L, 4L, 3L, 2L, 4L, 3L, 2L
), .Label = c(" Sheltered", "Exposed", "Moderately Exposed", 
"Sheltered"), class = "factor"), Average = c(1L, 2L, 4L, 
3L, 4L, 2L, 2L, 4L, 2L, 4L, 3L, 2L, 2L, 5L, 4L, 3L, 2L, 1L, 
1L, 1L, 2L, 4L, 2L, 2L), Site = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), SEM = c(0.5, 0.1, 0.4, 0.5, 1, 0.5, 0.5, 0.5, 
0.5, 0.5, 0.2, 0.5, 0.5, 0.1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.3, 0.2, 0.5, 0.5)), class = "data.frame", row.names = c(NA, 
-24L))


`setwd("C:/Users/phl5/Documents/PippaPhD")
 getwd()
 read.csv("Graphed_Data.csv")
 Data2<-read.csv("Graphed_Data.csv")

 library(ggplot2)
 library(gtable)
 library(grid)

 dodge<- position_dodge(width=0.9)

 ggplot(Data2, aes(x = interaction(Exposure, Season), y = Average, fill 
  = factor(Site))) +
 geom_bar(stat = "identity", position = position_dodge()) +
 geom_errorbar(aes(ymax = Average + SEM, ymin = Average - SEM), position 
 = dodge, width = 0.2)


 g1<- ggplot(data = Data2, aes(x = interaction(Exposure, Season), y = 
 Average, fill = factor(Site))) +
 geom_bar(stat = "identity", position = position_dodge()) +
 geom_errorbar(aes(ymax = Average + SEM, ymin = Average - SEM), position 
 = dodge, width = 0.2) +
 coord_cartesian(ylim = c(0, 12.5))+ 
 annotate("text", x = 1:12, y = 400,
       label = rep(c("Exposed", "Moderately Exposed", "Sheltered"),4)) +
 annotate("text", c(0.5, 1.5, 2.0, 2.5), y = -800, label = c("Spring", 
 "Summer", "Autumn", "Winter"))+
 theme_classic()+
 theme(plot.margin = unit(c(1,1,1,1), "lines"),
    axis.title.x = element_blank(),
    axis.text.x = element_blank())

 g2 <- ggplot_gtable(ggplot_build(g1))
 g2$layout$clip[g2$layout$name == "panel"] <- "off"
 grid.draw(g2)`

Can anyone see if the is an obvious problem in my code that I'm using or if the is a different script that I could use?

Code: Output get from current code, with the problem of no x axis codes appearing at all

This is the kind of output I would want, and that I can create in Excel

I'm very much a beginner in R, but any help would be greatly appreciated.


Solution

  • Edit 2:

    For the OP's second question in the comment:

    1. There is no need to add a geom_hline() to display the axis, just add axis.line to the theme() and panel.spacing.x=unit(0, "lines") to make it continuous across facets
    gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), data=data)
    gg <- gg + geom_bar(stat = 'identity')
    gg <- gg + scale_fill_discrete(guide_legend(title = 'Site')) # just to get 'site' instead of 'as.factor(Site)' as legend title
    # gg <- gg + scale_fill_manual(values=c('black', 'grey85'), guide_legend(title = 'Site')) # to get bars in black and grey instead of ggplot's default colors
    # gg <- gg + theme_classic() # get white background and black axis.line for x- and y-axis
    gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
    gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
    gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05))) # remove space below zero
    gg <- gg + theme(axis.text.x = element_blank(),
                     axis.ticks.x = element_blank(),
                     axis.title.x = element_blank(),
                     axis.line = element_line(color='black'),
                     strip.placement = 'outside', # place x-axis above (factor-label-) strips
                     panel.spacing.x=unit(0, "lines"), # remove space between facets (for continuous x-axis)
                     panel.grid.major.x = element_blank(), # remove vertical grid lines
                     # panel.grid = element_blank(), # remove all grid lines
                     # panel.background = element_rect(fill='white'), # choose background color for plot area
                     strip.background = element_rect(fill='white', color='white')  # choose background for factor labels, color just matters for theme_classic()
    )
    
    1. To place exposure labels above season labels in the facet strips you can change the gtable overlayed on each strip
    # facet factor levels
    season.levels <- levels(data$Season)
    exposure.levels <- levels(data$Exposure)
    
    # convert to gtable
    g <- ggplotGrob(gg)
    
    # find the grobs of the strips in the original plot
    grob.numbers <- grep("strip-b", g$layout$name)
    # filter strips from layout 
    b.strips <- gtable_filter(g, "strip-b", trim = FALSE)
    # b.strips$layout shows the strips position in the cell grid of the plot
    # b.strips$layout
    season.left.panels <- seq(1, by=length(levels(data$Exposure)), length.out = length(season.levels))
    season.right.panels <- seq(length(exposure.levels), by=length(exposure.levels), length.out = length(season.levels))
    left <- b.strips$layout$l[season.left.panels]
    right <- b.strips$layout$r[season.right.panels]
    top <- b.strips$layout$t[1]
    bottom <- b.strips$layout$b[1]
    
    # create empty matrix as basis to overly new gtable on the strip
    mat   <- matrix(vector("list", length = 10), nrow = 2)
    mat[] <- list(zeroGrob())
    
    # add new gtable matrix above each strip
    for (i in 1:length(season.levels)) {
      res <- gtable_matrix("season.strip", mat, unit(c(1, 0, 1, 0, 1), "null"), unit(c(1, 1), "null"))
      season.left <- season.left.panels[i]
      # place season labels below exposure labels in row 2 of the overlayed gtable for strips
      res <- gtable_add_grob(res, g$grobs[[grob.numbers[season.left]]]$grobs[[1]], 2, 1, 2, 5)
      # move exposure labels to row 1 of the overlayed gtable for strips
      for (j in 0:2) {
        exposure.x <- season.left+j
        res$grobs[[c(1, 5, 9)[j+1]]] <- g$grobs[[grob.numbers[exposure.x]]]$grobs[[2]]
      }
      new.grob.name <- paste0(levels(data$Season)[i], '-strip')
      g <- gtable_add_grob(g, res, t = top,  l = left[i],  b = top,  r = right[i], name = c(new.grob.name))
      new.grob.no <- grep(new.grob.name, g$layout$name)[1]
      g$grobs[[new.grob.no]]$grobs[[nrow(g$grobs[[new.grob.no]]$layout)]]$children[[2]]$children[[1]]$gp <- gpar(fontface='bold')
    }
    
    grid.newpage()
    grid.draw(g)
    

    The result looks like this: enter image description here

    1. To also get the bars in black and grey as in your example picture change the ggplot like this:
    gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), data=data)
    gg <- gg + geom_bar(stat = 'identity')
    # gg <- gg + scale_fill_discrete(guide_legend(title = 'Site')) # just to get 'site' instead of 'as.factor(Site)' as legend title
    gg <- gg + scale_fill_manual(values=c('black', 'grey85'), guide_legend(title = 'Site')) # to get bars in black and grey instead of ggplot's default colors
    gg <- gg + theme_classic() # get white background and black axis.line for x- and y-axis
    gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
    gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
    gg <- gg + scale_y_continuous(expand = expand_scale(mult = c(0, .05))) # remove space below zero
    gg <- gg + theme(axis.text.x = element_blank(),
                     axis.ticks.x = element_blank(),
                     axis.title.x = element_blank(),
                     axis.line = element_line(color='black'),
                     strip.placement = 'outside', # place x-axis above (factor-label-) strips
                     panel.spacing.x=unit(0, "lines"), # remove space between facets (for continuous x-axis)
                     panel.grid.major.x = element_blank(), # remove vertical grid lines
                     # panel.grid = element_blank(), # remove all grid lines
                     # panel.background = element_rect(fill='white'), # choose background color for plot area
                     strip.background = element_rect(fill='white', color='white')  # choose background for factor labels, color just matters for theme_classic()
    )
    

    The result should look like this: enter image description here Edit:

    For the OP's question in the comment:

    1. Removing grid lines can be done using ggplot's theme():
    gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), data=data)
    gg <- gg + geom_bar(stat = 'identity')
    gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
    gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
    gg <- gg + scale_fill_discrete(guide_legend(title = 'Site'))
    gg <- gg + theme(axis.text.x = element_blank(),
                     axis.ticks.x = element_blank(),
                     axis.title.x = element_blank(),
                     panel.grid.major.x = element_blank(), # remove vertical grid lines
                     # panel.grid = element_blank(), # remove al grid lines
                     # panel.background = element_rect(fill='white'), # choose background color for plot area
                     strip.background = element_rect(fill='white')  # choose background for factor labels
                     )
    
    1. To have only one label for each season is a bit more tricky. You'll need to edit the gtable of the ggplot. One way to do so would be this:
    # facet factor levels
    season.levels <- levels(data$Season)
    exposure.levels <- levels(data$Exposure)
    # convert to gtable
    g <- ggplotGrob(gg)
    # find the grobs of the strips in the original plot
    grob.numbers <- grep("strip-b", g$layout$name)
    # filter strips from layout 
    b.strips <- gtable_filter(g, "strip-b", trim = FALSE)
    # b.strips$layout shows the strips position in the cell grid of the plot
    b.strips$layout
    season.left.panels <- seq(1, by=length(levels(data$Exposure)), length.out = length(season.levels))
    season.right.panels <- seq(length(exposure.levels), by=length(exposure.levels), length.out = length(season.levels))
    left <- b.strips$layout$l[season.left.panels]
    right <- b.strips$layout$r[season.right.panels]
    top <- b.strips$layout$t[1]
    bottom <- b.strips$layout$b[1]
    
    # create empty matrix as basis to overly new gtable on the strip
    mat   <- matrix(vector("list", length = 10), nrow = 2)
    mat[] <- list(zeroGrob())
    
    # add new gtable matrix above each strip
    for (i in 1:length(season.levels)) {
      res <- gtable_matrix("season.strip", mat, unit(c(1, 0, 1, 0, 1), "null"), unit(c(1, 1), "null"))
      res <- gtable_add_grob(res, g$grobs[[grob.numbers[season.left.panels[i]]]]$grobs[[1]], 1, 1, 1, 5)
      new.grob.name <- paste0(levels(data$Season)[i], '-strip')
      g <- gtable_add_grob(g, res, t = top,  l = left[i],  b = top,  r = right[i], name = c(new.grob.name))
      new.grob.no <- grep(new.grob.name, g$layout$name)
      g$grobs[[new.grob.no]]$grobs[[nrow(g$grobs[[new.grob.no]]$layout)]]$children[[2]]$children[[1]]$gp <- gpar(fontface='bold')
    }
    grid.newpage()
    grid.draw(g)
    

    enter image description here

    Original answer

    I think what you are looking for can – using ggplot() – be best achieved using facetting.

    data <- expand.grid(c('Spring', 'Summer', 'Autumn', 'Winter'), c('Sheltered', 'Moderately exposed', 'Exposed'), c(1, 2))
    names(data) <- c('Season', 'Exposure', 'Site')
    # adding some arbitrary values
    set.seed(42)
    data$Average <- sample(c(rep(3, 3), rep(2, 2), rep(1, 2), rep(NA, 17)))
    data$SEM <- NA
    SEM <- sample(c(rep(0.5, 3), rep(0.3, 2), rep(.1, 2)))
    data$SEM[which(!is.na(data$Average))] <- SEM
    
    gg <- ggplot(aes(x=as.factor(Site), y=Average, fill=as.factor(Site)), data=data)
    gg <- gg + geom_bar(stat = 'identity')
    gg <- gg + geom_errorbar(aes(ymin=Average-SEM, ymax=Average+SEM), width=.3)
    gg <- gg + facet_wrap(~Season*Exposure, strip.position=c('bottom'), nrow=1, drop=F)
    gg <- gg + scale_fill_discrete(guide_legend(title = 'Site'))
    gg <- gg + theme(axis.text.x = element_blank(),
                     axis.ticks.x = element_blank(),
                     axis.title.x = element_blank())
    print(gg)
    

    enter image description here