Reputation: 370
I am creating violin plot using ggplot in R. I want to create violin plot showing different number count in each plot like this.
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)
Upvotes: 1
Views: 2903
Reputation: 1321
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)
Upvotes: 1