简体   繁体   English

在ggplot facet_grid中创建小计

[英]Create subtotal facets in ggplot facet_grid

Is there are an elegant way to add subtotal facets on a facet_grid(var1~var2) plot applied to a grouped aggregation, where an aggregation is similar to a sum , mean , sd , etc. 是否有一种优雅的方法可以在应用于分组聚合的facet_grid(var1~var2)图上添加小计分面,其中聚合类似于summeansd等。

The "total" facet should use the same aggregation as the other facets. “总计”构面应使用与其他构面相同的聚合。

The plot below gives an example of using mean . 下图给出了使用mean的示例。 There are 6 subtotal and 1 total facets all up. 总共有6个小计和1个小面。 Each total facet provides the mean mpg at each carb value. 每个总方面都提供每个碳水化合物值的平均mpg。

具有总构面的构面

My approach below is clunky and verbose. 我下面的方法笨拙而冗长。 An approach that is generalized to handle a general number of both x and y faceting/grouping variables is desirable. 期望一种被通用化以处理x和y刻面/分组变量的总数的方法。 I could code up a function that does a whole lot of inelegant conditional calculations depending on the groupings in the aggregation, but I'm really looking for something convenient and simple. 我可以编写一个函数,该函数根据聚合中的分组进行很多不完善的条件计算,但是我实际上是在寻找方便和简单的方法。 Tidyverse solutions prefered. 首选Tidyverse解决方案。

library(dplyr)
library(ggplot2)
# use mtcars data set
data <- mtcars

# aggregate data by grouping variables
aggregate_data<- data%>%
  group_by(gear,cyl, carb)%>%
  summarize(mpg=mean(mpg))%>%
  ungroup

# get total for gear
data_tot_cyl<- data%>%
  group_by(cyl, carb)%>%
  summarize(mpg=mean(mpg))%>%
  ungroup%>%
  mutate(gear='total')

# get total for cyl
data_tot_gear<- data%>%
  group_by(gear, carb)%>%
  summarize(mpg=mean(mpg))%>%
  ungroup%>%
  mutate(cyl='total')

# get total for total-total
data_tot_tot<- data%>%
  group_by(carb)%>%
  summarize(mpg=mean(mpg))%>%
  ungroup%>%
  mutate(cyl='total', gear='total')

# get data frame with all total's data.
new_data<-data_tot_tot%>%
  bind_rows(data_tot_gear%>%mutate(gear=as.character(gear)))%>%
  bind_rows(data_tot_cyl%>%mutate(cyl=as.character(cyl)))%>%
  bind_rows(aggregate_data%>%mutate_at(vars(gear, cyl), funs(as.character)))

# Arghh, gotta order the levels so total is at the end.
new_data$cyl <- factor(new_data$cyl, 
                         levels=c('4','6','8','total'),ordered=T)
new_data$gear <- factor(new_data$gear, 
                        levels=c('3','4','5','total'),ordered=T)

# Finally after over 20 additional lines of code, I get the 
# faceted plot with totals for x and y facets. 
p<-ggplot(new_data, aes(x=carb, y=mpg))+
  geom_bar(stat='identity')+
  facet_grid(cyl~gear)+
  geom_text(aes(label=round(mpg,1), y=0), 
            col='white', size=3,hjust=-0.3, angle=90)+
  ggtitle('Average MPG vs Num Carbs, by Num Cylinders & Num Gears')
print(p)

you want to use the margins option in your facet_grid() function. 您想在facet_grid()函数中使用margins选项。 See the following: 请参阅以下内容:

p2 <- ggplot(aggregate_data, aes(x=1, y=value))+
  geom_point()+
  facet_grid(dist~scale, margins = TRUE)
p2

ggplot can do it directly from the original data frame. ggplot可以直接从原始数据帧执行此操作。 But the geom_text still displays the individual row values, rather than the aggregation. 但是geom_text仍显示单个行值,而不是聚合。

p3<-ggplot(mtcars, aes(x=carb, y=mpg))+
  stat_summary(fun.y="mean", geom="bar")+
  facet_grid(cyl~gear, margins=T)+
  geom_text(aes(label=round(..y..,1)), 
            col='red', size=3,hjust=-0.3, angle=90)+
  ggtitle('Average MPG vs Num Carbs, by Num Cylinders & Num Gears')
print(p3)

And it allows you to group the aggregation by any facet_grid combination. 并且它允许您按任何facet_grid组合对聚合进行分组。 Eg 例如

p4<-ggplot(mtcars, aes(x=carb, y=mpg))+
  stat_summary(fun.y="mean", geom="bar")+
  facet_grid(am+vs~gear, margins=T)+
  geom_text(aes(label=round(..y..,1)), 
            col='red', size=3,hjust=-0.3, angle=90)+
  ggtitle('Average MPG vs Num Carbs, by Num Gears and vs & am')
print(p4)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM