Search code examples
rggplot2label

Adding title to top of ggplot


I created a plot successfully, with 2 facets. However, there is a third grouping factor I would like to create - the first four columns should be labeled "A" and the last four "B" with the titles centered but along the same row. I've added an image of what I am looking for. Does anyone know how to do this in ggplot2? It would also be great to have some sort of blank column separating the two groups visually. I would just create 2 plots and join them, but the problem is that adding the axes on the left-most group makes the boxes smaller than the latter four boxes. I don't include the axes here for confidentiality.enter image description here

Here is my code

Carbon.p<-ggplot(data.QC.1[!data.QC.1$hemiparasite %in% c("TRER", "TRVE", "CAHI"),], aes(x=type.full, y = Value))+
  # geom_point(aes(color=type.full))+
  stat_summary(fun = "mean", shape = "-", size = 2.5)+
  scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) +  
  facet_grid(host ~ hemiparasite, scales= "free", switch = "y")+
  theme_bw()+
  geom_jitter(aes(color = type.full), alpha = 0.5))+
  theme(text = element_text(size = 10),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        # panel.border = element_blank(),
        axis.line.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        legend.title= element_blank(),
        legend.position = "bottom",
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.key = element_blank(), 
        strip.background = element_rect(colour="black", fill="white"),
       # strip.text.y = element_blank())+
  # axis.text.x =  element_text(hjust = 0.5, angle = 90))+ ## why isn't this working
  labs(x = expression(""),
       y = expression("d13C"),
       title = "")

Thanks!


Solution

  • Option 1 - use annotations or geom_text outside the plot range, constraining the view window with coord_cartesian and clip = off.

    library(ggplot2); library(dplyr) 
    mtcars %>%
      mutate(label = "") %>%
      add_row(wt = 3.5, mpg = 42, am = 0, carb = c(2, 6), label = c("A", "B")) %>%
      ggplot(aes(wt, mpg, label = label)) +
      geom_point(data = . %>% filter(label == "")) +
      geom_text() +
      facet_grid(am~carb) +
      coord_cartesian(ylim = c(10,35), clip = "off") +
      theme(plot.margin = unit(c(1,0,0,0), "line"))
    

    enter image description here

    Option 2 - Two separate charts joined with patchwork.

    library(patchwork)
    plot_carbs <- function(my_carbs, label, axis = TRUE) {
      mtcars %>%
        filter(carb %in% my_carbs) %>%
        ggplot(aes(wt, mpg)) +
        geom_point() +
        facet_grid(am~carb) +
        labs(title = label) +
        theme(plot.title = element_text(hjust = 0.5)) +
        { if(!axis) theme(
          axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.title.y = element_blank()) } +
        coord_cartesian(xlim = c(1.5, 6), ylim = c(10,35))
    }
    
    plot_carbs(1:3, "A") +
    plot_carbs(c(4,6,8), "B", FALSE) 
      
    

    enter image description here