在R中,如何根据列表自动进行绘制?

时间:2019-12-15 13:05:57

标签: r r-plotly

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

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

代表需要绘制的每一列的前缀,并且应该是图标题名称 like this

由于我是调查的创建者,所以我根据此列表进行构建

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="_")

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

1 个答案:

答案 0 :(得分:1)

在这里,我们可以将其重塑为“长”格式,然后使用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)

-输出

enter image description here


或者我们可以使用facet_wrap_paginate中的ggforce

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,然后分别在其上分别应用

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