繁体   English   中英

ggplot2:geom_violin(),geom_text()与facet_grid()一起打印每个因素中不包括NA的总行数

[英]ggplot2: geom_violin(), geom_text() with facet_grid() to print total number of rows in each factor excluding NA

在下面的小提琴图中,我想添加用于绘制每个图的总行数,不包括NA值。

输入:

df <- cbindX(as.data.frame(rep(c(rep("trt", 4*500), rep("trt2",4*500)),2)), 
            as.data.frame(rnorm(15*500,2)), 
            as.data.frame(c(rep("A", 8*500), rep("B", 8*500))))
colnames(df) <- c("variable", "value", "mark")

码:

ggplot(df,aes(x=variable,y=value)) + geom_violin(trim = T) + geom_text(aes(x = variable, y = -2, label=nrow(df)),color="red")

输出: 在此处输入图片说明 预期产量: 在此处输入图片说明

这应该可以帮助您:

library(dplyr)
count<-df %>% filter(!is.na(value)) %>%
group_by(variable) %>%
summarise(n=n()) %>%
as.data.frame

#    variable    n
#  1      trt 4000
#  2     trt2 3500

ggplot(df,aes(x=variable,y=value)) + geom_violin(trim = T) +
geom_text(data=count,aes(x = variable, y = -2, label=n),color="red")

在此处输入图片说明

这个锻炼对你有用吗

ggplot(df,aes(x=variable,y=value)) + geom_violin(trim = T) + annotate("text", label = "4000", x =1, y = -3, size = 10, colour = "black") + annotate("text", label = "3500", x =2, y = -3, size = 10, colour = "black")

在此处输入图片说明

暂无
暂无

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

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