有没有办法在ggplot的小提琴图中添加每个值的观察次数标签?

Is there a way for adding labels with number of observations per value in violin plot in ggplot?

您想创建小提琴图并具有如下数据的图像:

set.seed(123)
Bodytype <- sample(LETTERS[1:3], 500, replace = T)
Weight <- rnorm(500,40,1)    
df <- data.frame(Bodytype, Weight)
  ggplot(data = df, 
         aes(x = Bodytype, y = Weight, fill = Bodytype)) +
  geom_violin(scale = "count", trim = F, adjust = 0.75) +
  scale_y_continuous(breaks = seq(34, 46, 1)) +
  theme_gray() 

现在我想在每个公斤​​级别为每个体型添加文本标签或其他内容,以查看每个体型类别中体重为 36 公斤、37 公斤等的观察值有多少。有没有办法实现这一点,或者我我最好使用另一个地块?

这可以通过多种方式完成,这里是一个:

library(dplyr)
library(ggplot2)
summ <- df %>%
  group_by(Bodytype) %>%
  summarize(n = n(), Weight = mean(Weight))
ggplot(data = df, aes(x = Bodytype, y = Weight, fill = Bodytype)) +
  geom_violin(scale = "count", trim = F, adjust = 0.75) +
  scale_y_continuous(breaks = seq(34, 46, 1)) +
  theme_gray() +
  geom_text(aes(label = n), data = summ)


好的,所以你想要多个重量计数:

weightcounts <- df %>%
  mutate(Weight = as.integer(round(Weight, 0))) %>%
  group_by(Bodytype, Weight) %>%
  count()
ggplot(data = df, aes(x = Bodytype, y = Weight, fill = Bodytype)) +
  geom_violin(scale = "count", trim = F, adjust = 0.75) +
  scale_y_continuous(breaks = seq(34, 46, 1)) +
  theme_gray() +
  geom_text(aes(label = n), data = weightcounts)

无论哪种方式,前提是你可以用你需要的关联标签生成一个摘要框架,然后用新数据集作为参数添加geom_text(或geom_label)。

base R之外计算标签的另一种方法:

set.seed(123)
Bodytype <- sample(LETTERS[1:3], 500, replace = T)
Weight <- rnorm(500,40,1)    
df <- data.frame(Bodytype, Weight)
#Labels
df$i <- 1
labs <- aggregate(i~Bodytype,df,sum)
labs$Weight<-NA
#Plot
ggplot(data = df, 
       aes(x = Bodytype, y = Weight, fill = Bodytype)) +
  geom_violin(scale = "count", trim = F, adjust = 0.75) +
  geom_text(data=labs,aes(x=Bodytype,y=45,label=i))
  scale_y_continuous(breaks = seq(34, 46, 1)) +
  theme_gray() 

输出: