如何将小提琴剧情与箱柜布置在一起 [英] how to align violin plots with boxplots
本文介绍了如何将小提琴剧情与箱柜布置在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数据框
set.seed(1234)
x< - rnorm(80,5 ,1)
df < - data.frame(groups = c(rep(group1,20),
rep(group2,20),
rep(group3 ,
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)))
我想将它作为小提琴情节进行绘制,并与一个窄盒子情节对齐,并按照
分组变量类型:
ggplot(data = df,aes(x = groups,y = value,fill = type))+
geom_violin()+
geom_boxpl ot(width = .1,outlier.colour = NA)
然而,箱子图并没有对齐到大提琴剧情,告诉ggplot做这样的重叠是缺少的理由是什么?
谢谢!
解决方案
您需要明确地为 geom
s设置闪避的宽度
:
闪避< - position_dodge(宽度= 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)
I have this data frame
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)))
And I would like to plot it as violin plot, aligned with a narrow box plot and grouped by
the variable type:
ggplot(data = df, aes(x = groups, y = value, fill = type)) +
geom_violin()+
geom_boxplot(width=.1, outlier.colour=NA)
However, the box plots does not align to the violon plots, what is the missing argument to tell ggplot to do such overlying?
Thanks!
解决方案
You need to set the width
of the dodging explicitly for both geom
s:
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)
这篇关于如何将小提琴剧情与箱柜布置在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文