【问题标题】:Loop one column and apply gt function to generate multiple plots using R循环一列并应用 gt 函数以使用 R 生成多个图
【发布时间】:2022-01-06 20:03:25
【问题描述】:

给定一个数据样本如下:

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

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

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"))%&gt;% 来重命名每个plot 的名称为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"))

【问题讨论】:

    标签: r for-loop dplyr gt


    【解决方案1】:

    我会将您的代码转换为函数,然后将每个类别放入列表中各自的数据框中,然后使用 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)
    

    输出

    【讨论】:

    • 非常感谢,您节省了我一天的时间。我怎么能删除输出中的category 列,因为它已经在每个图的标题中指示了?这意味着我希望在最终输出中保留 3 列 variablepricequantity
    • @ahbon 没问题。您可以在函数中添加一行以使用dplyr::select(-category) 删除category。我已经更新了答案以反映这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多