【问题标题】:Restructuring Data for Ggplot2 Combination Grouped and Stacked Barchart Using Tidyverse [closed]使用 Tidyverse 重构 Ggplot2 组合分组和堆叠条形图的数据 [关闭]
【发布时间】:2017-10-21 16:59:31
【问题描述】:
library(tidyverse)
library(ggplot2)

我正在尝试创建下面的条形图,但在重组数据时遇到了问题。我在下面提供了一些示例数据,我创建的速度很快,所以结果可能很奇怪,但我对如何使用 tidyverse 工具来设置数据更感兴趣。

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)

下面是我到目前为止开发的代码,但我被困在这一点上,不知道如何为彩色分组条合并 Q20 变量。我想使用 Tidyverse 和 ggplot2 来实现这一点。任何其他关于如何使我的代码更加优雅和紧凑的反馈也将不胜感激。

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

【问题讨论】:

  • PCodeCCode 未找到
  • 抱歉,我添加了它们。希望您可以再试一次...
  • Rating 未找到
  • Q20 是什么?您的情节中是否需要它?
  • 你在这里问了五件事,其中大部分可以单独完成。试着每个问题问一件事,否则我们只是为你编写代码。

标签: r ggplot2 tidyverse


【解决方案1】:

一般来说,ggplot 需要长格式的表格,而且您的数据似乎很宽。即,最终您的数据应如下所示:

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

这样您就可以使用aes(color=barColor, y=Y) 等致电ggplot
我想说tidyr::gather 应该负责大部分的重组,但也请参阅这个很棒的cheatsheet 以获取其他有用的工具。


编辑:堆叠+分组条形图的可能解决方案,不使用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"))

【讨论】:

  • 感谢您的指点。我添加了代码来显示到目前为止我已经尝试过的内容以及我遇到的问题。我已经将数据重组为看起来像您的示例,但我不确定如何进一步进行......
  • @Mike 那么现在的问题是制作一个包含堆栈和组的条形图?在 SO 上的快速搜索给出了this 解决方案,facet_wrap。如果您不喜欢单独的方面,您可以制作一个假 x 变量来将条形精确映射到您想要的位置(例如 1、4、7... 用于灰色条,2、5... 用于蓝色条, 3, 6... 用于空格)。修复刻度线文本应该很容易。没有实际数据就不能说更多了。
  • 您能提供我在帖子中提供的示例数据的代码吗?我提供了一些代码来展示我到目前为止所做的尝试......它非常接近。如果你能复制它并展示如何制作所附的图表,那么我可以给你功劳。到目前为止,我感谢您的帮助.....
  • @Mike 编辑详细说明了我所说的假 x 变量映射的含义。 facet_wrap 解决方案的代码在之前链接的答案中提供。
猜你喜欢
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多