本文介绍了如何将小提琴剧情与箱柜布置在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有这个数据框 set.seed(1234)x< - rnorm(80,5 ,1) df 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设置闪避的宽度 : 闪避 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 frameset.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 geoms: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) 这篇关于如何将小提琴剧情与箱柜布置在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 11:06