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)