使用 gt 表绘制每行直方图 - R

Plot histograms per row using gt tables - R

我想创建一个 gt table,在其中我可以看到一些指标,例如观察次数、平均值和中位数,并且我想要一个带有直方图的列。对于这个问题,我将使用 iris 数据集。

我最近学会了如何使用这段代码将情节放在小标题中:

library(dplyr)
library(tidyr)
library(purrr)
library(gt)
my_tibble <- iris %>%
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  group_by(Vars) %>%
  summarise(obs = n(),
            mean = round(mean(Values),2),
            median = round(median(Values),2), 
            plots = list(ggplot(cur_data(), aes(Values)) + geom_histogram()))

现在我想使用 plots 列为每个变量绘制直方图,所以我尝试了这个:

my_tibble %>%
  mutate(ggplot = NA) %>%
  gt() %>%
  text_transform(
    locations = cells_body(vars(ggplot)),
    fn = function(x) {
      map(.$plots,ggplot_image)
    }
  )

但它 returns 我出错了:

Error in body[[col]][stub_df$rownum_i %in% loc$rows] <- fn(body[[col]][stub_df$rownum_i %in%  : 
  replacement has length zero

gt table 应该是这样的:

任何帮助将不胜感激。

我们需要遍历 plots

library(dplyr)
library(tidyr)
library(purrr)
library(gt)
library(ggplot2)
iris %>%
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  nest_by(Vars) %>%
  mutate(n = nrow(data),
         mean = round(mean(data$Values), 2), 
         median = round(median(data$Values), 2), 
         plots = list(ggplot(data, aes(Values)) + geom_histogram()), .keep = "unused") %>%
  ungroup %>%
  mutate(ggplot = NA) %>%
  {dat <- .
  dat %>%
    select(-plots) %>%
    gt() %>%
  text_transform(locations = cells_body(c(ggplot)),
                 fn = function(x) {
                  map(dat$plots, ggplot_image, height = px(100))
                 }
                 
                 
                 )
  }

-检查输出

更新: 见评论:

根据闪亮的应用程序,您可以使用 summarytools 请参阅此处:https://cran.r-project.org/web/packages/summarytools/vignettes/introduction.html

兼容r shiny!

这是一个小例子:

library(summarytools)
dfSummary(iris, 
          plain.ascii  = FALSE, 
          style        = "grid", 
          graph.magnif = 0.75, 
          valid.col    = FALSE,
          tmp.img.dir  = "/tmp")

view(dfSummary(iris))

试试这个:

library(skimr)
skim(iris)
  skim_variable n_missing complete_rate  mean    sd    p0   p25   p50   p75  p100 hist 
* <chr>             <int>         <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Sepal.Length          0             1  5.84 0.828   4.3   5.1  5.8    6.4   7.9 ▆▇▇▅▂
2 Sepal.Width           0             1  3.06 0.436   2     2.8  3      3.3   4.4 ▁▆▇▂▁
3 Petal.Length          0             1  3.76 1.77    1     1.6  4.35   5.1   6.9 ▇▁▆▇▂
4 Petal.Width           0             1  1.20 0.762   0.1   0.3  1.3    1.8   2.5 ▇▁▇▅▃

在回顾@akrun 和@TarJae 的优秀想法后,我有了这个解决方案,它提供了所需的 gt table:

plots <- iris %>% 
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  group_by(Vars) %>%
  nest() %>%
  mutate(plot = map(data, 
                    function(df) df %>% 
                      ggplot(aes(Values)) + 
                      geom_histogram())) %>%
  select(-data)

iris %>%
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  group_by(Vars) %>%
  summarise(obs = n(),
            mean = round(mean(Values),2),
            median = round(median(Values),2)) %>%
  mutate(ggplot = NA) %>%
  gt() %>%
  text_transform(
    locations = cells_body(vars(ggplot)),
    fn = function(x) {
      map(plots$plot, ggplot_image, height = px(100))
    }
  )

这是 table:

我不得不在输出 table 之外创建绘图,因此我可以在 gt table 中调用它 table。