小提琴图未按预期工作 [英] Violin plot not working as expected

查看:156
本文介绍了小提琴图未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为给定的数据集生成小提琴图.数据从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()的呼叫,而改用此呼叫(您可能想使用shapesize参数:

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)

这篇关于小提琴图未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