making function in R

I have the following two graphs I make:

animal_arrange <-
  grid.arrange(
    dogs_male + 
      ggtitle("A. Dogs") +
    cats_male + 
      ggtitle("B. Cats") +
   elepahnts_male + 
      ggtitle("C. Elephant") +
    ncol = 1
  )

animal_arrange <-
  grid.arrange(
    dogs_female + 
      ggtitle("A. Dogs") +
    cats_female + 
      ggtitle("B. Cats") +
   elepahnts_female + 
      ggtitle("C. Elephant") +
    ncol = 1
  )

is there a way to create a tidyverse function to avoid unnecessary lines of codes? I just want the same graph for the data frames that end with "animal"_female and "animal"_male...

Hi, hard to tell without seeing your datasets, but possibly something with ggplot2::facet_wrap() or another package like patchwork or cowplot.

Wrap a 1d ribbon of panels into 2d — facet_wrap • ggplot2 (tidyverse.org)

The Composer of Plots • patchwork (data-imaginist.com)

Streamlined Plot Theme and Plot Annotations for ggplot2 • cowplot (wilkelab.org)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.