在 R 中,如何根据列表自动绘图?

in R, how to automatically plots based on lists?

我有两个产品类别的调查结果,这是数据

surveyresults<-data.frame("Name"=c("Adam","John"),
"Gender"=c("m","m"),
"City"=c("Sydney","Melbourne"),
"Product"=c("Fashion","Electronics"),
"fash_pay_cash"=c(5,"NA"),
"fash_pay_card"=c(5,"NA"),
"elc_pay_cash"=c("NA",5),
"elc_pay_card"=c("NA",6),
"fash_DlvTime_morning"=c(5,"NA"),
"fash_DlvTime_afternoon"=c(7,"NA"),
"elc_DlvTime_morning"=c("NA",5),
"elc_DlvTime_afternoon"=c("NA",8))
surveyresults

我需要绘制此列表中的每个元素

fshnprxlist<-list("fash_pay","fash_DlvTime")

表示需要绘制的每一列的前缀,应该是绘制标题名称

因为我是调查创建者,所以我根据这个列表构建了它

list1<-list("fashion","electronics")
listPM<-list("cash","card")
listDT<-list("morning","afternoon")
fshlistPM<-paste("fash_pay",listPM,sep="_")
fshlistDT<-paste("fash_DlvTime",listDT,sep="_")
elcprxlist<-list("elc_pay","elc_DlvTime")
elclistPM<-paste("elc_pay",listPM,sep="_")
elclistDT<-paste("elc_DlvTime",listDT,sep="_")

我的要求很简单,使用以“第一个元素中每个元素的文本”开头的列为第一个列表的每个元素动态创建绘图

在这里,我们可以reshape成'long'格式,然后用ggplot绘制

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
p1 <- surveyresults %>%
      type.convert(as.is = TRUE) %>%
      pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon, 
        names_to = c("group", ".value"), names_pattern = "^(\w+_\w+)_(\w+)$") %>% 
      pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>% 
      group_by(group, name) %>%
      summarise(value = sum(value)) %>%
      ggplot(aes(x = name, y = value, fill = group)) + 
        geom_col(position = 'dodge') +
        facet_wrap(vars(group))

ggplotly(p1)

-输出


或者我们可以使用 ggforce

中的 facet_wrap_paginate
library(ggforce)
p1 <- surveyresults %>%
       type.convert(as.is = TRUE) %>%
       pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon, 
         names_to = c("group", ".value"),
               names_pattern = "^(\w+_\w+)_(\w+)$") %>% 
       pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>% 
       group_by(group, name) %>%
       summarise(value = sum(value)) %>%
       ggplot(aes(x = name, y = value, fill = group)) + 
         geom_col(position = 'dodge') +
         facet_wrap_paginate(~ group, ncol = 1, nrow = 2, page = 2)

n <- n_pages(p1)
pdf('surveyout.pdf')
for(i in seq_len(n)) print(p1 + 
           facet_wrap_paginate(~ group, ncol = 1, nrow = 2, page = i))
dev.off()

或者可以拆分成更小的子集,在每个子集中创建 ggplot,然后分别在其上应用 plotly

library(purrr)
lstOut <- surveyresults %>%
           type.convert(as.is = TRUE) %>%
           pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon, 
              names_to = c("group", ".value"),
                names_pattern = "^(\w+_\w+)_(\w+)$") %>% 
           pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>% 
           group_by(group, name) %>%
           summarise(value = sum(value)) %>%
           ungroup %>%
           group_split(grp =(as.integer(factor(group, 
               levels = unique(group)))%/% 3) + 1) %>%
           map(~ 
             ggplot(.x, aes(x = name, y = value, fill = group)) + 
             geom_col(position = 'dodge') +
             facet_wrap(vars(group)) -> p)

ggplotly(lstOut[[1]])
ggplotly(lstOut[[2]])