1
votes

I am creating violin plot using ggplot in R. I want to create violin plot showing different number count in each plot like this. enter image description here

I used this code to create plot like this.

data<-read.csv("Clinical violion file.csv")
mat <- reshape2::melt(data.frame(data), id.vars = NULL)
pp <- ggplot(mat, aes(x = variable, y = value)) + geom_violin(scale="width",adjust = 1,width = 0.5,aes(color=factor(variable)))+ geom_point() 
pp

I have got plot like this.

But I don't know how to add point showing different number count in each plot. Here is header of my file.

I solved this issue by doing this.

 library(ggbeeswarm)
pp <- ggplot(mat, aes(x = variable, y = value)) + geom_violin(scale="width",adjust = 1,width = 0.5,aes(color=factor(variable)))+geom_quasirandom(aes(color=factor(variable)),groupOnX=FALSE)
1
try using ggbeeswarm::geom_quasirandom() vs geom_point() to get jittering to enable showing the cases where points overlap. You can use geom_text() to position total count of observations in each group on the plot or modify the factor labels to add them there.hrbrmstr
Please provide your data in a reproducible format. See for example here: stackoverflow.com/a/5963610/5892059kath

1 Answers

0
votes

This should do what you want:

library(ggplot2)

head(iris)

iris %>%  group_by(Species) %>% summarise(n=n(), avg = mean(Sepal.Length)) ->Summary.data


ggplot(iris,aes(x=Species,y=Sepal.Length)) + geom_violin(trim = T) +
  geom_jitter(height = 0, width = 0.1) +
  geom_text(data=Summary.data ,aes(x = Species, y = avg, label=n),color="red", fontface =2, size = 5)

Result