组合箱式图与小提琴图不对齐

3
我想用一个带有箱线图的小提琴图来绘制沿两个维度分布的图形。结果可以非常迷人,但只有在正确操作时才能实现。
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
plot <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_violin() + geom_boxplot(width=0.1)  + theme(legend.position="none")
ggsave(filename="Violinboxplot.png", plot, height=6, width=4)

这是我得到的结果:

enter image description here

箱线图沿着因子所属的轴对齐。我该如何将它们移动到小提琴图的中心位置?
1个回答

3
这个问题的答案在这里: 如何将小提琴图与箱线图对齐 您可以使用position参数根据需要移动图形元素:
dodge <- position_dodge(width = 0.5)

ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_violin(position = dodge) +
  geom_boxplot(width=.1, position = dodge) +
  theme(legend.position="none")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接