【问题标题】:Autonumbering figures in R in sequence按顺序自动编号R中的数字
【发布时间】:2021-08-18 14:14:11
【问题描述】:

我有一个生成 8 个 ggplot/ggiraph 地图的功能。我想按照它们生成的顺序对数字从 8 到 16 进行编号。我试图构建一个用于标记数字的函数,但无济于事。

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

【问题讨论】:

    标签: r function ggplot2 tags figure


    【解决方案1】:

    你可以在你的函数中为标签添加一个额外的参数。 简化示例如下:

    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)
    

    reprex package (v0.3.0) 于 2021 年 8 月 18 日创建

    【讨论】:

      猜你喜欢
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多