简体   繁体   English

R中的数字按顺序自动编号

[英]Autonumbering figures in R in sequence

I have a function for generating 8 ggplot/ggiraph maps.我有一个 function 用于生成 8 个 ggplot/ggiraph 地图。 I want to number the figures from 8 to 16 in the sequence they are generated.我想按照生成的顺序将数字从 8 编号到 16。 I've attempted to build a function for tagging the figures, to no avail.我试图构建一个 function 来标记数字,但无济于事。

#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)

You can include an extra argument to your function for the tags.您可以在 function 中为标签添加一个额外的参数。 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)reprex package (v0.3.0) 创建于 2021-08-18

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM