按组排列ggplot图

时间:2018-07-13 11:32:40

标签: r ggplot2

我正在尝试从下面的两个ggplot图中排列一个分组的ggplot。我已附上一张希望实现的图像,但不确定如何实现。

我正在使用以下代码创建两个图;

Ra_plot %>%
  filter(date >= as_date("2016-12-01")) %>%
  ggplot(aes(x = date, y = adjusted, color = symbol)) +
  geom_line(size = 1) +
  facet_wrap(~ symbol, ncol = 2, scales = "free_y")


Ra_plot %>%
  filter(date >= as_date("2016-12-01")) %>%
  ggplot(aes(x = date)) +
  geom_line(aes(y = signal), color = "blue", linetype = 2) +
  geom_bar(aes(y = diff), stat = "identity", color = palette_light()[[1]]) +
  facet_wrap(~ symbol, ncol = 2, scale = "free_y")

我希望输出看起来像以下内容:

enter image description here

我应该考虑使用网格还是可以ggplot以我希望获得的方式处理地块。

数据:

Ra_plot <- structure(list(symbol = c("CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-018", 
    "CCT6-018", "CCT6-018", "CCT6-018", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-019", 
    "CCT6-019", "CCT6-019", "CCT6-019", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-020", 
    "CCT6-020", "CCT6-020", "CCT6-020", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", "CCT6-021", 
    "CCT6-021", "CCT6-021", "CCT6-021"), date = structure(c(17106, 
    17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119, 
    17120, 17121, 17122, 17123, 17126, 17127, 17128, 17130, 17133, 
    17134, 17135, 17136, 17137, 17140, 17141, 17142, 17143, 17144, 
    17147, 17148, 17149, 17150, 17151, 17154, 17155, 17156, 17157, 
    17158, 17162, 17163, 17164, 17165, 17106, 17107, 17108, 17109, 
    17112, 17113, 17114, 17115, 17116, 17119, 17120, 17121, 17122, 
    17123, 17126, 17127, 17128, 17130, 17133, 17134, 17135, 17136, 
    17137, 17140, 17141, 17142, 17143, 17144, 17147, 17148, 17149, 
    17150, 17151, 17154, 17155, 17156, 17157, 17158, 17162, 17163, 
    17164, 17165, 17106, 17107, 17108, 17109, 17112, 17113, 17114, 
    17115, 17116, 17119, 17120, 17121, 17122, 17123, 17126, 17127, 
    17128, 17130, 17133, 17134, 17135, 17136, 17137, 17140, 17141, 
    17142, 17143, 17144, 17147, 17148, 17149, 17150, 17151, 17154, 
    17155, 17156, 17157, 17158, 17162, 17163, 17164, 17165, 17106, 
    17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119, 
    17120, 17121, 17122, 17123, 17126, 17127, 17128, 17130, 17133, 
    17134, 17135, 17136, 17137, 17140, 17141, 17142, 17143, 17144, 
    17147, 17148, 17149, 17150, 17151, 17154, 17155, 17156, 17157, 
    17158, 17162, 17163, 17164, 17165), class = "Date"), adjusted = c(129.5, 
    127.169998, 120, 120.75, 122.150002, 124.220001, 123.18, 120.800003, 
    119.019997, 115.080002, 117.199997, 116.339996, 117.790001, 117.019997, 
    121.769997, 121.470001, 120.839996, 120.379997, 120.410004, 120.870003, 
    118.419998, 115.099998, 115.400002, 117.43, 117.309998, 117.949997, 
    118.910004, 119.68, 117.769997, 120.309998, 120.209999, 120.57, 
    119.870003, 119.239998, 119.089996, 119.040001, 117.400002, 117.269997, 
    118.010002, 116.919998, 116.349998, 115.050003, 785.409973, 765.559998, 
    767.030029, 755.049988, 784.929993, 787.75, 771.880005, 742.380005, 
    739.01001, 719.070007, 743.23999, 746.48999, 756.400024, 760.159973, 
    780, 785.330017, 780.119995, 780.369995, 766.77002, 762.52002, 
    750.570007, 743.650024, 740.340027, 759.359985, 764.719971, 770.419983, 
    767.330017, 768.659973, 760.119995, 774.340027, 768.820007, 761, 
    757.77002, 766, 771.219971, 770.599976, 766.340027, 760.590027, 
    771.400024, 772.130005, 765.150024, 749.869995, 123.300003, 122.339996, 
    122.139999, 122.029999, 124.580002, 124.339996, 122.190002, 115.419998, 
    114.779999, 113.379997, 113.589996, 115.190002, 115.029999, 115.209999, 
    117.959999, 118.040001, 117.690002, 117.410004, 116.93, 117.510002, 
    117, 117.220001, 120.809998, 119.160004, 124.57, 125.389999, 
    123.239998, 122.879997, 122.830002, 123.779999, 123.440002, 125, 
    124.220001, 125.449997, 125.120003, 126.5, 125.580002, 125.589996, 
    128.350006, 125.889999, 125.330002, 123.800003, 783.609985, 768.700012, 
    762.130005, 762.02002, 782.52002, 790.51001, 785.309998, 762.559998, 
    754.02002, 736.080017, 758.48999, 764.47998, 771.22998, 760.539978, 
    769.200012, 768.27002, 760.98999, 761.679993, 768.23999, 770.840027, 
    758.039978, 747.919983, 750.5, 762.52002, 759.109985, 771.190002, 
    776.419983, 789.289978, 789.27002, 796.099976, 797.070007, 797.849976, 
    790.799988, 794.200012, 796.419983, 794.559998, 791.26001, 789.909973, 
    791.549988, 785.049988, 782.789978, 771.820007), macd = c(0.775656910147982, 
    0.797346932149945, 0.521357938089917, 0.136184278050555, -0.189225922380254, 
    -0.562608723559488, -1.06956649261651, -1.58988420666739, -2.08672587969461, 
    -2.6328170184005, -3.1748828867045, -3.77297957133487, -4.21809665237388, 
    -4.57592546268422, -4.27842404747918, -4.05114131115747, -3.90961345620027, 
    -3.87769145767599, -3.77766883260927, -3.4366348094813, -3.02752419970859, 
    -2.50071754284896, -2.14121709554718, -1.68458637009835, -1.27877141907935, 
    -0.801211775630006, -0.662540261160538, -0.547616894476566, -0.690771598645834, 
    -0.681558984014052, -0.633303911869121, -0.537003121626467, -0.328646048125936, 
    0.0123666066978556, 0.269135086702899, 0.253527954587685, 0.253349884965104, 
    0.1755341017178, 0.105334423845704, -0.0848526621859413, -0.00907272762492584, 
    -0.170681933418881, -1.39741925937584, -1.64478151344726, -1.87486107309583, 
    -2.17220688902349, -2.19001170903169, -2.28360769820303, -2.67640945629708, 
    -3.16221761155032, -3.5538954065233, -4.03156809717544, -3.92095503463473, 
    -3.98267767647376, -3.9253330362836, -3.65723179383786, -3.31596185001577, 
    -2.86200454707787, -2.73373910181484, -2.63349273026018, -2.47851151699531, 
    -1.98888906445145, -1.43675528543228, -0.718707190244894, -0.339907299839792, 
    0.0970206085879166, 0.246495829549653, 0.456899457964988, 0.409962341436043, 
    0.211760844980757, 0.0277545328368545, -0.13563251053994, -0.0318934464552778, 
    0.0867525521517809, 0.237252901615004, 0.362420754166992, 0.537024214660198, 
    0.398764035692856, 0.299756970784082, 0.121605790402302, 0.0904638856131124, 
    0.068062539742253, 0.197465459016377, 0.109452369702434, 10.4817336801601, 
    11.1994207189728, 10.5134236362317, 9.57977051479777, 8.71513088628475, 
    7.70434778379978, 6.63200568570297, 5.52150630281762, 4.32004033207796, 
    3.10122551434258, 1.84055267575476, 0.678550859590987, -0.403813234961548, 
    -1.3786972359436, -2.18723105892463, -3.02812455149013, -3.46836540638097, 
    -3.80898326755053, -3.97538200443274, -3.52390525737234, -3.04992862026117, 
    -2.49247111286869, -1.79454060399381, -1.28461918599766, -0.552431033430645, 
    0.143918173601332, 0.515710334381669, 0.83713465878188, 1.17454917884416, 
    1.56313432346815, 2.05628047529247, 2.55875179687635, 2.99654375306577, 
    3.23791459192655, 3.19505253612893, 3.2705556443696, 2.94623845670969, 
    2.62047722898282, 2.53846052040871, 2.39906310330718, 2.33218102596211, 
    2.14880396198645, 1.16142408778224, 1.11289796834209, 0.857634605608393, 
    0.502738295170069, 0.323644396136347, 0.141574969069547, -0.196560513454014, 
    -0.60702736767867, -0.974101509630498, -1.4149908864092, -1.6747989032932, 
    -1.79873019336693, -1.85871822912509, -1.86048330205084, -1.7397964129521, 
    -1.61630897125068, -1.68022385242601, -1.79567565807346, -1.83917135625697, 
    -1.61100237230263, -1.29781430231263, -0.875134042826042, -0.720760171699075, 
    -0.578602147304341, -0.529415074109341, -0.34664447684073, -0.231934605482187, 
    -0.106327159686415, 0.0650733179262897, 0.2677973262599, 0.507418932971704, 
    0.763304301879297, 1.09066442179129, 1.43169087317443, 1.71318302123171, 
    1.76256435620636, 1.94269528713715, 2.0152098411186, 2.07486304778541, 
    1.90576683657873, 1.76798761158086, 1.4902979731293), signal = c(0.471248889710887, 
    0.550665334378575, 0.585899192345393, 0.559732591031496, 0.485843882520468, 
    0.372709606752546, 0.19661512109012, -0.0493110445741803, -0.363051685164443, 
    -0.741771010558719, -1.18312987932032, -1.66027849147863, -2.14408748374801, 
    -2.63149854378179, -3.0443669131062, -3.37565300405519, -3.63340069844774, 
    -3.832396873779, -3.95960263091331, -3.98868617788851, -3.90585780326337, 
    -3.71503790220504, -3.44451475030093, -3.15631056392528, -2.84826946480549, 
    -2.50289150029768, -2.14565247846263, -1.78675781867011, -1.48166190635506, 
    -1.22099910461122, -1.01350870116901, -0.835262704066713, -0.684602668292, 
    -0.541142887650088, -0.422215458501988, -0.32043010119663, -0.231433792369778, 
    -0.135177603440485, -0.0477450025671785, 0.013194025175397, 0.0718529578422349, 
    0.0894045261430187, -0.00883209068458739, -0.356694793813961, 
    -0.675289749494132, -0.991566329513017, -1.27363036666115, -1.5225889332732, 
    -1.78621117267921, -2.05360643070891, -2.3283789573942, -2.62106216159415, 
    -2.87397033061498, -3.10817217543475, -3.30296396957477, -3.46598842344212, 
    -3.5806944403102, -3.60131611706362, -3.55370739375968, -3.45144042973044, 
    -3.2788785874882, -3.06420459080117, -2.7813243251299, -2.42503256445893, 
    -2.0564409540147, -1.67722068083651, -1.33183175010012, -0.977316354569031, 
    -0.639154679936118, -0.340235528605444, -0.116164017795632, 0.0284051794146275, 
    0.104717817613474, 0.15212446783476, 0.167705833726658, 0.180586380906363, 
    0.189489131650275, 0.188244875456587, 0.198022222768068, 0.20845014027534, 
    0.233571962070123, 0.244678182758738, 0.256979616854804, 0.242779557753407, 
    7.62178184661316, 8.32109570649725, 8.88484290540874, 9.2675658486473, 
    9.48610906683087, 9.50984544412109, 9.32669650985554, 8.92062537743054, 
    8.29637550453838, 7.47631904166976, 6.43644481464553, 5.34368117279657, 
    4.23439408948998, 3.1128576314646, 2.01379331560633, 0.940445511473768, 
    -0.058429122881631, -0.961653967284796, -1.74794369159316, -2.34399457305173, 
    -2.75827007081308, -2.99034316835832, -3.03654798703056, -2.93625777892757, 
    -2.6611807213654, -2.25981587914515, -1.77929436781935, -1.24457029412883, 
    -0.722519801215889, -0.209957251912631, 0.295459591216386, 0.779158746868626, 
    1.25484351787567, 1.67599303180425, 2.01500796097398, 2.32110188430598, 
    2.5554467507424, 2.71610542298003, 2.82447500041787, 2.86256195908617, 
    2.83738742898459, 2.74319411886466, 0.938934056137465, 0.999690520765603, 
    1.02013038395502, 0.982569427314858, 0.909698261306271, 0.816315684711638, 
    0.681400896118077, 0.494491953191845, 0.258024992371722, -0.0282433380939926, 
    -0.337987434942357, -0.633139079272948, -0.895523137527966, -1.1382039928821, 
    -1.34724525755117, -1.50499508619524, -1.62423914005606, -1.71552515654972, 
    -1.76265631986614, -1.75556781642275, -1.69991049519449, -1.59062336338348, 
    -1.46398746001107, -1.33496587493909, -1.21419988636783, -1.06602440019169, 
    -0.892275394348216, -0.699737150284821, -0.513506518037163, -0.339549670417994, 
    -0.185932673107133, -0.0210366204873137, 0.164437442745534, 0.382338103554842, 
    0.611207825562891, 0.832818821306063, 1.06048798206424, 1.27716981797449, 
    1.477954898144, 1.63332688743367, 1.74495836628939, 1.78936209421584
    ), diff = c(0.304408020437095, 0.24668159777137, -0.0645412542554765, 
    -0.423548312980941, -0.675069804900722, -0.935318330312034, -1.26618161370663, 
    -1.54057316209321, -1.72367419453016, -1.89104600784178, -1.99175300738418, 
    -2.11270107985623, -2.07400916862587, -1.94442691890243, -1.23405713437298, 
    -0.675488307102275, -0.276212757752532, -0.0452945838969905, 
    0.181933798304037, 0.55205136840721, 0.87833360355478, 1.21432035935608, 
    1.30329765475375, 1.47172419382693, 1.56949804572614, 1.70167972466767, 
    1.48311221730209, 1.23914092419354, 0.790890307709222, 0.539440120597166, 
    0.380204789299893, 0.298259582440246, 0.355956620166064, 0.553509494347944, 
    0.691350545204886, 0.573958055784314, 0.484783677334881, 0.310711705158285, 
    0.153079426412883, -0.0980466873613383, -0.0809256854671607, 
    -0.2600864595619, -1.38858716869126, -1.2880867196333, -1.1995713236017, 
    -1.18064055951047, -0.916381342370541, -0.761018764929824, -0.890198283617874, 
    -1.10861118084141, -1.2255164491291, -1.41050593558129, -1.04698470401975, 
    -0.874505501039003, -0.622369066708831, -0.191243370395739, 0.26473259029443, 
    0.739311569985753, 0.819968291944841, 0.817947699470263, 0.800367070492897, 
    1.07531552634972, 1.34456903969762, 1.70632537421404, 1.71653365417491, 
    1.77424128942443, 1.57832757964978, 1.43421581253402, 1.04911702137216, 
    0.551996373586201, 0.143918550632487, -0.164037689954568, -0.136611264068752, 
    -0.0653719156829788, 0.0695470678883455, 0.18183437326063, 0.347535083009923, 
    0.210519160236268, 0.101734748016014, -0.0868443498730381, -0.143108076457011, 
    -0.176615643016485, -0.059514157838427, -0.133327188050973, 2.85995183354694, 
    2.87832501247557, 1.62858073082295, 0.312204666150468, -0.770978180546118, 
    -1.80549766032131, -2.69469082415257, -3.39911907461291, -3.97633517246042, 
    -4.37509352732719, -4.59589213889078, -4.66513031320558, -4.63820732445152, 
    -4.49155486740821, -4.20102437453097, -3.9685700629639, -3.40993628349934, 
    -2.84732930026573, -2.22743831283957, -1.17991068432061, -0.291658549448086, 
    0.49787205548963, 1.24200738303675, 1.65163859292991, 2.10874968793476, 
    2.40373405274648, 2.29500470220102, 2.08170495291071, 1.89706898006005, 
    1.77309157538078, 1.76082088407609, 1.77959305000772, 1.7417002351901, 
    1.5619215601223, 1.18004457515494, 0.949453760063623, 0.390791705967294, 
    -0.0956281939972077, -0.286014480009154, -0.463498855778994, 
    -0.505206403022477, -0.594390156878217, 0.222490031644772, 0.113207447576482, 
    -0.162495778346629, -0.479831132144789, -0.586053865169925, -0.674740715642091, 
    -0.877961409572091, -1.10151932087051, -1.23212650200222, -1.3867475483152, 
    -1.33681146835084, -1.16559111409398, -0.963195091597123, -0.722279309168744, 
    -0.39255115540093, -0.111313885055436, -0.0559847123699511, -0.0801505015237374, 
    -0.0765150363908331, 0.144565444120117, 0.402096192881862, 0.715489320557442, 
    0.743227288311991, 0.756363727634752, 0.684784812258493, 0.719379923350961, 
    0.660340788866029, 0.593409990598406, 0.578579835963453, 0.607346996677893, 
    0.693351606078837, 0.784340922366611, 0.926226979045754, 1.04935276961959, 
    1.10197519566882, 0.929745534900301, 0.882207305072916, 0.738040023144109, 
    0.596908149641417, 0.272439949145061, 0.0230292452914631, -0.299064121086543
    )), row.names = c(NA, -168L), vars = "symbol", drop = TRUE, class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), .Names = c("symbol", "date", 
    "adjusted", "macd", "signal", "diff"), indices = list(0:41, 42:83, 
        84:125, 126:167), group_sizes = c(42L, 42L, 42L, 42L), biggest_group_size = 42L, labels = structure(list(
        symbol = c("CCT6-018", "CCT6-019", "CCT6-020", "CCT6-021"
        )), row.names = c(NA, -4L), vars = "symbol", drop = TRUE, class = "data.frame", .Names = "symbol"))

