我有这个数据框
set.seed(1234)
x <- rnorm(80, 5, 1)
df <- data.frame(groups = c(rep("group1",20),
rep("group2",20),
rep("group3",20),
rep("group4",20)),
value = x,
type = c(rep("A", 10),
rep("B", 10),
rep("A", 10),
rep("B", 10),
rep("A", 10),
rep("B", 10),
rep("A", 10),
rep("B", 10)))
我想将其绘制为 fiddle 图,与狭窄的盒子图对齐并按“类型”分组:ggplot(data = df, aes(x = groups, y = value, fill = type)) +
geom_violin()+
geom_boxplot(width = 0.1, outlier.colour = NA)
但是,箱形图与 fiddle 图不对齐。告诉ggplot进行此类覆盖的缺少参数是什么?谢谢!
最佳答案
您需要为两个width
显式设置躲避的geom
:
dodge <- position_dodge(width = 0.4)
ggplot(data = df, aes(x = groups, y = value, fill = type)) +
geom_violin(position = dodge)+
geom_boxplot(width=.1, outlier.colour=NA, position = dodge)
关于r - 将 fiddle 图与闪避的箱形图对齐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27012500/