在次面ggplot中为两个图中的一个添加二次系列

时间:2016-10-06 00:31:45

标签: r ggplot2 facet

我已经获得了以下图表enter image description here

由以下代码和数据构成。

   g <- ggplot()
g <- g + geom_line(data = test, aes(x = date, y = value))
g <- g+ facet_wrap(~variable, dir = "v", scales = "free")
g

前100行数据:

dput(test)
     structure(list(city = c("City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1", "City1", "City1", "City1", "City1", "City1", 
"City1", "City1"), date = structure(c(16801, 16802, 16803, 16804, 
16805, 16806, 16807, 16808, 16809, 16810, 16811, 16812, 16813, 
16814, 16815, 16816, 16817, 16818, 16819, 16820, 16821, 16822, 
16823, 16824, 16825, 16826, 16827, 16828, 16829, 16830, 16831, 
16832, 16833, 16834, 16835, 16836, 16837, 16838, 16839, 16840, 
16841, 16842, 16843, 16844, 16845, 16846, 16847, 16848, 16849, 
16850, 16851, 16852, 16853, 16854, 16855, 16856, 16857, 16858, 
16859, 16860, 16861, 16862, 16863, 16864, 16865, 16866, 16867, 
16868, 16869, 16870, 16871, 16872, 16873, 16874, 16875, 16876, 
16877, 16878, 16879, 16880, 16881, 16882, 16883, 16884, 16885, 
16886, 16887, 16888, 16889, 16890, 16891, 16892, 16893, 16894, 
16895, 16896, 16897, 16898, 16899, 16900, 16901, 16902, 16903, 
16904, 16905, 16906, 16907, 16908, 16909, 16910, 16911, 16912, 
16913, 16914, 16915, 16916, 16917, 16918, 16919, 16920, 16921, 
16922, 16923, 16924, 16925, 16926, 16927, 16928, 16929, 16930, 
16931, 16932, 16933, 16934, 16935, 16936, 16937, 16938, 16939, 
16940, 16941, 16942, 16943, 16944, 16945, 16946, 16947, 16948, 
16949, 16950, 16951, 16952, 16953, 16954, 16955, 16956, 16957, 
16958, 16959, 16960, 16961, 16962, 16963, 16964, 16965, 16966, 
16967, 16968, 16969, 16970, 16971, 16972, 16973, 16974, 16975, 
16976, 16977, 16978, 16979, 16980, 16981, 16982, 16801, 16802, 
16803, 16804, 16805, 16806, 16807, 16808, 16809, 16810, 16811, 
16812, 16813, 16814, 16815, 16816, 16817, 16818, 16819, 16820, 
16821, 16822, 16823, 16824, 16825, 16826, 16827, 16828, 16829, 
16830, 16831, 16832, 16833, 16834, 16835, 16836, 16837, 16838, 
16839, 16840, 16841, 16842, 16843, 16844, 16845, 16846, 16847, 
16848, 16849, 16850, 16851, 16852, 16853, 16854, 16855, 16856, 
16857, 16858, 16859, 16860, 16861, 16862, 16863, 16864, 16865, 
16866, 16867, 16868, 16869, 16870, 16871, 16872, 16873, 16874, 
16875, 16876, 16877, 16878, 16879, 16880, 16881, 16882, 16883, 
16884, 16885, 16886, 16887, 16888, 16889, 16890, 16891, 16892, 
16893, 16894, 16895, 16896, 16897, 16898, 16899, 16900, 16901, 
16902, 16903, 16904, 16905, 16906, 16907, 16908, 16909, 16910, 
16911, 16912, 16913, 16914, 16915, 16916, 16917, 16918, 16919, 
16920, 16921, 16922, 16923, 16924, 16925, 16926, 16927, 16928, 
16929, 16930, 16931, 16932, 16933, 16934, 16935, 16936, 16937, 
16938, 16939, 16940, 16941, 16942, 16943, 16944, 16945, 16946, 
16947, 16948, 16949, 16950, 16951, 16952, 16953, 16954, 16955, 
16956, 16957, 16958, 16959, 16960, 16961, 16962, 16963, 16964, 
16965, 16966, 16967, 16968, 16969, 16970, 16971, 16972, 16973, 
16974, 16975, 16976, 16977, 16978, 16979, 16980, 16981, 16982
), class = "Date"), variable = c("P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", "P", 
"P", "P", "P", "P", "P", "P", "P", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q"), value = c(1.19595583697778, 
1.19727308224637, 1.197591250109, 1.19707831670078, 1.19534608609092, 
1.19408361941821, 1.18465671605919, 1.18303109443474, 1.17953071944175, 
1.17653583434317, 1.17726536329349, 1.17161104715376, 1.17713306585643, 
1.17693788659163, 1.16832058435098, 1.16821094612151, 1.16497115588415, 
1.1675127622148, 1.15598294413332, 1.152255756492, 1.14755413926052, 
1.14807378351653, 1.14451041405442, 1.13969229363408, 1.14011155295943, 
1.1366271710468, 1.13527316436016, 1.12953453394174, 1.12304999675919, 
1.11952644860651, 1.11868871910188, 1.11853208023604, 1.11383857928919, 
1.11465245198515, 1.11207010726748, 1.104257288807, 1.09905270873952, 
1.09990718771727, 1.09518208540324, 1.08892078163938, 1.08495287776348, 
1.08463628690608, 1.08539606871615, 1.08012638256601, 1.08220770048229, 
1.07774707639465, 1.07187590767893, 1.06732234596841, 1.06471813276957, 
1.05983857272792, 1.05289706639051, 1.05460258416785, 1.0499814950701, 
1.05061217330383, 1.04789795008156, 1.04269021176944, 1.04497801193182, 
1.04214237436377, 1.04673092048574, 1.04459306533948, 1.04273112443642, 
1.03966031385237, 1.03052005652388, 1.02958839308547, 1.02567049745298, 
1.02866461261841, 1.02502699312409, 1.02187792234554, 1.01757789243438, 
1.01871076583442, 1.02013855011857, 1.01913702133028, 1.02340665916329, 
1.02161211038772, 1.0209744043766, 1.02113175108364, 1.15622510415428, 
1.24703243981324, 1.17784560462463, 1.12115427217624, 1.07897067408633, 
1.04006931898307, 1.03417455023498, 1.02531804624456, 1.02049892326534, 
1.02182202489205, 1.02523012424604, 1.02804937458388, 1.01955280433173, 
1.01658889240353, 1.01628527069251, 1.01669243011437, 1.0179979953923, 
1.0182484851811, 1.01649692485876, 1.01666851572913, 1.01605454002516, 
1.01653866044671, 1.01741150974144, 1.01568548074531, 1.01903902751509, 
1.01780563780184, 1.14454088928908, 1.19028573439265, 1.02409665165698, 
1.01885462650038, 1.02095137272463, 1.02291401457957, 1.01376052569088, 
1.01493930209775, 1.01803841453356, 1.06480269536968, 1.21515082497158, 
1.25194599663668, 1.25652598461445, 1.25172666582325, 1.249529403639, 
1.2499977810082, 1.24449897293083, 1.23794129386962, 1.22836528370345, 
1.22718630592982, 1.22521303237052, 1.22145169871298, 1.21848383487046, 
1.21198097053623, 1.20313863164648, 1.17455205067335, 1.16897510412217, 
1.17230469864649, 1.1703505875805, 1.17767948530013, 1.17506209298896, 
1.17603824669991, 1.1662445884426, 1.1710799789741, 1.17102186965628, 
1.17634513036903, 1.16915792174402, 1.16499477632285, 1.16398023048126, 
1.15688237930377, 1.15736490355842, 1.17423548132354, 1.15979503896867, 
1.14634431525202, 1.14393260332781, 1.14487592353223, 1.14344219160489, 
1.14301967918615, 1.14678245417723, 1.13981779604755, 1.13898173123374, 
1.13826542424226, 1.14883836029851, 1.16509927093523, 1.19837000830001, 
1.20729037331915, 1.20915802099201, 1.20796335409097, 1.20647111925534, 
1.20412218561609, 1.20400554849383, 1.20326362522666, 1.20541218850056, 
1.23124476309252, 1.29553079195154, 1.29127102815494, 1.2640152056018, 
1.24825446335391, 1.24930412214907, 1.25046655412771, 1.24937863369142, 
1.24595624789709, 1.24285039593887, 1.24068331496315, 1.23528030201594, 
1.23459562721402, 1.23681644491003, 1.23413163628408, 1.23250241904689, 
1.22718771756916, 31.0655241264559, 31.1344895448955, 30.7244674835061, 
32.346149068323, 31.6280070391553, 31.9367123287671, 32.2545975395431, 
31.7553729328383, 31.5440654016956, 30.6076864167674, 31.4644831261101, 
31.6311260623229, 32.056388308977, 31.959912464986, 33.2696990084986, 
30.8183526011561, 30.8237096774194, 32.0415612718828, 31.4449603311487, 
32.6560123541524, 31.9236363636364, 32.684814690027, 32.0152649285114, 
30.409302213667, 31.3665088312664, 30.5288613625536, 31.7748211829436, 
32.5768355333571, 32.0374296630775, 31.7912190889371, 30.8937422771403, 
32.3614485627837, 31.9945498154982, 32.5385891968728, 34.0038387715931, 
33.2023394330822, 32.4538723013321, 31.468351543943, 32.3251289949942, 
32.3028608058608, 32.8246254295533, 33.5843394957983, 33.5230983118173, 
32.178807106599, 31.7983123953099, 33.1008447573175, 32.8832780708365, 
32.3889087656529, 34.4613921282799, 33.7972937521574, 32.1406167979003, 
32.2994454148472, 33.0451459992416, 33.0220445660672, 33.2791743444366, 
34.1011626321173, 34.203805537669, 32.5900088495575, 31.8928041144902, 
32.7245606531882, 32.8331918238994, 33.4368714448237, 34.3246924671735, 
33.6011208406305, 32.1016369455876, 32.5541333959605, 33.5697835330499, 
33.2804558716266, 34.1139131909122, 34.3971260504202, 34.1853037932192, 
33.0993342269884, 32.5084633240483, 33.6142179732314, 33.3158967391304, 
33.6998339483395, 31.4930986509275, 30.173149540518, 29.9533418107833, 
30.6344482929243, 31.9801386748844, 33.0810794862895, 33.2963586072242, 
33.5764487067713, 32.2763194819212, 32.3099542543458, 32.1056442920137, 
33.1722404371585, 33.4575755342668, 33.6854843205575, 33.7882066469233, 
34.1263812335958, 32.4418177797435, 32.9273103448276, 33.3583601532567, 
33.2924176231026, 33.9792640390652, 34.4365950704225, 33.4180832735104, 
32.9443778383288, 32.3794597249509, 33.6505189231369, 31.4590072859745, 
31.1987426503844, 33.8312586395053, 33.8342499078511, 32.181307220387, 
32.7778998968008, 33.4298050682261, 33.5854939209726, 33.3305589836661, 
33.2628471967754, 30.4699912701877, 29.5665595794393, 28.4461097992916, 
28.288617221873, 29.8103100454733, 30.2436651767574, 30.6968358445678, 
30.3143667296786, 29.7831834259727, 30.2178185993111, 30.849485140226, 
30.7303600654664, 30.6218956792526, 31.4531953096372, 31.3770564650852, 
30.6881558935361, 31.0718737594283, 31.7385341437254, 31.4707510946447, 
31.0942626970228, 31.6889581928523, 31.7369900990099, 30.5846955060899, 
30.9038881549978, 31.095121056493, 30.4771621621622, 31.8030300884956, 
32.1036966176973, 31.6250702341137, 30.5364735945486, 31.0456163194444, 
31.5749903660886, 31.7424405817666, 31.9091225720027, 32.0009637753406, 
32.9180676168379, 30.6367379032258, 31.401959234609, 32.1638317130406, 
31.4913933040243, 32.2066137394686, 32.5509534028204, 32.5669479733817, 
30.8478368121442, 30.045958986731, 30.1729124315872, 30.3799077831059, 
31.2671658502449, 31.6199276859504, 30.9851956382296, 30.2176903336185, 
30.2882907662083, 29.928804440649, 30.1152611012433, 29.5357713858797, 
29.9978343023256, 30.8895151726543, 30.4835202205882, 29.8759495708155, 
30.5014751029577, 30.1842084805654, 30.6842552482506, 31.1299466132799, 
30.794447082768, 30.2690059982862, 30.6518622564567, 30.6830382215289, 
30.8988059701493, 31.071034719835, 31.501870138654)), .Names = c("city", 
"date", "variable", "value"), class = c("data.table", "data.frame"
), row.names = c(NA, -364L), .internal.selfref = <pointer: 0x0000000000330788>, index = structure(integer(0), "`__variable`" = integer(0)))

