我有3个数据框如下:
p=structure(list(m.key = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L), .Label = c("WSbin1", "WSbin10", "WSbin11",
"WSbin12", "WSbin13", "WSbin14", "WSbin15", "WSbin16", "WSbin2",
"WSbin3", "WSbin4", "WSbin5", "WSbin6", "WSbin7", "WSbin8", "WSbin9"
), class = "factor"), m.value = c(0.0163683333333333, 0.047105,
0.0778416666666667, 0.108578333333333, 0.139315, 0.170051666666667,
0.200788333333333, 0.231525, 0.262261666666667, 0.292998333333333,
0.323735, 0.354471666666667, 0.385208333333333, 0.415945, 0.446681666666667,
0.477418333333333, 0.508155, 0.538891666666667, 0.569628333333333,
0.600365, 0.631101666666667, 0.661838333333333, 0.692575, 0.723311666666667,
0.754048333333333, 0.784785, 0.815521666666667, 0.846258333333333,
0.876995, 0.907731666666667, 0.0184833333333333, 0.05345, 0.0884166666666667,
0.123383333333333, 0.15835, 0.193316666666667, 0.228283333333333,
0.26325, 0.298216666666667, 0.333183333333333, 0.36815, 0.403116666666667,
0.438083333333333, 0.47305, 0.508016666666667, 0.542983333333333,
0.57795, 0.612916666666667, 0.647883333333333, 0.68285, 0.717816666666667,
0.752783333333333, 0.78775, 0.822716666666667, 0.857683333333334,
0.89265, 0.927616666666667, 0.962583333333334, 0.99755, 1.03251666666667
), d.value = c(0.47015075540689, 0, 2.49179900365652, 0.564180906488269,
2.25672362595307, 0.799256284191713, 1.17537688851723, 0.893286435273092,
2.25672362595307, 1.78657287054619, 2.16269347487169, 1.36343719067998,
3.19702513676686, 2.20970855041238, 2.25672362595308, 0.799256284191713,
2.39776885257514, 1.97463317270893, 1.50448241730205, 0.658211057569647,
0.329105528784822, 0.282090453244135, 0.141045226622067, 0.235075377703445,
0.0470150755406891, 0.0940301510813782, 0, 0.0470150755406889,
0.0940301510813782, 0.0470150755406891, 0.196435277464829, 1.96435277464829,
1.38660195857527, 1.2017216974319, 1.2017216974319, 1.44437704018257,
1.99901782361268, 2.83097899875784, 2.03368287257706, 2.91186411300806,
2.28789323164919, 2.18389808475604, 2.29944824797065, 1.2017216974319,
1.15550163214606, 0.762631077216396, 0.496865701822803, 0.473755669179882,
0.277320391715054, 0.127105179536066, 0.0346650489643816, 0.0346650489643817,
0.0115550163214605, 0.0462200652858422, 0.0115550163214605, 0.0115550163214605,
0, 0, 0, 0.0115550163214606)), .Names = c("m.key", "m.value",
"d.value"), row.names = c(NA, 60L), class = "data.frame")
fit1= structure(list(key = c("WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2"
), value = c(0.0101871466691896, 0.385468980185449, 0.767075920237358,
1.12837007569642, 1.4528604685121, 1.72782361032424, 1.94414857335225,
2.09651233018556, 2.18340458158706, 2.2069072391712, 2.17223360628518,
2.0870763621798, 1.96083583914478, 1.80380810807998, 1.62640888752383,
1.43849683142649, 1.2488413453842, 1.06475900154186, 0.891921980214881,
0.734324308253646, 0.594378696276398, 0.473109259189085, 0.370403212290125,
0.285286949120412, 0.216197474533171, 0.161227577370596, 0.118331054198357,
0.0854816653043088, 0.0607855878768767, 0.0425515596053735, 0.0142149190679768,
0.578642388931195, 1.12727304342989, 1.61379883935818, 2.00734097848227,
2.28788344719097, 2.44688548420796, 2.48698063973164, 2.42045103600036,
2.26672813341405, 2.04936790771528, 1.7929935743732, 1.52063663950998,
1.25177326143101, 1.00118844134034, 0.778644259011712, 0.589209327872266,
0.434039709996902, 0.311387104287108, 0.217637522734546, 0.148236385395059,
0.0984169216473399, 0.0637042740784906, 0.0402094311582182, 0.0247521932605652,
0.0148621832487459, 0.00870530237342066, 0.00497462291659652,
0.00277363175262758, 0.00150897612822448)), .Names = c("key",
"value"), row.names = c(NA, 60L), class = "data.frame")
fit2= structure(list(key = c("WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1",
"WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin1", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2",
"WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2", "WSbin2"
), value = c(1.51369316493653e-06, 0.771724500719409, 1.77098891174459,
2.2237085929387, 2.32157560271493, 2.24188290322259, 2.08390066769219,
1.89860524571171, 1.71120658799604, 1.53355806751419, 1.37063863652871,
1.22392444808426, 1.09315374373053, 0.977255359747995, 0.874836139885851,
0.784432929959711, 0.70463795442206, 0.634156020911497, 0.571825341257592,
0.516619401020481, 0.467639462707478, 0.424102948018859, 0.385330524786113,
0.350733370250508, 0.319801326279231, 0.292092244232595, 0.267222591355362,
0.244859273743345, 0.224712575603594, 0.206530093350524, 2.1679623852545e-08,
0.752708825828478, 2.03466964917954, 2.61220685586129, 2.67995009090851,
2.50491984004508, 2.23927522144873, 1.95684426833444, 1.69019912197279,
1.45168214792837, 1.2441217165105, 1.06612210392846, 0.914631216543235,
0.786158414931748, 0.677325359568017, 0.585089597626746, 0.506810555671949,
0.440242819524289, 0.383498950740825, 0.335002506894312, 0.293440982029585,
0.257722855425043, 0.226940192533764, 0.200336929417348, 0.177282391645001,
0.157249386672229, 0.13979617573075, 0.124551679235952, 0.111203348733634,
0.0994872238643757)), .Names = c("key", "value"), row.names = c(NA,
60L), class = "data.frame")
在ggplot
中使用R
我可以在单独的图中绘制一个数据框,例如
ggplot(data=p, aes(m.value, d.value)) + geom_point()+geom_line() + facet_grid(~ m.key)
如何在此处添加其他2个图表,例如将fit1
和fit2
数据添加到上述绘图中。还有哪些选项可以将网格更改为具有多行。目前,我的整个数据生成了17个图,显示在1x17网格中。
答案 0 :(得分:1)
最简单的方法是将所有数据放在一个数据框中,格式为ggplot2
最喜欢的格式:
# First, put all together in list and select only needed (and rename variables):
list(p, dplyr::select(fit1, fit1 = value), dplyr::select(fit2, fit2 = value)) %>%
# cbind all:
Reduce(f = cbind) %>%
# make data long
tidyr::gather(key = key, value = value, -m.key, -m.value) %>%
# plot it:
ggplot(aes(x = m.value, y = value, color = key)) +
geom_line() +
# use facet_wrap instead of facet_grid, you could also specify the number of columns:
facet_wrap(~m.key, ncol = 2)