Search code examples
rvisualizationpatchwork

Plot too Wide in Patchwork (R)


I've been struggling to understand the patchwork package in R. The barchart on the right of the plot should be much thinner, like the one on top of the scatter plot. Below is my code. Any suggestions?

Thank you!

(fac1_trajectory_bp + coord_flip()) / (p_bm) + 
  plot_layout(height = c(0.125,1)) | 
  (guide_area() / fac1_trajectory_bp) + 
  plot_layout(heights = c(0.125,1), widths = c(0.125,1))

enter image description here


Solution

  • Instead of using | and / and multiple plot_layouts I find it much easier to set the layout via plot_layout, e.g. using ncol and byrow or using the design argument:

    library(patchwork)
    library(ggplot2)
    
    fac1_trajectory_bp <- ggplot()
    p_bm <- ggplot()
    
    (fac1_trajectory_bp + coord_flip()) + 
      p_bm + 
      guide_area() + 
      fac1_trajectory_bp +
      plot_layout(
        widths = c(1, 0.125),
        heights = c(0.125, 1),
        byrow = FALSE,
        ncol = 2
      )
    

    
    design <-
    "
    AC
    BD
    "
    
    (fac1_trajectory_bp + coord_flip()) + 
      p_bm + 
      guide_area() + 
      fac1_trajectory_bp +
      plot_layout(
        widths = c(1, 0.125),
        heights = c(0.125, 1),
        design = design
      )