编辑:我要问的问题与建议的链接不同。我已经应用了cowplot包,该包基本上并排绘制了两个图(根据上面的数据),这不是我要的。例如,应用cowplot::plot_grid(iris1, iris2, labels = "AUTO")可得出以下图:

enter image description here

这不是我所追求的。我正在尝试一些如何通过symbol重新布置图。数据看起来像;

# A tibble: 168 x 6
# Groups:   symbol [4]
   symbol   date       adjusted   macd  signal    diff
   <chr>    <date>        <dbl>  <dbl>   <dbl>   <dbl>
 1 CCT6-018 2016-11-01     130.  0.776  0.471   0.304 
 2 CCT6-018 2016-11-02     127.  0.797  0.551   0.247 
 3 CCT6-018 2016-11-03     120.  0.521  0.586  -0.0645
 4 CCT6-018 2016-11-04     121.  0.136  0.560  -0.424 
 5 CCT6-018 2016-11-07     122. -0.189  0.486  -0.675 
 6 CCT6-018 2016-11-08     124. -0.563  0.373  -0.935 
 7 CCT6-018 2016-11-09     123. -1.07   0.197  -1.27  
 8 CCT6-018 2016-11-10     121. -1.59  -0.0493 -1.54  
 9 CCT6-018 2016-11-11     119. -2.09  -0.363  -1.72  
