gpt4 book ai didi

r - ggplot : geom_text does not center-align above geom_col()

转载 作者:行者123 更新时间:2023-12-04 08:51:16 26 4
gpt4 key购买 nike

请找我的资料p以下。我必须包含 100 个样本才能重现错误。
问题:为什么是 geom_text打印始终居中对齐 geom_col - 例如 21 28 All在右边 SSA -方面?我试过调整 position.dodge2vjust ,但这没有用。
This thread解决了这个问题,但没有解决我的问题。
enter image description here
我的剧本

  ggplot(p %>%
mutate(nystudie=as.character(study),
best.resp =as.factor(response)) %>%
group_by(nystudie,best.resp) %>%
summarise(N=n(),Val=unique(treatment)) %>%
bind_rows(p %>% filter(response %in% 1:4, treatment!="Control") %>% droplevels() %>%
mutate(nystudie=as.character(study),
best.resp =as.factor(response)) %>%
group_by(best.resp,treatment) %>% summarise(N=n()) %>%
mutate(nystudie="All") %>%
rename(Val=treatment)),
aes(nystudie, N, color = best.resp, fill= best.resp)) +
geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
facet_wrap(~Val,ncol = 2, scales="free") +
scale_fill_grey(name="") +
scale_color_grey(name="") +
scale_y_continuous(breaks = seq(0,120,20)) +
geom_text(aes(label=N),position = position_dodge2(.5), vjust=0, fontface=2, cex=4.5, show.legend = F) +
theme(strip.background = element_blank(),
strip.text = element_text(color = "black", size = 15),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
plot.margin = unit(c(1,3,1,1), "lines"))
数据
p <- structure(list(study = structure(c(8L, 12L, 12L, 12L, 4L, 4L, 
1L, 11L, 11L, 13L, 1L, 13L, 14L, 9L, 9L, 10L, 12L, 11L, 4L, 11L,
11L, 12L, 8L, 11L, 13L, 11L, 6L, 15L, 6L, 4L, 7L, 13L, 11L, 4L,
1L, 6L, 1L, 11L, 16L, 1L, 10L, 15L, 1L, 11L, 1L, 6L, 1L, 11L,
12L, 11L, 13L, 16L, 1L, 8L, 11L, 10L, 4L, 4L, 12L, 10L, 6L, 15L,
12L, 14L, 12L, 1L, 1L, 16L, 12L, 12L, 8L, 7L, 1L, 1L, 13L, 13L,
14L, 9L, 14L, 2L, 11L, 4L, 1L, 16L, 15L, 11L, 9L, 4L, 13L, 12L,
6L, 16L, 4L, 1L, 15L, 6L, 4L, 1L, 9L, 2L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "22"), class = "factor"), response = c("1", "3", "4", "4",
"3", "3", "3", "4", "4", "4", "4", "4", "3", "4", "4", "4", "3",
"4", "4", "4", "4", "3", "1", "4", "4", "4", "3", "4", "3", "3",
"4", "4", "4", "3", "4", "4", "4", "4", "4", "3", "4", "4", "3",
"4", "4", "3", "3", "4", "3", "4", "4", "4", "4", "3", "3", "4",
"4", "3", "3", "4", "3", "4", "4", "4", "3", "3", "4", "4", "4",
"4", "2", "4", "4", "4", "4", "4", "3", "4", "3", "3", "4", "4",
"4", "4", "4", "4", "3", "3", "4", "4", "3", "4", "4", "4", "4",
"3", "3", "4", "2", "3"), treatment = structure(c(2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L), .Label = c("SSTR", "SSA"), class = "factor")), row.names = c(NA,
-100L), class = "data.frame")

最佳答案

添加标签时,您必须注意使用与 geom_col 相同的定位。 .要将标签与条形对齐,请使用 position_dodge2(preserve = "single", width = .9, padding = 0.1) :

library(ggplot2)
library(dplyr)

d1 <- p %>%
mutate(
nystudie = as.character(study),
best.resp = as.factor(response)
) %>%
group_by(nystudie, best.resp) %>%
summarise(N = n(), Val = unique(treatment))
#> `summarise()` regrouping output by 'nystudie' (override with `.groups` argument)

d2 <- p %>%
filter(response %in% 1:4, treatment != "Control") %>%
droplevels() %>%
mutate(
nystudie = as.character(study),
best.resp = as.factor(response)
) %>%
group_by(best.resp, treatment) %>%
summarise(N = n()) %>%
mutate(nystudie = "All") %>%
rename(Val = treatment)
#> `summarise()` regrouping output by 'best.resp' (override with `.groups` argument)

d <- bind_rows(d1, d2)

ggplot(d, aes(nystudie, N, color = best.resp, fill = best.resp)) +
geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
facet_wrap(~Val, ncol = 2, scales = "free") +
scale_fill_grey(name = "") +
scale_color_grey(name = "") +
scale_y_continuous(breaks = seq(0, 120, 20)) +
geom_text(aes(label = N), position = position_dodge2(preserve = "single", width = .9, padding = 0.1), vjust = 0, fontface = 2, cex = 4.5, show.legend = F) +
theme(
strip.background = element_blank(),
strip.text = element_text(color = "black", size = 15),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
plot.margin = unit(c(1, 3, 1, 1), "lines")
)

关于r - ggplot : geom_text does not center-align above geom_col(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64091110/

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