2017-06-14 68 views
2

每個小區設置不同的y軸上限我有一個數據集,看起來像這樣如何與facet_wrap

 Number   Type Count   Fcts 
1   1   AA 5505   AA 
2   2   AA 8150   AA 
3   3   AA 9424   AA 
4   4   AA 10050   AA 
5   5   AA 10815   AA 
6   6   AA 11483   AA 
7   7   AA 12322   AA 
8   8   AA 12431   AA 
9   9   AA 12693   AA 
10  10   AA 13099   AA 
11  11   AA 13361   AA 
12  12   AA 13028   AA 
13  13   AA 13403   AA 
14  14   AA 12869   AA 
15  15   AA 12680   AA 
16  16   AA 14028   AA 
17  17   AA 15331   AA 
18  18   AA 15861   AA 
19  19   AA 15722   AA 
20  20   AA 15716   AA 
21  21   AA 16078   AA 
22  22   AA 16283   AA 
23  23   AA 15621   AA 
24  24   AA 15225   AA 
25  25   AA 14221   AA 
26  26   AA 12996   AA 
27  27   AA 12710   AA 
28  28   AA 12418   AA 
29  29   AA 12837   AA 
30  30   AA 13235   AA 
31  31   AA 12073   AA 
32  32   AA 6189   AA 
33  1  New.AA 5505   AA 
34  2  New.AA 3820   AA 
35  3  New.AA 3571   AA 
36  4  New.AA 3148   AA 
37  5  New.AA 3017   AA 
38  6  New.AA 2959   AA 
39  7  New.AA 3167   AA 
40  8  New.AA 2759   AA 
41  9  New.AA 2898   AA 
42  10  New.AA 3790   AA 
43  11  New.AA 3306   AA 
44  12  New.AA 2900   AA 
45  13  New.AA 3376   AA 
46  14  New.AA 3999   AA 
47  15  New.AA 3502   AA 
48  16  New.AA 4069   AA 
49  17  New.AA 3814   AA 
50  18  New.AA 3193   AA 
51  19  New.AA 3166   AA 
52  20  New.AA 2999   AA 
53  21  New.AA 2851   AA 
54  22  New.AA 2823   AA 
55  23  New.AA 2471   AA 
56  24  New.AA 2204   AA 
57  25  New.AA 2152   AA 
58  26  New.AA 2163   AA 
59  27  New.AA 2013   AA 
60  28  New.AA 1841   AA 
61  29  New.AA 2085   AA 
62  30  New.AA 2139   AA 
63  31  New.AA 1652   AA 
64  32  New.AA 440   AA 
65  1 Comulative.AA 5505 Comulative.AA 
66  2 Comulative.AA 9325 Comulative.AA 
67  3 Comulative.AA 12896 Comulative.AA 
68  4 Comulative.AA 16044 Comulative.AA 
69  5 Comulative.AA 19061 Comulative.AA 
70  6 Comulative.AA 22020 Comulative.AA 
71  7 Comulative.AA 25187 Comulative.AA 
72  8 Comulative.AA 27946 Comulative.AA 
73  9 Comulative.AA 30844 Comulative.AA 
74  10 Comulative.AA 34634 Comulative.AA 
75  11 Comulative.AA 37940 Comulative.AA 
76  12 Comulative.AA 40840 Comulative.AA 
77  13 Comulative.AA 44216 Comulative.AA 
78  14 Comulative.AA 48215 Comulative.AA 
79  15 Comulative.AA 51717 Comulative.AA 
80  16 Comulative.AA 55786 Comulative.AA 
81  17 Comulative.AA 59600 Comulative.AA 
82  18 Comulative.AA 62793 Comulative.AA 
83  19 Comulative.AA 65959 Comulative.AA 
84  20 Comulative.AA 68958 Comulative.AA 
85  21 Comulative.AA 71809 Comulative.AA 
86  22 Comulative.AA 74632 Comulative.AA 
87  23 Comulative.AA 77103 Comulative.AA 
88  24 Comulative.AA 79307 Comulative.AA 
89  25 Comulative.AA 81459 Comulative.AA 
90  26 Comulative.AA 83622 Comulative.AA 
91  27 Comulative.AA 85635 Comulative.AA 
92  28 Comulative.AA 87476 Comulative.AA 
93  29 Comulative.AA 89561 Comulative.AA 
94  30 Comulative.AA 91700 Comulative.AA 
95  31 Comulative.AA 93352 Comulative.AA 
96  32 Comulative.AA 93792 Comulative.AA 

我想要做的是使用ggplot繪製爲條形圖AA型和New.AA,並作爲分散/折線圖類型Comulative.AA,分爲兩個不同的方面。

我可以用下面的代碼

ggplot(data = subset(df, Type != "Comulative.AA"), aes(x = Fraction, y = Count, fill = Type)) + geom_bar(stat = "identity", position = "dodge") + geom_line(data = subset(df, Type == "Comulative.AA"), aes(x = Fraction, y = Count), size = 1) + geom_point(data = subset(df, Type == "Comulative.AA"), aes(x = Fraction, y = Count), size = 3, shape = 21) + theme_bw() + theme(legend.position = "right") + scale_fill_manual(values = c("black", "blue", "red")) + facet_wrap(~Fcts, nrow = 2, scales = "free") 

plot

這是我得到的,這幾乎是很好,除了y軸做到這一點。我希望上面的情節從0到120000,而下面的情節從0到20000。有沒有辦法達到這個目的?

+0

看看@巴蒂斯特的這個問題的答案:https://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and- scale-free-in-ggplot2 – mzuba

+0

感謝您的建議。我嘗試過,但它不適用於我...我不明白我應該如何創建「虛擬」數據集 – pisistrato

+0

我糾正自己,它完美的作品。我用'dummy < - df [df $ Number == 32,]''和'dummy [,3] < - c(20000,20000,120000)'創建了一個虛擬數據框。然後'geom_blank(data = dummy)'竅門 – pisistrato

回答

1

一種替代方案是使用來自包gridExtragrid.arrange()

grid.arrange(
    ggplot(data = subset(df, Type != 'Comulative.AA'), aes(x = Fcts, y = Count, fill = Type)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    ylim(c(0, 120e3)), 

    ggplot(data = subset(df, Type == 'Comulative.AA'), aes(x = Number, y = Count, fill = Type)) + 
    geom_line(size = 1) + geom_point() + 
    ylim(c(0, 20e3))) 

enter image description here

+0

這也適用,但我更喜歡facet_wrap生成圖。 – pisistrato