Search code examples
rggplot2patchwork

patchwork 'ncol' Error: Theme element `ncol` is not defined in the element hierarchy


I am trying to plot 6 ggplot objects with patchwork such that it plots 3 ggplot objects in each row. Upon running the code below; I get an error while running Final.Plot. How can this be fixed? On a side note the object patchwork works fine on its own as intended.

patchwork = (ggplot1+ ggplot2 + ggplot3) / (ggplot4 + ggplot5 + ggplot6) 
Final.Plot = patchwork + 
  plot_annotation(tag_levels = 'A')  & theme(legend.position = "bottom") +
  plot_layout(guides = "collect") 

Error

'ncol'  Error: Theme element `ncol` is not defined in the element hierarchy.

Solution

  • Ran into the same problem. Turns out the order matters. I got it to work by putting plot_layout() before the plot_annotation().

    patchwork = (ggplot1+ ggplot2 + ggplot3) / (ggplot4 + ggplot5 + ggplot6) 
    Final.Plot = patchwork + 
      plot_layout(guides = "collect") +
      plot_annotation(tag_levels = 'A')  & theme(legend.position = "bottom")