0

In the violin plot below, I want to add total number of rows used to draw each plot excluding NA values.

Input:

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")

code:

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

Output: enter image description here Expected output: enter image description here

Rashedul Islam
  • 879
  • 1
  • 10
  • 23

2 Answers2

4

This should help you:

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")

enter image description here

Maju116
  • 1,607
  • 1
  • 15
  • 30
1

would this workout for you

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")

enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56