I am using the following in R to generate a Boxplot out of a given set of data:
我在R中使用以下内容从给定的数据集生成Boxplot:
ggplot(data = daten, aes(x=Bodentyp, y=Fracht)) + geom_boxplot(aes(fill=Bewirtschaftungsform))
Now I want to display the number of data points going into each category of the column "Bodentyp". How do I achieve this?
现在我想显示进入“Bodentyp”列的每个类别的数据点数。我该如何实现这一目标?
1 个解决方案
#1
3
You can use fun.data
to apply a function (f
) to the grouped data to return a count (length(y)
) and a position for the label (median(y)
)
您可以使用fun.datato将函数(f)应用于分组数据以返回计数(长度(y))和标签的位置(中位数(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")
#1
3
You can use fun.data
to apply a function (f
) to the grouped data to return a count (length(y)
) and a position for the label (median(y)
)
您可以使用fun.datato将函数(f)应用于分组数据以返回计数(长度(y))和标签的位置(中位数(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")