合并/合并颜色并将图例填充为一个

时间:2019-01-10 23:47:51

标签: r ggplot2 legend

问题

我有一个带有辅助轴的图表,并且我试图结合其两个图例。 y轴主要显示以百万为单位的美元,次轴显示两个系列的变化百分比。即使阅读了Combine legends for color and shape into a single legendHow to merge color, line style and shape legends in ggplot,我也无法弄清楚如何合并图例。

希望有人可以帮助我破解代码。

尝试

这是我徒劳的尝试之一。我也根据上面的两个链接尝试了其他一些方法,但是想出所有尝试都不会使问题杂乱无章。

library(tidyverse)
library(scales)
m <- 100; b <- 60 # set slope and constant for transformation
df %>% 
  {ggplot(data = ., aes(x = Date)) + 
      geom_area(aes(y = B / 1000000, fill = "B"), color = "black") + 
      geom_line(aes(y = (A_pct * m) + b, color = "A % Chg"), size = 1.5) + 
      geom_line(aes(y = (B_pct * m) + b, color = "B % Chg"), size = 1.5) + 
      labs(x = "Date", 
           y = "(Millions)", 
           fill = "Series", 
           color = "Series") + 
      scale_y_continuous(labels = dollar, breaks = seq(0, 100, 10), expand = c(0, 0), limits = c(0, 100),
                         sec.axis = sec_axis(trans = ~ (. - b) / m, name = "Percent Change", 
                                             breaks = seq(-0.3, 0.3, 0.05), labels = percent)) + 
      scale_x_date(labels = date_format("%b-%y"), date_breaks = "12 months", expand = c(0, 0)) + 
      coord_cartesian(xlim = c(unique(.$Date)[length(unique(.$Date))] %m-% years(10), unique(.$Date)[length(unique(.$Date))]), 
                      ylim = c(30, 90), 
                      expand = F) + 
      scale_fill_manual(values = c("yellow"),
                        name  = "Series") + 
      scale_color_manual(values = c("red", "blue"),
                         name  = "Series")}

这产生了以下图表: Chart Attempt

数据:

