r - 提供小提琴图的样本量
问题描述
使用以下代码,我为我的大多数变量制作了小提琴图,并在我没有足够信息的某些数据处添加了点。 我想在每把小提琴的右端添加样本大小,但我一直无法找到这样做的方法。
#dataset
str(threats)
'data.frame': 60 obs. of 3 variables:
$ threat : Factor w/ 7 levels "weather","competition",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Species : Factor w/ 5 levels "Bank","Barn",..: 1 1 1 1 1 1 1 1 1 1 ...
$ effect.abs : int 18 13 0 43 43 0 23 13 14 16 ...
#added to help 0 values with logarithmic axis scale
threats$effect.abs1<-threats$effect.abs+0.1
#subset of data with insufficient info for violin plot
#plotted with geom_dotplot
threats.sub<-subset(threats,
(threat=="competition") |
(threat=="disease" & Species =="Barn") |
(threat=="insect_availability") |
(threat=="weather" &
(Species=="Cliff" | Species=="Purple")) |
(threat=="incidental_loss") |
(threat=="predation" & Species=="Bank"))
ggplot() +
geom_dotplot(data=threats.sub, aes(x=Species, y=effect.abs1, fill=Species),
binaxis='y', stackdir='center', binwidth =.1) +
geom_violin(data=threats, aes(x=Species, y=effect.abs1, fill=Species)) +
coord_flip() +
facet_wrap(~threat, ncol=2, labeller = labeller(threat=facet.labels),
strip.position = "left") +
scale_y_log10(breaks=c(0.1,1,10,100), labels=c(0,1,10,100)) +
labs(x=("Threat"), y=("Absolute effect on adult survival (%)")) +
theme_bw() +
theme(axis.text=element_text(size=9, colour="black"),
axis.title=element_text(size=10, colour="black"),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid=element_blank(),
panel.border=element_rect(colour="black", size=1),
plot.margin=unit(c(.3,.3,.4,.4), "cm"),
strip.background=element_rect(fill=NA, colour=NA), #element_blank(),
legend.position="right")
我尝试使用下面的解决方案(在其他问题中提供),只导致出现错误消息。
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
stat_summary(fun.data = give.n, geom = "text") #added to ggplot code above
Error in if (empty(data)) { : missing value where TRUE/FALSE needed
我将不胜感激有关此问题的任何帮助。我更愿意为 R 找到一种方法来计算样本量(而不是我提供每个样本量),因为当我生成这个数字时,我也会不断收到以下警告消息,我想仔细检查所有的数据显示正确。
Warning messages:
1: In max(data$density) : no non-missing arguments to max; returning -Inf
2: In max(data$density) : no non-missing arguments to max; returning -Inf
3: In max(data$density) : no non-missing arguments to max; returning -Inf
谢谢!
按照要求:
structure(list(threat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L), .Label = c("weather", "competition", "incidental_loss",
"contaminants", "insect_availability", "disease", "predation"
), class = "factor"),
Species = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L,
5L, 5L, 1L, 2L, 2L, 2L, 2L), .Label = c("Bank", "Barn", "Cliff",
"Tree", "Purple"), class = "factor"),
effect.abs = c(18L,
13L, 0L, 43L, 43L, 0L, 23L, 13L, 14L, 16L, 18L, 29L, 0L, 40L,
0L, 20L, 53L, 0L, 17L, 15L, 13L, 25L, 19L, 25L, 0L, 0L, 0L, 14L,
20L, 0L, 0L, 0L, 0L, 4L, 1L, 0L, 1L, 1L, 1L, 1L, 12L, 0L, 30L,
95L, 10L, 3L, 7L, 12L, 14L, 100L, 0L, 23L, 13L, 5L, 0L, 58L,
20L, 4L, 9L, 0L)), row.names = c(NA, -60L), class = "data.frame")
解决方案
解决这个问题的方法是预先计算你的 n 的 EG
summary_df <- df %>%
group_by(threat, Species, effect.abs1) %>%
summarise(n = n())
然后将其添加到您的图表中
+ geom_label(aes(x = 100, y = effect.abs1, label = n), data = summary_df)
推荐阅读
- flutter - Flutter - 无法在远程服务器上使用 HTTPS 调用 API
- vba - 根据电子邮件地址移动超过 20,000 封电子邮件会冻结 Outlook
- next.js - 使用 Nextjs 在 Netlify 上部署 Storybook
- css - 如何在 Next.js 应用程序中将输入框与图像对齐
- c++ - 在自定义链表类型的结构中实现自定义迭代器
- python - IIS 上的 Flask 应用程序 - 代表域用户更新 Active Directory 属性
- mysql - MySQL 值依赖于不同的表
- scala - 在 Scala 中转换 spark 数据帧的模式
- r - 试图在 ggplot 上实现 expss 包中的 use_labels
- python - 如何从 perlin-noise 获得 2D 瓦片输出?