Amara Alexander
Amara Alexander

Reputation: 1

Autonumbering figures in R in sequence

I have a function for generating 8 ggplot/ggiraph maps. I want to number the figures from 8 to 16 in the sequence they are generated. I've attempted to build a function for tagging the figures, to no avail.

#Snippet of Map Function 
demozipfx <- function(x) {
  out <- ggplot(data = x) +
    geom_sf_interactive(aes(geometry = geometry), fill = "grey80", color = "white") +
    geom_sf_interactive(aes(fill = as.numeric(rate), tooltip= tooltip), 
      data_id= zip),
      colour = "white")+
    geom_sf(
      data = county.simp,
      fill = NA,
      color= "grey20",
      lwd = 1) +
    scale_fill_viridis_c_interactive(
      name = "% Population Fully\nVaccinated",
      aesthetics = "fill",
      option = "D",
      direction = -1,
      na.value = "grey80",
      labels = percent)  +              
    labs(title = title)) +
    theme(plot.title = element_text(hjust = 0.5))
  return(out)
}

#output 
zipdemomaps.l <- lapply(zip.demo.mapdat, demozipfx)

zipdemomaps <- ggiraph(ggobj= plot_grid(plotlist= zipdemomaps.l[1:8], nrow=4), width_svg = 12, height_svg = 20)

Upvotes: 0

Views: 33

Answers (1)

teunbrand
teunbrand

Reputation: 38013

You can include an extra argument to your function for the tags. Simplified example below:

library(patchwork)
library(ggplot2)

my_fun <- function(x, i) {
  ggplot(data = x) +
    geom_point(aes(mpg, wt)) +
    labs(tag = i)
} 

plots <- mapply(my_fun, x = rep(list(mtcars), 9), i = 8:16,
                SIMPLIFY = FALSE)
wrap_plots(plots)

Created on 2021-08-18 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions