在 R 中,如何根据列表自动绘图? [英] in R, how to automatically plots based on lists?

查看:56
本文介绍了在 R 中,如何根据列表自动绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a survey results for two products categories, here is the data

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

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

and i need to plot each element in this list

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

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

which represents the prefix of each column needed to be ploted and should be the plot title name

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

since I'm the survey creator, so i built it based on this lists

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

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

my request is simply, dynamically create plot for each element of the 1st list using the column that starts with the "text of each element in the first elements

推荐答案

这里,我们可以重新整形为'long'格式,然后使用ggplot绘图

Here, we could reshape into 'long' format and then use ggplot to plot

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

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


Or it can be split into smaller subsets, create ggplot in each and then apply the plotly on it separately

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

这篇关于在 R 中,如何根据列表自动绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