我有一組實驗在10個階段或幾個月完成。我正在種植3種不同類型的細菌並計數生長(ACC)。有或沒有ggplot2的多面分組boxplot r
我試圖獲得一個facet_wrap
分組箱形圖的三種細菌類型(A,E和H)的不同階段的增長。我的數據:
head(EAH)
ACC sample site bed phase X.M.SA TYPE
1 SG A 1 0 1 NO E
2 SG A 2 0 1 NO A
3 MG A 3 0 1 NO H
4 SG A 4 0 1 NO A
5 LG A 1 0 2 NO E
6 LG A 2 0 2 NO H
不某些原因而一些有代表性的數據:
EAH<- data.frame(ACC=factor(sample(1:5,10,replace=T), label=c("NG","SG","LG","MG","GH")),
Phase=factor(seq(1,10,1)),
TYPE=factor(sample(1:3,10,replace=T), label=c("A","E","S"),replace=T))
我想GGPLOT2但如果這是可以做到不沒關係了。
ggplot(EAH, aes(x=as.factor(EAH$phase), y=EAH$ACC, group=EAH$TYPE)) +
geom_boxplot(aes(fill=factor(EAH$TYPE)))+ facet_grid(. ~ as.factor(EAH$phase))
這裏是我到目前爲止管理,但它不能得到面:
有點像第3圖下來對這個職位看起來很不錯: ggplot: arranging boxplots of multiple y-variables for each group of a continuous x
編輯
新代碼是接近,但我有將ACC更改爲數字。我能否在Y軸上獲得NG,SG,LG,MG,HG的標籤?
ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) +
geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ phase)
最終代碼:
library(RColorBrewer)
library(ggplot2)
ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) +
geom_boxplot(aes(fill=TYPE))+
facet_grid(. ~ phase) +
labs(x = "Phase", y = "Growth",color="Type")+
scale_fill_brewer(palette="Blues")+
theme_bw()+
theme(strip.background=element_rect(fill="black"))+
theme(strip.text=element_text(color="white", face="bold"))+
scale_y_discrete(breaks=c("1", "2", "3","4","5"),
labels=c("NG", "SG", "LG","MG","HG"))
這是一個常見問題解答。你已經告訴ggplot2在哪裏尋找變量。不要使用EAH $! – Roland
謝謝你看這個。我已經嘗試了至少10個不同的ggplot代碼,主要來自於stackoverflow,但沒有爲我工作。我無法理解這個錯誤的含義:layout_base(data,cols,drop = drop)中的錯誤: 至少有一個圖層必須包含所有用於構造的變量。另外爲什麼我不應該使用EAH $!? – HCAI