df <- structure(list(Date = structure(c(14123, 14153, 14184, 14214, 
14245, 14276, 14304, 14335, 14365, 14396, 14426, 14457, 14488, 
14518, 14549, 14579, 14610, 14641, 14669, 14700, 14730, 14761, 
14791, 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 
15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 
15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 
15614, 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 
15887, 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 
16161, 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 
16436, 16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679, 
16709, 16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953, 
16983, 17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226, 
17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501, 
17532, 17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775
), class = "Date"), A = c(762802827.667328, 684557819.357221, 
683078228.685863, 684923302.80348, 680108742.131622, 674345726.58818, 
670367822.267582, 668775713.03974, 662147200.645882, 659653570.470808, 
658100884.274198, 649038407.743977, 649865746.371988, 642739421.218352, 
645055621.146561, 640364325.394116, 643483290.76661, 642104901.401949, 
645042378.428708, 644639212.118485, 643684820.036354, 645331197.18658, 
649676687.291223, 650070593.609856, 649833600.882055, 651613886.137383, 
651525206.340858, 647539746.657107, 654043995.49301, 654470506.433311, 
660228922.87752, 662973484.519923, 663546266.226065, 665956114.261005, 
663494450.715777, 666085960.901351, 665037956.991653, 665268683.177349, 
668885213.86704, 667117622.047159, 668164549.305417, 668613878.025327, 
666630965.795326, 666532863.504481, 663957649.994865, 665263243.591996, 
663792382.913478, 663524136.85967, 666199810.722237, 669074492.030905, 
662084745.211241, 668256287.121948, 668282475.914218, 675156013.429153, 
670939409.057424, 671156502.269539, 667831681.817089, 668859878.065185, 
668653060.225967, 675398523.213436, 672820478.944018, 672516833.91557, 
673235798.147654, 675995232.017351, 672319813.933483, 672598913.964035, 
672876432.160761, 672147290.37548, 672333402.435955, 671803586.865589, 
671361694.596288, 672983124.225669, 674338250.52938, 677146802.911298, 
677232518.432708, 684924976.664664, 687641059.867747, 688188879.344581, 
688503682.252372, 692355028.691986, 698663737.946556, 691644262.920506, 
692146337.496614, 691718075.102452, 688564391.826365, 694277783.230451, 
697695482.092572, 699642224.771449, 698260964.239, 701068073.603142, 
695849336.356779, 699609581.33745, 701482536.535517, 707003996.404896, 
713175786.209646, 709984856.393022, 716718142.355192, 722830549.929658, 
722764884.71728, 724060682.219488, 723036313.23472, 728297769.13983, 
726930276.654163, 728281942.37379, 728174161.622552, 735945462.302633, 
736376568.621623, 744579122.250237, 741114133.41255, 738419937.656301, 
737167817.322584, 741155963.667472, 737393478.208422, 739334840.564452, 
742281289.277673, 744624650.985584, 744201568.886584, 749181240.802847, 
742149761.891815, 742243432.572682, 747154941.639562), B = c(72673289.3505159, 
75175973.662292, 72162585.115631, 69132209.8468562, 73943288.6765151, 
65281127.7148463, 66284392.9607938, 68853030.0371962, 68612070.3235348, 
70527744.0337862, 64793052.575308, 65028797.4022448, 67256155.9760917, 
63605828.6271377, 71656481.06915, 62044790.2666628, 65673527.4866829, 
66667885.0245654, 67539464.1752361, 66328988.6787681, 65364549.8541026, 
63205801.39903, 62243524.374848, 65334616.7924324, 63969153.9553376, 
65018625.4937797, 59072449.7059314, 58886533.8817185, 64487952.2943509, 
60583547.6118104, 63217434.3782532, 63736034.1250396, 63156326.1383038, 
62346931.8826291, 61687778.6429483, 59582301.8421127, 62506221.5055315, 
61322750.264894, 59954395.4590005, 62715626.4983179, 62573826.918567, 
62907669.6876324, 59933337.4365451, 57164134.404576, 60757687.9194966, 
62602511.4739311, 60679314.7943261, 60890928.9983419, 61710047.3919278, 
59516229.9696478, 57060501.6865499, 62744749.7968118, 62299548.2789245, 
63933582.9897922, 64194779.1813546, 64255467.1364963, 63041559.0293688, 
62962914.7166446, 63055382.4141208, 64632997.036102, 61398325.9545797, 
60883641.8864262, 62657613.7233469, 60831150.7882065, 60872687.3230163, 
62880097.3078214, 59532706.9697267, 62516509.6750197, 62774257.4060072, 
63221413.9489894, 62917513.723825, 62139958.200027, 61478761.5212116, 
62598796.8665797, 63516190.1019831, 65878945.1058892, 67062980.6743056, 
64942796.634432, 65632743.5400966, 62219480.9009314, 63953050.3253247, 
64061276.1140881, 63580862.4439483, 62219423.7457726, 66679967.1461682, 
65083647.9411808, 67478567.3003478, 65807532.4662258, 64563479.1954444, 
68934356.0984697, 66427630.2383053, 67500786.1834834, 66333171.5204909, 
66575836.5682508, 66352216.4413541, 64857859.4747038, 67746871.9277182, 
67288844.1114591, 68601695.2009794, 70310502.1387566, 64519592.0189133, 
66073431.4530784, 69269206.5949295, 66954225.5224552, 66884496.942257, 
66141094.4636408, 70173415.2476402, 71704017.9926114, 70496544.1935097, 
71763055.7070818, 70377922.1164147, 73928316.3138038, 72070160.4761618, 
69933750.0218853, 69364573.5347625, 70559233.6282076, 72231321.3445194, 
73615408.6904654, 72792532.1826755, 73994387.3091113, 71040270.6731584
), A_pct = c(-0.0823041788099015, -0.175274509686838, -0.174662505070068, 
-0.174700426948242, -0.176509673308039, -0.112137496689676, -0.120500996736812, 
-0.124184375630025, -0.130238880404948, -0.135640115594826, -0.136599002523788, 
-0.145928720524406, -0.148055404619703, -0.0610881900058278, 
-0.0556636208600172, -0.0650568862629995, -0.0538523460972125, 
-0.0478105279162245, -0.0377784299867026, -0.036090576333206, 
-0.0278825925587531, -0.0217119620439649, -0.0128007683689193, 
0.00159033094738859, -4.94648165600964e-05, 0.0138072516264973, 
0.0100294997550716, 0.0112052170591714, 0.0164117777072634, 0.0192579203247993, 
0.0235434832759449, 0.0284411373940252, 0.0308558561138499, 0.0319602045652567, 
0.0212686767046637, 0.024636350957765, 0.023397306770472, 0.0209553499863369, 
0.0266451817323854, 0.0302342450654525, 0.0215896085121354, 0.0216104033000564, 
0.00969670169841019, 0.0053688104692986, 0.000619977520391446, 
-0.00104041490748674, 0.000449034950299243, -0.00384608622919214, 
0.00174704874867548, 0.00572070946640579, -0.0101668694640196, 
0.00170684304709989, 0.000176493363683639, 0.00978462400922253, 
0.00646301099583302, 0.00693685040636693, 0.00583475741600981, 
0.00540633276801739, 0.00732258675695374, 0.0178959373052578, 
0.00993796172743355, 0.00514493068509636, 0.0168423347873017, 
0.0115808037193832, 0.00604136448998149, -0.00378742011365685, 
0.00288703134320034, 0.0014762400462347, 0.00674080122496945, 
0.00440108443777398, 0.00405088158783795, -0.00357625743135315, 
0.00225583440584942, 0.00688453992857108, 0.00593658313484013, 
0.0132097746024986, 0.022788627698811, 0.0231786954407416, 0.0232245466547674, 
0.030064449572083, 0.039162616962362, 0.0295334476368114, 0.0309589347554666, 
0.0278386637084538, 0.0210964471996316, 0.0252987686650823, 0.0302155657073593, 
0.02148738709815, 0.0154439648692524, 0.0187146212981921, 0.0106690120819346, 
0.0104780818291585, 0.00403455687745535, 0.0222075629161336, 
0.0303829516588823, 0.0264078415008373, 0.0408876074090199, 0.041125853928887, 
0.0359317256140435, 0.0349013489802104, 0.0354815037136171, 0.0388403017651922, 
0.0446661923400156, 0.0409833738719341, 0.0380503058833934, 0.0409353639369847, 
0.0325316462793606, 0.0487253573730662, 0.034038472888646, 0.0215671400830528, 
0.0199275489302968, 0.0236102882918339, 0.0198567688937654, 0.0151546138026175, 
0.0211175859866037, 0.0224400848914721, 0.0220104037038598, 0.0179847273720553, 
0.0078400013202462, -0.00313692609389338, 0.00815098235840739
), B_pct = c(-0.165446574859147, -0.112764415998189, -0.166200148642091, 
-0.227602397560488, -0.0638113015895933, -0.188987036514848, 
-0.206797066844619, -0.153006465894801, -0.108548496719733, -0.0588379655763306, 
-0.22155605118564, -0.179032287010621, -0.074540913488812, -0.153907484951643, 
-0.00701338575482069, -0.10251978919658, -0.111839239745077, 
0.0212428516213228, 0.0189346414499814, -0.0366583918974162, 
-0.0473316204294489, -0.103816487186215, -0.0393487897100808, 
0.00470283016762418, -0.0488728797096665, 0.0222117516135814, 
-0.175616094670833, -0.0509028456921267, -0.0180525584311335, 
-0.0912633933191832, -0.0639926574745853, -0.0390923275837387, 
-0.033783200844, -0.0135884601949543, -0.00892857108400291, -0.0880439073913108, 
-0.0228693418522856, -0.0568433306735971, 0.0149298997664649, 
0.0650249278432764, -0.0296819065838376, 0.0383622644668125, 
-0.0519492284684971, -0.103111211902055, -0.0379793817258227, 
0.00409931304692179, -0.0163478710176814, 0.021963353475281, 
-0.0127375178730527, -0.0294592184375722, -0.0482682503975804, 
0.000464370685903548, -0.0043832805687184, 0.0163082388404143, 
0.0711030275816249, 0.124052131739314, 0.0375898291735246, 0.00575700933122447, 
0.0391577859415262, 0.0614552627019698, -0.00505138872067812, 
0.0229754458149609, 0.0980908311592417, -0.0304981534678544, 
-0.022903231168225, -0.0164778138922544, -0.0726238530777905, 
-0.0270631829316963, -0.00424008586521596, 0.00410557918908516, 
-0.00218646981458837, -0.0385722301362951, 0.00131006123345134, 
0.0281710312821462, 0.0137026664058273, 0.0829804179647597, 0.101692460502704, 
0.0328036917072969, 0.102465298167474, -0.00475120533171702, 
0.0187782853677325, 0.0132844571583981, 0.010543148971764, 0.00127881556485443, 
0.0846016656201194, 0.0396948695339499, 0.0623837354224588, -0.00108399792298763, 
-0.0372709571469861, 0.061462697495251, 0.0121111301361809, 0.0848818602482577, 
0.037216695420448, 0.0392524252823885, 0.043587864191823, 0.0424053385597367, 
0.0160003795324501, 0.033882491839905, 0.0166442167574858, 0.0684263564333132, 
-0.000679752347271245, -0.0415021595516842, 0.0427770243561332, 
-0.00809710066994662, 0.00831145879397299, -0.00653002841600525, 
0.0575896181201949, 0.105556343878073, 0.040587442453804, 0.0664926208007302, 
0.0258918808089446, 0.0514548191948268, 0.117027529483372, 0.0584246721248054, 
0.00137675807939708, 0.0538428766462744, 0.0799411619538448, 
0.113005602453909, 0.0373234924621024, 0.031941994055841, 0.00771281040608462
)), row.names = c(NA, -121L), class = c("tbl_df", "tbl", "data.frame"
))

1 个答案:

答案 0 :(得分:2)

您可以为geom_line传递虚拟填充变量(默认情况下,它们没有此变量,但是我们将使用一个填充图例)。接下来,您必须在填充和删除颜色图例中设置所有三个值。

ggplot(df, aes(x = Date)) + 
    geom_area(aes(y = B / 1000000, fill = "B")) + 
    geom_line(aes(y = (A_pct * m) + b, color = "A % Chg", fill = "A % Chg"), size = 1.5) + 
    geom_line(aes(y = (B_pct * m) + b, color = "B % Chg", fill = "B % Chg"), size = 1.5) +
    scale_color_manual(values = c("red", "blue"), guide = FALSE) +
    scale_fill_manual(values = c("red", "yellow", "blue"),
                      name  = "Series")

enter image description here