如何使用facet_wrap为每个绘图设置不同的y轴上限

时间:2017-06-14 11:47:01

标签: r ggplot2 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.有没有办法达到这个目的?

1 个答案:

答案 0 :(得分:1)

另一种方法是使用 gridExtra 包中的grid.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