10 CCT6-018 2016-11-14     115. -2.63  -0.742  -1.89  
# ... with 158 more rows

因此,我试图在第二张图上方绘制adjusted的{​​{1}}列(如第一张图所示)。

P.S。我正在使用symbol = CCT6-018-如果某些命令不起作用。

1 个答案:

答案 0 :(得分:1)

这是您要寻找的东西:

Ra_plot %>%
    gather(measure, value, adjusted, signal, diff) %>%
    mutate(dummy_facet = case_when(
        measure == "adjusted" ~ paste0("adjusted", symbol),
        measure == "signal" ~ paste0("sig_diff", symbol),
        measure == "diff" ~ paste0("sig_diff", symbol)
    )) %>%
    mutate(dummy_facet = factor(dummy_facet, levels = c("adjustedCCT6-018", "adjustedCCT6-019", "sig_diffCCT6-018", "sig_diffCCT6-019", "adjustedCCT6-020", "adjustedCCT6-021", "sig_diffCCT6-020", "sig_diffCCT6-021"))) %>%
    filter(date >= as_date("2016-12-01")) %>% 
    ggplot(aes(x = date, y = value))+
    geom_line(data = . %>% filter(measure == "adjusted"), aes(color = symbol), size = 1)+
    geom_line(data = . %>% filter(measure == "signal"), color = "blue", linetype = 2) +
    geom_bar(data = . %>% filter(measure == "diff"), stat = "identity", color = palette_light()[[1]])+
    facet_wrap(~dummy_facet, ncol = 2, scale = "free_y")+
    theme(legend.position = "none")

首先,我们gather您要绘制在一起的所有数据。接下来,我们创建一个虚拟facet变量,以确保您获得了所有想要的图。您可以不考虑虚拟变量和符号,但是,我个人只喜欢对一个变量进行改面,因为我不喜欢在绘图上具有两个方面标签(这就是为什么要在虚拟标签上粘贴符号)。我还将变量更改为一个因子,然后将级别设置为希望它们绘制的顺序。接下来,我们为每个图过滤合适的y值,然后按正常方式绘制。您需要稍微使用labeller参数来修正构面标签(如果您对此有疑问,请告诉我)。

enter image description here