在 ggplot facet_grid 中创建小计方面
Create subtotal facets in ggplot facet_grid
是否有一种优雅的方法可以在应用于分组聚合的 facet_grid(var1~var2)
图上添加小计方面,其中聚合类似于 sum
、mean
、sd
, 等等
"total" 方面应该使用与其他方面相同的聚合。
下图给出了使用 mean
的示例。共有 6 个小计和 1 个总分面。每个总方面提供每个碳水化合物值的平均 mpg。
我下面的方法笨拙而冗长。一种被推广来处理一般数量的 x 和 y faceting/grouping 变量的方法是可取的。我可以编写一个函数,根据聚合中的分组进行大量不雅的条件计算,但我真的在寻找方便和简单的东西。首选 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)
您想在 facet_grid()
函数中使用 margins
选项。请参阅以下内容:
p2 <- ggplot(aggregate_data, aes(x=1, y=value))+
geom_point()+
facet_grid(dist~scale, margins = TRUE)
p2
ggplot
可以直接从原始数据框做。但是 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)
它允许您按任何 facet_grid 组合对聚合进行分组。例如。
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)
是否有一种优雅的方法可以在应用于分组聚合的 facet_grid(var1~var2)
图上添加小计方面,其中聚合类似于 sum
、mean
、sd
, 等等
"total" 方面应该使用与其他方面相同的聚合。
下图给出了使用 mean
的示例。共有 6 个小计和 1 个总分面。每个总方面提供每个碳水化合物值的平均 mpg。
我下面的方法笨拙而冗长。一种被推广来处理一般数量的 x 和 y faceting/grouping 变量的方法是可取的。我可以编写一个函数,根据聚合中的分组进行大量不雅的条件计算,但我真的在寻找方便和简单的东西。首选 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)
您想在 facet_grid()
函数中使用 margins
选项。请参阅以下内容:
p2 <- ggplot(aggregate_data, aes(x=1, y=value))+
geom_point()+
facet_grid(dist~scale, margins = TRUE)
p2
ggplot
可以直接从原始数据框做。但是 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)
它允许您按任何 facet_grid 组合对聚合进行分组。例如。
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)