I am trying to generate a violin plot for a given dataset. The data varies from 0 to 100. But the violin plot goes over 100 and goes below 0.
How can I limit it between 0 to 100?
Code Used:
library(ggplot2)
input_data <- read.csv("C:/Temp/Recall.csv")
input_data
precision_in_percentage <- input_data$ResultValue
# Basic violin plot
p <- ggplot(input_data, aes(x='TRAssociation', y=precision_in_percentage)) +
geom_violin(trim=FALSE) + geom_violin(trim=FALSE, fill='#A4A4A4', color="darkred")+
geom_boxplot(width=0.05) + theme_minimal()
p
Documentation for that function can be found here. It looks like trim=TRUE might be used for this.
Hope this helps!
Checking the documentation for
geom_violin()
, it looks like you should remove thetrim=False
specification. By default, ggplot2 setstrim = True
.From ggplot2 docs:
Note that if you like the shape of the geom_violin() and just want to restrict the y-axis boundaries, you can do that by adding
+ ylim(0, 100)
to your plotting function calls.Also, note that there is a boxplot tool that plays better with violin plots available via the
stat_summary()
function. Try removing your call togeom_boxplot()
and instead using this (you may want to play with theshape
andsize
parameters:+ stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5)