gpt4 book ai didi

r - 在 ggplot 图表旁边显示总计和均值

转载 作者:行者123 更新时间:2023-12-04 08:44:34 25 4
gpt4 key购买 nike

enter image description here
我试图将我的平均值和总数放在条形图的左侧,就像上面的例子一样。
我在 ggplot 中创建了条形图。图片和代码如下。关于如何将平均值和总数显示在正确位置的任何建议?可能是 custom_annotations?
谢谢
enter image description here

  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", "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"), 100, replace = TRUE)
)
stotal$response <- factor(stotal$response, levels = c("Poor", "Fair", "Good", "Very good", "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", "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", position = 'fill', 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))

关于r - 在 ggplot 图表旁边显示总计和均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64393525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com