调整 x 轴上小提琴图之间的间距

问题描述 投票:0回答:1

我有 x 轴上带有非数字变量的小提琴图。由于分布原因,有些小提琴图非常宽,有些则非常窄。为了避免图之间的重叠,我缩小了图的宽度。然而,这一次本来就很狭窄的情节看起来很难理解。我想增加图之间的空间以避免重叠而不缩小图。我怎样才能做到这一点?或者您可以建议其他选择吗?

这是我在 R 中使用的代码和结果:

ggplot(dataviolin, aes(x=ROI, y=Values, fill=ROI) +
  geom_violin(width=1) + geom_boxplot(width=0.08)

outcome

r ggplot2 violin-plot
1个回答
0
投票

您可以手动设置

ROI
的位置,例如

dataviolin = data.frame(ROI=as.factor(rep(LETTERS[C(1:6)], each=100)),
                        Values=c(rnorm(100)/13, rnorm(100)/11,
                                 rnorm(100)/12, rnorm(100)/1,
                                 rnorm(100)/1, rnorm(100)/1))
pos <- c(1,4,7,9,10,11)  # note irregular spacing
wider <- 3
dataviolin$x <- pos[as.numeric(dataviolin$ROI)]
ggplot(dataviolin, aes(x=x, y=Values, fill=ROI)) +
  geom_violin(width=wider*1) + geom_boxplot(width=wider*0.08) +
  scale_x_continuous(labels=levels(dataviolin$ROI), breaks=pos) +
  theme(legend.position="none")

plot for the geom_violin with manually adjusted spacing

ggplot2
正在对变量进行一些猜测,因此如果您更改为水平箱线图,您可能需要传递
orientation

© www.soinside.com 2019 - 2024. All rights reserved.