在 r 中绘制数据点数

plot number of data points in r

我在 R 中使用以下内容根据给定的数据集生成箱线图:

ggplot(data = daten, aes(x=Bodentyp, y=Fracht)) + geom_boxplot(aes(fill=Bewirtschaftungsform))

现在我想显示进入列 "Bodentyp" 的每个类别的数据点数。我该如何实现?

您可以使用 fun.data 将函数 (f) 应用于分组数据以 return 计数 (length(y)) 和标签位置 ( median(y))

f <- function(y) 
    c(label=length(y), y=median(y))

library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) +
  geom_boxplot() + theme_bw() +
  stat_summary(fun.data=f, geom="text", vjust=-0.5, col="blue")