循环一列并应用 gt 函数使用 R 生成多个图

Loop one column and apply gt function to generate multiple plots using R

给定一个数据样本如下:

df <- structure(list(category = c("food", "food", "food", "food", "electronic product", 
"electronic product", "electronic product", "electronic product"
), type = c("vegetable", "vegetable", "fruit", "fruit", "computer", 
"computer", "other", "other"), variable = c("cabbage", "radish", 
"apple", "pear", "monitor", "mouse", "camera", "calculator"), 
    price = c(6, 5, 3, 2.9, 2000, 10, 600, 35), quantity = c(2L, 
    4L, 5L, 10L, 1L, 3L, NA, 1L)), class = "data.frame", row.names = c(NA, 
-8L))

我可以使用以下代码绘制 table 图:

library(gt)
library(magrittr)

dt <- df %>% 
  group_by(category) %>%
  gt() %>% 
  tab_header(
    title = md("Category name")
  )%>%
     tab_style(
     locations = cells_column_labels(columns = everything()),
     style     = list(
       #Give a thick border below
       cell_borders(sides = "bottom", weight = px(3)),
       #Make text bold
       cell_text(weight = "bold")
     )
   ) %>%
     tab_style(
     locations = cells_row_groups(groups = everything()),
     style     = list(
       cell_text(weight = "bold")
     )
   ) %>%
  cols_align(align = "center", columns = everything())
dt
gt::gtsave(dt, file = file.path("./Category_name.png"))

输出:

现在我希望循环 categorygroup_by(type) 为每个 category 生成多个图。同时,我还需要通过动态修改gtsave(dt, file = file.path("./Category_name.png"))tab_header(title = md("Category name"))%>%来重命名名称为category的每个地块。

我如何使用 R 和 gt 包实现这一目标?谢谢。

编辑: 绘制食物类别

food <- df %>% 
  filter(category=='food') %>% 
  group_by(type) %>%
  gt() %>% 
  tab_header(
    title = md("Food")
  )%>%
  fmt_missing(
    columns = where(is.numeric),
    missing_text = "-"
  ) %>%
     tab_style(
     locations = cells_column_labels(columns = everything()),
     style     = list(
       #Give a thick border below
       cell_borders(sides = "bottom", weight = px(3)),
       #Make text bold
       cell_text(weight = "bold")
     )
   ) %>%
     tab_style(
     locations = cells_row_groups(groups = everything()),
     style     = list(
       cell_text(weight = "bold")
     )
   ) %>%
  cols_align(align = "center", columns = where(is.character)) %>%
  cols_align(align = "right", columns = where(is.numeric))
gt::gtsave(food, file = file.path("./food.png"))

我会把你的代码变成一个函数,然后将每个类别放入列表中它们自己的数据框中,然后用 purrr::map 应用该函数。

library(tidyverse)
library(gt)

plot_category <- function(x) {
  p <- x %>%
    dplyr::select(-category) %>% 
    dplyr::group_by(type) %>%
    gt() %>%
    tab_header(title = md(str_to_title(x$category[1]))) %>%
    fmt_missing(columns = where(is.numeric),
                missing_text = "-") %>%
    tab_style(
      locations = cells_column_labels(columns = everything()),
      style     = list(
        #Give a thick border below
        cell_borders(sides = "bottom", weight = px(3)),
        #Make text bold
        cell_text(weight = "bold")
      )
    ) %>%
    tab_style(locations = cells_row_groups(groups = everything()),
              style     = list(cell_text(weight = "bold"))) %>%
    cols_align(align = "center", columns = where(is.character)) %>%
    cols_align(align = "right", columns = where(is.numeric))
  
  gt::gtsave(p, file = file.path(paste0("./", x$category[1], ".png")))
  
}

df %>%
  dplyr::group_split(category) %>%
  purrr::map(plot_category)

输出