我试图在一个图表中的六个箱图中绘制一组数据(len),其中包含三组(mea,tre和sex)。数据首先被分解为“mea”组,然后进一步细分为“tre”,最后再分为“sex”。
StackOverflow上有很多类似的问题(请参阅Plot multiple boxplot in one graph或How to display two groups of boxplots?,但我似乎无法在我的数据集上复制任何解决方案。
可能我最接近的是:
dataSummary <- summarySE(my.df, measurevar="len", groupvars=c("sex", "tre", "mea"))
ggplot(dataSummary, aes(x=mea, y=len, fill=sex)) +
+ geom_bar(position=position_dodge(), stat="identity") +
+ geom_errorbar(aes(ymin=len-se, ymax=len+se), width = .2, position=position_dodge(.9))
...但是这并没有给出正确输出附近的任何地方(整个组似乎都缺失了)。
任何人都可以提供解决方案来填补我所缺少的内容吗?
我的数据框的输入:
structure(list(mea = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L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 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L), .Label = c("m", "f"), class = "factor"), len = c(10.1,
10, 9.4, 11.1, 10.4, 10, 10.4, 11, 9.5, 10, 9.4, 9.2, 10.9,
11.6, 12.2, 10.3, 11, 11.4, 10.5, 10.9, 11, 11, 12, 10.7,
10.1, 11, 10.5, 10.8, 9.9, 11.4, 10, 11.9, 10, 12.2, 12.1,
11.8, 10.8, 10.4, 10.9, 11.7, 10, 10.6, 10.4, 10.9, 11, 9,
9.1, 9.8, 10, 9.9, 10.6, 11.5, 10.4, 10.7, 10.3, 10.6, 10,
11.6, 10.6, 10.7, 10.8, 10.1, 11.4, 10.2, 11.9, 10.2, 11,
9.5, 10.3, 10.8, 10.7, 11.5, 10.7, 9.4, 10, 11.7, 9.9, 10.7,
10, 9.8, 9.2, 10.9, 10.8, 10.6, 8.5, 11.2, 10.9, 10.8, 10.3,
10.2, 11, 10.4, 10.3, 10, 9, 10.5, 10.3, 9.5, 10.9, 11.5,
10.5, 9.5, 10, 10, 11.2, 10.1, 8.8, 10.6, 10, 11.1, 10.9,
10.5, 11.5, 10.5, 10.9, 11.6, 9.8, 10.8, 8.9, 10, 11, 11.8,
11, 11.1, 10.7, 12.1, 10.4, 11.8, 10.5, 8.9, 9.6, 8.7, 10.7,
8.8, 11.7, 9.8, 10.7, 10.6, 10.1, 11.3, 11.6, 11.2, 8.8,
11.2, 9.8, 10.7, 9.1, 10.1, 10.7, 10.1, 11.3, 9.9, 9.9, 10.1,
11.2, 11.1, 12, 11.9, 10.8, 12.1, 12, 13.1, 10.5, 12, 12.5,
12.2, 12, 11.5, 11.1, 10.9, 11.5, 10.5, 12, 13, 11.1, 10.5,
12, 11, 11.5, 13, 13, 11.3, 12, 11.5, 9.1, 13, 11.2, 10.5,
11.9, 12.5, 12, 9, 13, 11, 11.3, 10.5, 11.5, 12.1, 12, 11,
11.8, 11.4, 10.5, 13, 12.5, 12.2, 11.9, 11.4, 11, 11.9, 12,
11.5, 11.9, 10.8, 13, 11.8, 12.9, 12.4, 11.6, 11, 10.3, 13,
10.2, 10.8, 12.7, 11.2, 11.2, 11.1, 11.3, 12.1, 11.9, 13,
11.9)), .Names = c("mea", "tre", "sex", "len"), row.names = c(NA,
-233L), class = "data.frame")
答案 0 :(得分:1)
如果您在fill=interaction(sex, tre)
来电中使用ggplot
该怎么办?我不确定你想要什么输出。
答案 1 :(得分:1)
一种方法是:
my.df$gr<-apply(my.df[,c("mea","tre","sex")],1,paste,collapse=".")
boxplot(my.df$len~my.df$gr,las=2,col=rainbow(length(unique(my.df$gr))))
你当然可以个性化颜色和距离。例如:
boxplot(my.df$len~my.df$gr,las=2,col=c("darkgray","lightgray"),
boxwex = 0.7, at = c(0.8,1.8,3,4,5.2, 6.2,7.4,8.4,9.6, 10.6, 11.8,12.8))