简体   繁体   English

r中数据点的绘图数量

[英]plot number of data points in r

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". 现在,我要显示进入“ Bodentyp”列的每个类别的数据点的数量。 How do I achieve this? 我该如何实现?

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.data将函数( f )应用于分组数据,以返回计数( 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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM