如何解决在ggplot中的图表旁边显示总计和均值
我正在尝试将我的均值和总计放在条形图的左侧,如上例所示。 我在ggplot中创建了条形图。下面的图片和代码。关于如何获取均值和总计以正确显示位置的任何建议?可能是custom_annotations?
percentData = stotal %>% #stotal = survey data frame
group_by(qtext,div,response) %>% #qtext is my question text
summarise(N = n()) %>%
mutate(prop = N/sum(N))
percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal
percentData
#colors
myColors <- c("green4","springgreen2","yellow1","orange1","red1","black","black")
ggplot(stotal)+
geom_bar(aes(x = div,fill = response),position = 'fill',width = 0.5)+
facet_grid(rows = vars(qtext))+
scale_fill_manual (values = myColors)+
coord_flip()+
ylab('')+
xlab('')+
scale_y_continuous(labels = percent)+
ggtitle(i)+
geom_text(data = percentData,aes(fill = response,y = N,label = prop,x = div),position=position_fill(vjust=0.5))+
theme(strip.text.y = element_text(size = 12,angle = 0,family = "serif"))
解决方法
一种实现此目的的方法是通过第二个ggplot制作表格,该表格可以通过例如patchwork
。基本上,表格图只复制了一个类别的主图,使用分面来获得带有均值和总数的列布局,并摆脱了轴,网格,背景色,...
使用一些随机示例数据尝试以下操作:
library(ggplot2)
library(dplyr)
library(scales)
library(patchwork)
# Random example data
set.seed(42)
stotal <- data.frame(
qtext = rep(c("A","B"),50),div = sample(c("University","KSAS-HUM"),100,replace = TRUE),response = sample(c("Poor","Fair","Good","Very good","Excellent"),replace = TRUE)
)
stotal$response <- factor(stotal$response,levels = c("Poor","Excellent"))
percentData = stotal %>% #stotal = survey data frame
group_by(qtext,div,response) %>% #qtext is my question text
summarise(N = n()) %>%
mutate(prop = N/sum(N))
#> `summarise()` regrouping output by 'qtext','div' (override with `.groups` argument)
percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal
#colors
myColors <- c("green4","springgreen2","yellow1","orange1","red1","black","black")
p1 <- ggplot(stotal)+
geom_bar(aes(x = div,fill = response),position = 'fill',width = 0.5)+
facet_grid(rows = vars(qtext))+
scale_fill_manual (values = myColors)+
coord_flip()+
ylab('')+
xlab('')+
scale_y_continuous(labels = percent)+
#ggtitle(i)+
geom_text(data = percentData,aes(fill = response,y = N,label = prop,x = div),position=position_fill(vjust=0.5))+
theme(strip.text.y = element_text(size = 12,angle = 0,family = "serif"))
#> Warning: Ignoring unknown aesthetics: fill
# Table plot
table_data <- stotal %>%
mutate(response = as.numeric(response)) %>%
group_by(qtext,div) %>%
summarise(Mean = mean(response),"Total N" = n()) %>%
mutate(Mean = round(Mean,1)) %>%
tidyr::pivot_longer(-c(qtext,div),names_to = "var")
#> `summarise()` regrouping output by 'qtext' (override with `.groups` argument)
p2 <- ggplot(table_data,aes(x = div)) +
geom_bar(color = "white",fill = "white",width = .5)+
#geom_vline(color = "grey",xintercept = c(.5,1.5,2.5)) +
geom_text(aes(y = 1,label = value),position=position_fill(vjust=0.5),size = 0.8 * 11 /.pt) +
facet_grid(qtext ~ var,switch = "y") +
coord_flip() +
labs(x = NULL,y = NULL) +
theme_minimal() +
theme(strip.text.y = element_blank()) +
theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(),axis.text.x = element_text(color = "transparent"),axis.ticks.x = element_line(color = "transparent"),axis.title = element_blank(),panel.grid = element_blank(),panel.spacing.x = unit(0,"pt"))
# Glue together
p2 + p1 + plot_layout(widths = c(1,3))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。