我想要做的是将次要系列放在顶部刻面图上。例如,我知道我可以通过指定辅助系列的来源添加辅助系列到ggplot,例如:

对于图表:

 p <- ggplot()
    p <- p+ geom_line(data = test1, aes(x = date, y = value), colour = "red")
    p <- p + geom_line(data = GateP_m, aes(x = date, y = value), colour = "blue")
    p

enter image description here

您可以看到红线与初始图表中的顶面相同。我似乎无法找到如何将红线添加到初始图表中。

第二个图的数据是:

test1 <- test[variable == "P",]

并且:

dput(head(GateP_m, n = 100))
structure(list(date = structure(c(16801, 16804, 16805, 16806, 
16807, 16808, 16811, 16812, 16813, 16814, 16815, 16818, 16819, 
16820, 16821, 16822, 16825, 16826, 16827, 16828, 16829, 16832, 
16833, 16834, 16835, 16836, 16839, 16840, 16841, 16842, 16843, 
16846, 16847, 16848, 16849, 16850, 16853, 16854, 16855, 16856, 
16857, 16860, 16861, 16862, 16863, 16864, 16867, 16868, 16869, 
16870, 16871, 16874, 16875, 16876, 16877, 16878, 16881, 16882, 
16883, 16884, 16885, 16888, 16889, 16890, 16891, 16892, 16895, 
16896, 16897, 16898, 16899, 16902, 16903, 16904, 16905, 16906, 
16909, 16910, 16911, 16912, 16913, 16916, 16917, 16918, 16919, 
16920, 16923, 16924, 16925, 16926, 16927, 16930, 16931, 16932, 
16933, 16934, 16937, 16938, 16939, 16940), class = "Date"), variable = structure(c(10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), class = "factor", .Label = c("Sydney", 
"Melbourne", "Brisbane", "Adelaide", "Perth", "Darwin", "Hobart", 
"National_Average", "date", "Average")), value = c(1.062, 1.062, 
1.059, 1.061, 1.066, 1.069, 1.068, 1.067, 1.064, 1.058, 1.054, 
1.046, 1.04, 1.034, 1.029, 1.023, 1.021, 1.02, 1.019, 1.02, 1.02, 
1.023, 1.026, 1.025, 1.02, 1.015, 1.01, 0.998, 0.992, 0.982, 
0.974, 0.966, 0.958, 0.957, 0.959, 0.959, 0.964, 0.968, 0.965, 
0.963, 0.963, 0.967, 0.968, 0.976, 0.979, 0.99, 0.988, 0.997, 
1.002, 1.007, 1.01, 1.019, 1.03, 1.034, 1.036, 1.037, 1.036, 
1.033, 1.034, 1.033, 1.035, 1.035, 1.037, 1.033, 1.035, 1.035, 
1.041, 1.036, 1.026, 1.022, 1.016, 1.013, 1.014, 1.017, 1.027, 
1.032, 1.037, 1.039, 1.035, 1.028, 1.024, 1.022, 1.023, 1.027, 
1.032, 1.043, 1.05, 1.061, 1.068, 1.07, 1.072, 1.07, 1.066, 1.065, 
1.065, 1.067, 1.069, 1.078, 1.086, 1.095)), .Names = c("date", 
"variable", "value"), class = c("data.table", "data.frame"), row.names = c(NA, 
-100L), .internal.selfref = <pointer: 0x0000000000330788>)

基本上,我想知道如何添加GateP_m&#34;值&#34; (红线)到初始图的顶部。

谢谢!

1 个答案:

答案 0 :(得分:1)

如果你想要这样的东西: enter image description here

尝试

library(dplyr)
library(ggplot2)

d <- rbind(test, mutate(GateP_m, city = "GateP_m", variable = "P"))
ggplot(d, aes(x = date, y = value, colour = city)) +
  geom_line() +
  facet_wrap(~variable, dir = "v", scales = "free") +
  scale_colour_manual(values = c("red", "blue"))

解决这类问题的一种方法是为ggplot提供已经“预格式化”的数据框。在这种情况下,我们会将cityvariable个变量附加到GateP_m数据框,然后rbind将两个数据框放在一起。然后,我们将city映射到colour美学,以在“P”面板中绘制两条不同颜色的线条。