gpt4 book ai didi

r - ggplot - 多个箱线图

转载 作者:行者123 更新时间:2023-12-02 04:56:27 26 4
gpt4 key购买 nike

我正在尝试使用此数据集按因子混合分面创建箱线图(组合 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)

enter image description here

» 当我添加 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)

enter image description here

»它将每个“ia”级别重复到每个“mix”级别

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")

enter image description here

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

从第一个图中,facet.grid(不重复“ia”变量),从第二个图中,方框,从第三个图中,左边距内的中值,如果可能的话,进入因子“mix”的每个级别,按中值重新排序“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)

enter image description here

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com