我正在尝试通过因子混合(3 个箱线图组合)使用此数据集分面创建箱线图:

daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")

这是样本的样子:
                                            ia mix   Rs
1                                    Fluazinam   1 0.62
2                                    Fluazinam   1 0.76
3                                    Fluazinam   1 0.76
4                                    Fluazinam   1 0.52
5                                    Fluazinam   1 0.56
6                                    Fluazinam   1 0.20
7                                    Fluazinam   1 0.98
235 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.65
236 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.28
237 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.41

这些是我失败的尝试!
library(ggplot2)

qplot( Rs, ia, data=daf) +
  facet_grid(mix ~ ., scales = "free", space = "free", labeller = label_both)



» 当我添加 qplot( Rs, ia, data=daf, geom="boxplot") 它只是出现一条线,而不是框。
ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  facet_grid(mix ~. , scales = "free", space="free", labeller = label_both)

» 它将每个“ia”级别重复到每个“混合”级别
ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +
  layer(data = a, mapping = aes(x = ia, y= 0, label=a$Rs.median),
        geom = "text", color="NavyBlue", size=3.5) +
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue")

最后我想要三个图的组合:

从第一个图,facet.grid(不重复“ia”变量),从第二个,框,从第三个,左边内边距的中值,如果可能的话,进入每个级别因子“混合”,按中值重新排序“ia”...

有人可以帮我解决这个问题吗??

提前致谢!

最佳答案

geom_boxplot 假设分类变量位于 x 轴上。 coord_flip 不能与 facet_grid + geom_boxplot 结合使用。一种解决方法是旋转文本。您可以在另一个程序中导出和旋转图像(或弄清楚如何拉出 grid 对象并旋转它)。

a = ddply(daf, .(ia,mix), function(x) c(Rs=median(x$Rs, na.rm=TRUE)))

ggplot( data=daf, aes(x=ia, y=Rs) ) +
  geom_boxplot() +
  facet_wrap(~mix, scales="free_x") +
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust=0.5)) +
  theme(axis.title.x=element_text(angle = 90, vjust=0.5)) +
  theme(axis.text.y=element_text(angle = 90, hjust=0.5)) +
  theme(strip.text=element_text(angle = 90, hjust=0.5)) +
  geom_text(data = a, mapping = aes(x = ia, y= 0.02, label=round(Rs,2)),
        color="NavyBlue", size=3.5, angle=90, hjust=1) +
  ylim(-0.03,1)

关于r - ggplot - 多个箱线图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27680837/

10-12 22:38