问题
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
Violin Plot:
回答1:
Checking the documentation for geom_violin()
, it looks like you should remove the trim=False
specification. By default, ggplot2 sets trim = True
.
From ggplot2 docs:
trim: If TRUE (default), trim the tails of the violins to the range of the data. If FALSE, don't trim the tails.
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 to geom_boxplot()
and instead using this (you may want to play with the shape
and size
parameters:
+ stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5)
回答2:
Documentation for that function can be found here. It looks like trim=TRUE might be used for this.
If TRUE (default), trim the tails of the violins to the range of the data. If FALSE, don't trim the tails.
Hope this helps!
来源:https://stackoverflow.com/questions/45108995/violin-plot-not-working-as-expected