2014-02-10 65 views
1

我試圖設置一個條形圖來比較特定化合物的控制和實驗樣本。該數據集被稱爲「hydrocarbon3」,幷包含以下信息:R中的條形圖幫助

Exp. Contr. 
c12 89 49 
c17 79 30 
c26 78 35 
c42 63 3 
pris 0.5 0.8 
phy 0.5 0.9 
nap 87 48 
nap1 83 44 
nap2 78 44 
nap3 73 20 
acen1 81 50 
acen2 86 46 
fluor 83 11 
fluor1 68 13 
fluor2 79 17 
dibe 65 7 
dibe1 67 6 
dibe2 56 10 
phen 82 13 
phen1 70 12 
phen2 65 15 
phen3 53 14 
fluro 62 9 
pyren 48 11 
pyren1 34 10 
pyren2 19 8 
chrys 22 3 
chrys1 21 3 
chrys2 21 3 

當我創建與式的條形圖:

barplot(as.matrix(hydrocarbon3), 
     main=c("Fig 1. Change in concentrations of different hydrocarbon compounds\nin sediments with and without the presence of bacteria after 21 days"), 
     beside=TRUE, 
     xlab="Oiled sediment samples collected at 21 days", 
     space=c(0,2), 
     ylab="% loss in concentration relative to day 0") 

我接收該圖中,但我所需要的控制和每種化學物質的實驗樣品彼此相鄰允許進行更準確的比較,而不是左側的實驗樣品和右側的對照樣品:有沒有辦法在R上進行校正? barchart

回答

0

嘗試調換你的矩陣:

barplot(t(as.matrix(hydrocarbon3)), beside=T) 

基本上,barplot將繪製的東西在他們的矩陣,由於矩陣僅僅是一個載體包裹colwise顯示順序,意味着barplot將繪製所有第一列的值,那麼所有這些第二列等

0

檢查這個問題了:Barplot with 2 variables side by side

它採用GGPLOT2,所以你必須使用日e運行之前的代碼如下:

intall.packages("ggplot2") 
library(ggplot2) 

希望這對你有效。加上它看起來更好用ggplot2!

0
> df 
    row exp con 
1 a 1 2 
2 b 2 3 
3 c 3 4 
> barplot(rbind(df$exp,df$con), 
+   beside = TRUE,names.arg=df$row) 

生產:

enter image description here