小提琴图未按预期工作

Violin plot not working as expected

我正在尝试为给定的数据集生成小提琴图。数据从 0 到 100 不等。但小提琴图超过 100 并低于 0。

如何将它限制在 0 到 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

小提琴情节:

Documentation 可在此处找到该函数。看起来 trim=TRUE 可能用于此目的。

If TRUE (default), trim the tails of the violins to the range of the data. If FALSE, don't trim the tails.

希望对您有所帮助!

查看 geom_violin() 的文档,您似乎应该删除 trim=False 规范。默认情况下,ggplot2 设置 trim = True.

来自 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.

请注意,如果您喜欢 geom_violin() 的形状并且只想限制 y 轴边界,您可以通过在绘图函数调用中添加 + ylim(0, 100) 来实现。

此外,请注意,通过 stat_summary() 函数可以使用箱线图工具更好地处理小提琴图。尝试删除对 geom_boxplot() 的调用并改为使用它(您可能想使用 shapesize 参数:

+ stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5)