我正在尝试为给定的数据集生成小提琴图.数据从0到100不等.但小提琴图超过100而低于0.
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.
如何将其限制在0到100之间?
How can I limit it between 0 to 100?
使用的代码:
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小提琴图:
推荐答案检查geom_violin()的文档,看来您应该删除trim=False规范.默认情况下,ggplot2设置trim = True.
Checking the documentation for geom_violin(), it looks like you should remove the trim=False specification. By default, ggplot2 sets trim = True.
来自ggplot2 文档:
From ggplot2 docs:
trim:如果为TRUE(默认值),则将小提琴的尾部修整到该范围 的数据.如果 假,不要修剪尾巴.
trim: If TRUE (default), trim the tails of the violins to the range of the data. If FALSE, don't trim the tails.
请注意,如果您喜欢geom_violin()的形状,并且只想限制y轴边界,则可以通过在绘图函数调用中添加+ ylim(0, 100)来实现.
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.
此外,请注意,有一个箱线图工具可以更好地与通过stat_summary()功能提供的小提琴图一起演奏.尝试删除对geom_boxplot()的呼叫,而改用此呼叫(您可能想使用shape和size参数:
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)
更多推荐
小提琴图未按预期工作
发布评论