ggplot2中的手动构面位置

时间:2017-10-23 15:54:10

标签: r ggplot2 position facet-wrap

我正在使用ggplot2绘制以下数据集:

DF <- structure(list(site = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E"
), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L), SWC = c(0.140086734851409, 0.137745990685859, 
0.146660019201229, 0.275950971628449, 0.298260250896057, 0.26870029739777, 
0.227566661823465, 0.197824137311287, 0.195409734063355, 0.229745648248465, 
0.226546607074933, 0.158508782420749, 0.0809095246636771, 0.0804010923965351, 
0.0845644708882278, 0.136702248824284, 0.121883242349049, 0.108167424836601, 
0.0970784232538687, 0.0860934461299105, 0.0910916878172589, 0.10747642248062, 
0.102700195758564, 0.0811833903700756, 0.115733715437788, 0.0631616319005478, 
0.0631265153446416, 0.171535848109378, 0.18694684173028, 0.142807562821677, 
0.145926108701425, 0.154393702185792, 0.171436382382201, 0.188897212829005, 
0.186402403754978, 0.165098945598251, 0.0713685071127924, 0.0436531172429078, 
0.0624862109235555, 0.127141665482761, 0.134542260869565, 0.124414092512545, 
0.100807230998223, 0.0765214392215714, 0.0798724029741452, 0.103098854664915, 
0.116568256944444, 0.1105108739241, 0.108650005144474, 0.0976296689160692, 
0.105006219572287, 0.122777662914972, 0.102765292125318, 0.0851933017211099, 
0.0566760862577016, 0.056282148272957, 0.0718264626865672, 0.0909327257326783, 
0.10461694624978, 0.103895834299474), LE = c(0.946565193060996, 
1.56650528637219, 4.45382423672104, 11.1985050677478, 29.1379975402081, 
74.6488855786053, 95.5801950803702, 77.4708126488623, 39.6136552461462, 
8.01576749720725, 2.6466216369622, 1.1745554117357, 2.13167679680568, 
2.98141098231535, 5.69653566874706, 13.8293309632019, 26.1687157009092, 
42.2656041113131, 53.2110193016699, 43.9856386693244, 28.6722592758158, 
10.8442703054334, 3.66463523523524, 1.97253929316558, 3.12430388517694, 
5.48701577036533, 6.95032997537497, 13.7237856698465, 32.2938743570904, 
48.7715488919351, 55.6860655989469, 46.0893482005537, 31.0498645245332, 
14.7640819905213, 8.98244641061733, 3.43372937817259, 3.69254598741488, 
5.03158318364554, 7.73098511689167, 22.4154276282377, 47.1483358705029, 
66.5623394397102, 77.0140731034483, 67.5194151592607, 44.6530820096047, 
20.4357590631479, 6.56212672069986, 2.8940355016033, 1.69359970210389, 
2.61391569682152, 6.3433679665485, 14.9249216033009, 41.1275149717784, 
82.0861782662955, 100.382874115357, 81.3020338687151, 54.7966365463682, 
21.3347745116999, 5.36748695652174, 1.70811548886738)), .Names = c("site", 
"month", "SWC", "LE"), row.names = c(NA, -60L), class = "data.frame")

当我绘制它时,这是默认情节:

library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
  geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
  geom_point() + facet_wrap(~site)

enter image description here

但是,我想手动控制构面的位置。例如,我希望空格位于方面B之后,因此方面CDE位于底行。

有什么办法吗?最好的方法是什么?

编辑:我投票重新打开这个问题,因为建议的答案似乎不再起作用。例如,names(g$grobs)返回NULL。

2 个答案:

答案 0 :(得分:1)

函数multiplot在这里非常有用。

{{1}}

试试这个

{{1}}

答案 1 :(得分:1)

试试这个(针对这个特定场景),

library(ggplot2)
ggplot(data = DF, aes(x = SWC, y = LE)) +
  geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
  geom_point() + facet_wrap(~factor(site,levels=c("C","D","E","A","B")), as.table=FALSE)