2016-11-12 73 views
-1

我有一個問題來創建一個適當的Barplot。這裏是我的數據:如何創建一個帶有3個變量的Barplot?

Site Leaftype Bacteria_per_mm2   SD 
1  A  E  1733540.8 455950.56 
2  A  B  1062815.7 352876.56 
3  A  MI_E  1414862.9 442902.69 
4  A  MI_B   638189.9 287774.87 
5  B  E    0.0   0.00 
6  B  B    0.0   0.00 
7  B  MI_E    0.0   0.00 
8  B  MI_B   639858.4 213881.70 
9  C  E  1862013.0 802574.16 
10 C  B   628596.2 153344.48 
11 C  MI_E  1417365.6 374644.06 
12 C  MI_B   502209.6 270575.29 
13 D  E    0.0   0.00 
14 D  B   614831.3 176989.64 
15 D  MI_E    0.0   0.00 
16 D  MI_B  38271374.5 133237805.80 
17 E  E  1084505.8 604725.59 
18 E  B   306164.3 113604.13 
19 E  MI_E   588135.8 178212.25 
20 E  MI_B   294207.0 119656.46 
21 F  E  1712406.8 1378365.84 
22 F  B   329522.9 123385.05 
23 F  MI_E    0.0   0.00 
24 F  MI_B   389865.9 122340.59 
25 G  E    0.0   0.00 
26 G  B  6934997.3 28121464.95 
27 G  MI_E   957702.0 235390.37 
28 G  MI_B   373737.4 131594.27 
29 H  E  1677647.0 609105.40 
30 H  B   438390.6 145930.97 
31 H  MI_E    0.0   0.00 
32 H  MI_B   602317.8 187389.36 
33 I  E  1683903.8 303170.48 
34 I  B    0.0   0.00 
35 I  MI_E    0.0   0.00 
36 I  MI_B   407941.0 135698.56 
37 K  E  1257192.5 297964.48 
38 K  B   291982.3 124092.78 
39 K  MI_E  1209432.5 1670628.39 
40 K  MI_B   442144.7 182276.10 
41 L  E  1453793.9 703390.84 
42 L  B   663495.1 313438.15 
43 L  MI_E  1405130.2 11237373.56 
44 L  MI_B  2775709.1 9921931.93 
45 N  E   461332.1 120115.39 
46 N  B   760822.5 285298.68 
47 N  MI_E  1647614.5 260567.19 
48 N  MI_B   615109.4 171088.51 
49 O  E   931423.6 498561.29 
50 O  B   299768.5 144299.92 
51 O  MI_E  1112035.5 285024.12 
52 O  MI_B   335362.6 114015.88 
53 P  E    0.0   0.00 
54 P  B   501375.4  84989.70 
55 P  MI_E    0.0   0.00 
56 P  MI_B   869273.1 149036.53 
57 Q  E   961873.2 189228.01 
58 Q  B   381523.6 168520.10 
59 Q  MI_E   894300.1 184394.29 
60 Q  MI_B    0.0   0.00 
61 R  E   966878.6 209821.35 
62 R  B   284474.2  80791.71 
63 R  MI_E   903893.8 558948.64 
64 R  MI_B    0.0   0.00 
65 S  E    0.0   0.00 
66 S  B  1076163.4 927758.07 
67 S  MI_E  1137479.7 305446.03 
68 S  MI_B   802812.3 190456.51 
69 T  E    0.0   0.00 
70 T  B  2287681.5 10420861.06 
71 T  MI_E    0.0   0.00 
72 T  MI_B   761656.7 406262.84 

我嘗試創建一個barplot,這說明我在不同Sites和每個Leaftype每平方毫米leafsurface細菌的數量,所以我想創建一個Y一barplot(響應)變量和2個分類變量。我的第一個問題是,LeaftypeSites不是數字,而R告訴我,不可能創建帶變量的barplot,這些變量不是數字向量。

這裏是我的代碼中使用:

Bacteria_all<-read.delim("Mean_Bacteria_counts_final.csv", header = TRUE, sep = ";")# my data 
Bacteria_all 
str(Bacteria_all) 
mean5<-Bacteria_all$Bacteria_per_mm2 
mean5 
sd5<-Bacteria_all$SD 
sd5 

library(ggplot2) 
ggplot(data=Bacteria_all)+ 
    geom_bar(stat="identity",aes(x=Site,y=mean5),fill = "forestgreen")+ 
    theme_bw()+ 
    geom_errorbar(aes(x=Site,ymin=mean5-sd5,ymax=mean5+sd5),width=0.5)+ 
    ylim(0,1000000)+ 
    labs(x='Sites and Tree Species',y= 'Bacterial number per mm2 leafsurface') 

,這裏是適當的圖像,但Leaftypes丟失。

enter image description here

能否請你幫我解決這個問題?我發現了一些相關的問題,但我無法將這些答案應用於我的問題,我是R中的初學者!

希望你能幫助我!

回答

1

如何通過leaftype添加facet_wrap到面:

ggplot(data=Bacteria_all)+ 
geom_bar(stat="identity",aes(x=Site,y=mean5),fill = "forestgreen")+ 
theme_bw()+ 
geom_errorbar(aes(x=Site,ymin=mean5-sd5,ymax=mean5+sd5),width=0.5)+ 
ylim(0,1000000)+ 
labs(x='Sites and Tree Species',y= 'Bacterial number per mm2 leafsurface') + 
facet_wrap(~ Leaftype, ncol=2) 

enter image description here

(注:我只用了你的數據的幾行,使這個)

另一種方法是通過葉型躲避酒吧,但這樣做會更復雜,可能看起來不太好。

相關問題