使用Tidyverse重组Ggplot2组合数据。分组和条形图条形图 [英] Restructuring Data for Ggplot2 Combination Grouped and Stacked Barchart Using Tidyverse

查看:244
本文介绍了使用Tidyverse重组Ggplot2组合数据。分组和条形图条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(tidyverse)
library(ggplot2)

我试图在下面创建条形图,但我在重构数​​据时遇到了麻烦。我提供了一些示例数据,我在其中创建了一些快速的数据,所以结果可能很奇怪,但我更关心如何使用Tidyverse工具来设置数据。

  Q1_Sat <-c(星期六,星期六,星期六,其他,其他, (VSat),其他,其他,其他,其他,其他)
Q1_VSat <-c(VSat,其他,其他,VSat,VSat,VSat,VSat )
Q1_M <-c(SatVSat,SatVSat,SatVSat,SatVSat,其他,其他,SatVSat,SatVSat)
Q2_Sat < c(Sat,Other,Sat,Other,Sat,Sat,Other,Other)
Q2_VSat <-c(VSat ,VSat,其他,VSat,VSat,VSat,VSat)
Q2_M <-c(SatVSat,SatVSat,SatVSat,SatVSat, SatVSat,SatVSat,SatVSat,其他)
Q3_Sat <-c(星期六,其他,星期六,其他,星期六, (Sat,Sat)
Q3_VSat <-c(VSat,其他,VSat,其他,其他,其他,其他,VSat)
Q3_M <-c(SatVSat,SatVSat,SatVSat,其他,其他,其他,其他,其他)

Q4_Sat < - c(Sat,Other,Other,Other,Other,Other,Other,Other)
Q4_VSat <-c(VSat ,VSat,VSat,VSat,VSat,VSat,VSat)
Q4_M <-c(SatVSat,其他,其他,其他, 其他, 其他, SatVSat,S atatat)

Q20 <-c(Nat,Internat,Nat,Nat,Internat,Internat,Nat,Nat)
Calc_Sat <-c(Sat,Sat,Sat,Other,Other,Other,Sat,Sat)
Calc_VSat< -c(Other ,其他,VSat,VSat,VSat,VSat,其他,VSat)
PCode <-c(C11,C11,H12 ,F33,F33,C11,S33,F33)
CCode <-c(Dept,Camit,Camit CCT,TTT,CCT)
Data_data_frame(Q1_Sat,Q1_VSat,Q1_M,Q2_Sat,Q2_VSat,Q2_M,Q3_Sat,Q3_VSat,Q3_M,Q4_Sat,Q4_VSat,Q4_M,Q20,PCode,CCode ,Calc_Sat,Calc_VSat)

以下是我迄今为止开发的代码,这一点,并不知道如何纳入彩色分组酒吧Q20变量。我想用Tidyverse和ggplot2来实现这一点。关于如何使我的代码更优雅和紧凑的任何其他反馈也将不胜感激。

 数据%>%
select(-CCode,-Q1_M,-Q2_M,-Q3_M,-Q4_M) %>%
collect(key,value,-PCode,-Q20)%>%
filter(PCode ==C11)%>%
count(Q20,key ,value)%>%
mutate(perc = round(n / sum(n),2))%>%
separate(key,c(Question,SatLevel (aes(x = Question,y = perc,fill = SatLevel))+ geom_col(%=
filter(value!=Other)%>%
ggplot ()

$ b $一般来说, ggplot 需要长格式的表格,而且您的数据看起来很宽。也就是说,最后你的数据应该是这样的:

  Q bar颜色barShade Y 
1灰色光55
1灰暗20
1蓝色光57
1蓝色黑色21
2灰色光23
...

,这样你可以用 aes(color = barColor,y = c)调用 ggplot Y)等。

我会说 tidyr :: gather 应该关注大部分重组,但请参阅对于其他


library(tidyverse)
library(ggplot2)

I am attempting to create the bar chart below but I'm having trouble restructuring the data. I provided some sample data below which I created kind of fast, so the results may be strange, but I'm more interested in how to use tidyverse tools to set up the data.

Q1_Sat<-c("Sat","Sat","Sat","Other","Other","Other","Other","Other")
Q1_VSat<-c("VSat","Other","Other","VSat","VSat","VSat","VSat","VSat")
Q1_M<-c("SatVSat","SatVSat","SatVSat","SatVSat","Other","Other","SatVSat","SatVSat")
Q2_Sat<-c("Sat","Other","Sat","Other","Sat","Sat","Other","Other")
Q2_VSat<-c("VSat","Other","VSat","Other","VSat","VSat","VSat","VSat")
Q2_M<-c("SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","SatVSat","Other")
Q3_Sat<-c("Sat","Other","Sat","Other","Sat","Sat","Sat","Sat")
Q3_VSat<-c("VSat","Other","VSat","Other","Other","Other","Other","VSat")
Q3_M<-c ("SatVSat","SatVSat","SatVSat","Other","Other","Other","Other","Other")

Q4_Sat<-c("Sat","Other","Other","Other","Other","Other","Other","Other")
Q4_VSat<-c("VSat","VSat","VSat","VSat","VSat","VSat","VSat","VSat")
Q4_M<-c("SatVSat","Other","Other","Other","Other","Other","SatVSat","SatVSat")

Q20<-c("Nat","Internat","Nat","Nat","Internat","Internat","Nat","Nat")
Calc_Sat<-c("Sat","Sat","Sat","Other","Other","Other","Sat","Sat")
Calc_VSat<-c("Other","Other","VSat","VSat","VSat","VSat","Other","VSat")
PCode<-c("C11","C11","H12","F33","F33","C11","S33","F33")
CCode<-c("Dept","Camit","Camit","CCT","Dept","CCT","TTT","CCT")
Data<-data_frame(Q1_Sat,Q1_VSat,Q1_M,Q2_Sat,Q2_VSat,Q2_M,Q3_Sat,Q3_VSat,Q3_M,Q4_Sat,Q4_VSat,Q4_M,Q20,PCode,CCode,Calc_Sat,Calc_VSat)

Below is the code I've developed so far but I'm stuck at this point and not sure how to incorporate the Q20 variable for the coloured grouped bars. I would like to use Tidyverse and ggplot2 to achieve this. Any other feedback about how to make my code more elegant and compact would also be greatly appreciated.

Data%>%
select(-CCode,-Q1_M,-Q2_M,-Q3_M,-Q4_M)%>%
gather(key,value,-PCode,-Q20)%>%
filter(PCode=="C11")%>%
count(Q20,key,value)%>%
mutate(perc=round(n/sum(n),2))%>%
separate(key,c("Question","SatLevel"),sep="_")%>%
filter(value != "Other")%>%
ggplot(aes(x=Question,y=perc,fill=SatLevel))+geom_col()

解决方案

Generally, ggplot requires long-format tables, and your data seems to be wide. I.e., in the end your data should look something like:

Q barColor barShade Y
1 grey light 55
1 grey dark 20
1 blue light 57
1 blue dark 21
2 grey light 23
...

so that you could call ggplot with aes(color=barColor, y=Y) etc.
I'd say tidyr::gather should take care of most of the restructuring, but see also this great cheatsheet for other helpful tools.


Edit: possible solution for stacked+grouped barplots, without using facet_wrap:

df = Data%>%
    select(-CCode,-Q1_M,-Q2_M,-Q3_M,-Q4_M)%>%
    gather(key,value,-PCode,-Q20)%>%
    filter(PCode=="C11")%>%
    count(Q20,key,value)%>%
    mutate(perc=round(n/sum(n),2))%>%
    separate(key,c("Question","SatLevel"),sep="_")%>%
    filter(value != "Other") df$Question = c(14, 14, 1, 1, 4, 4, 7, 10,
                15, 2, 2, 5, 5, 8, 8, 11, 11)

ggplot(df, aes(x=Question,y=perc,fill=SatLevel)) + geom_col() +
    theme_bw() +
    scale_x_continuous(breaks=c(1.5, 4.5, 7.5, 10.5, 14.5),
                       labels=c("Q1", "Q2", "Q3", "Q4", "Calc"))

这篇关于使用Tidyverse重组Ggplot2组合数据。分组和条形图条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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