【发布时间】:2026-01-26 01:10:01
【问题描述】:
我正在尝试将样本大小标签添加到 ggplot 中的箱线图。我拥有我认为需要的一切,但我不确定如何微调标签的位置。我尝试了 position_nudge 和 position_jitter,但我希望能够单独调整每个标签,这样我就不必担心标签会跑到平均值栏中。
我的数据代码给了我这个
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
ggplot(ratings_county_2019_DI, aes(x=county, y=di))+
geom_boxplot(aes(fill = county), fatten = 1.25)+
scale_fill_manual(values = col19, aesthetics = c("colour", "fill"))+
labs(x = "County", y = " DSI (%)")+
theme_minimal()+
theme(legend.position = "top")+
stat_summary(fun.data = give.n, geom = "text")+
ggtitle("Disease Serverity Index 2019 Survey")+
theme(plot.title = element_text(hjust = 0.5))
【问题讨论】:
-
使用 R 代码进行数据构建的简单示例。那么可能有一种方法可以访问绘图对象中的数据。
标签: r ggplot2 position boxplot