[英]ggplot - multiple boxplots
我正在嘗試使用因子混合創建這個數據集的箱形圖(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)
我找到了https://github.com/lionel-/ggstance,並認為我會做出另一個答案。
library(devtools)
devtools::install_github("lionel-/ggstance")
library(ggplot2)
library(ggstance)
daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")
library(dplyr)
a = daf %>%
group_by(ia, mix) %>%
summarize(Rs=mean(Rs))
ggplot(daf, aes(x=Rs, y=ia)) +
geom_boxploth() +
geom_point(data=a, shape = 4, size = 3, colour = "blue") +
geom_text(data = a, mapping = aes(y = ia, x=0, label=round(Rs,2)),
color="NavyBlue", size=3.5, hjust=0) +
facet_grid(mix~., scales="free_y")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.