在合并图例R ggplot2中创建混合标签类型

时间:2019-02-07 23:51:28

标签: r ggplot2

我有一张图表。

Chart

如您所见,我右边有一个图例。这个传说几乎是我想要的,但事实并非如此。我想从结束标签中删除黑线,但保留黄色背景。最好,我想在图表的黄色区域保留黑色边框,但是没有边框也可以。对于价格标签,是否有办法将边框变成黑色或完全删除边框,同时使绿线覆盖在白色背景上?

几乎所有图表都是完美的,但是需要对图例进行调整,以显示黄色的实心框和仅带有绿线的第二个框。该怎么办?

我看着: How to merge color, line style and shape legends in ggplotRemove lines from color and fill legends,但两种情况都与我的有些不同。

这是我的代码:

library(ggplot2)
ggplot(data = df, aes(x = Date)) + 
     geom_area(aes(y = Closings, color = "closings", fill = "closings")) + 
     geom_line(aes(y = (Price * 0.001333333) + 83.33333, color = "price", fill = "price"), size = 1.5) + 
     scale_fill_manual(values = c("yellow", "white"),
                       name  = NULL,
                       breaks = c("closings", "price"),
                       labels = c("Closings", "Median Price")) +
     scale_color_manual(values = c("black", "green3"),
                        name  = NULL,
                        breaks = c("closings", "price"),
                        labels = c("Closings", "Median Price"))

这是我的数据:

df <- structure(list(Date = structure(c(13879, 13910, 13939, 13970, 
14000, 14031, 14061, 14092, 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, 17805, 
17836, 17866), class = "Date"), Closings = c(250.156142028049, 
278.357976437638, 255.121081362418, 283.702250084887, 283.28935185905, 
288.313623559134, 325.000699725773, 298.376264701902, 313.84570882404, 
305.210255253672, 325.167893489321, 306.987145839372, 346.917797496066, 
382.32544168682, 366.043189727097, 395.528885200985, 413.192739388789, 
436.198713316806, 448.857236597506, 458.12844469499, 467.934456709454, 
531.718551380374, 489.036029651921, 424.145436933803, 480.771931684385, 
424.642009395637, 456.026852374217, 481.774827399362, 438.07838530662, 
497.787244232307, 378.461055105199, 406.736455324577, 417.379172567169, 
421.576783421227, 405.028778695547, 478.191650757529, 496.894754901524, 
478.609660794221, 483.529758706635, 424.061194075769, 476.069214422593, 
436.292678831308, 482.080852838583, 471.208298573889, 521.57277412691, 
494.662515658451, 502.577814463075, 545.566636089223, 561.365872327693, 
556.269339067741, 517.116503763691, 524.479775060783, 465.628788487937, 
439.644235427938, 470.021446447943, 461.632774011286, 474.547410701393, 
499.253296310819, 527.224782494392, 530.539320943973, 462.958529914718, 
513.983733770454, 501.597790930051, 462.552111079358, 484.16759628069, 
466.85148846189, 521.003988379343, 564.00372012142, 488.49423201669, 
519.494492425345, 504.576220483446, 506.872226824766, 465.856317160947, 
489.538676300571, 484.663919536111, 506.956908536912, 492.454946762983, 
487.778557225938, 449.629197246976, 537.191437830848, 464.109069017065, 
470.628629323684, 531.051855365986, 516.884471679629, 504.345731330288, 
508.948061702574, 532.078717023471, 536.566373647872, 546.867242773361, 
537.462592066804, 559.77142834591, 481.207575398739, 527.649185787963, 
542.34239711043, 468.577086880184, 509.001969646315, 554.596640239335, 
490.868639064067, 543.728816654576, 565.291400229651, 541.137354666698, 
544.001727012899, 548.287267385355, 507.783353384915, 593.536224116433, 
527.284368523773, 551.142049002881, 554.652683077533, 510.866876699988, 
505.764845738803, 527.46665705407, 541.968694157968, 603.45118314086, 
635.9132068477, 595.556380657524, 637.053092773872, 584.306260267669, 
558.432266359433, 549.508951330324, 539.224102652134, 536.229486076422, 
560.825854108347, 541.44659261869, 504.134585438331, 500.388810459863, 
491.701638680997, 500.007728524597, 471.598923505976, 457.300388732037, 
455.337353120735, 486.826956528693, 442.941766916031), Price = c(292992.425757596, 
282232.785900843, 275389.834275573, 274500.109731967, 264944.862222162, 
261706.592852482, 250524.250644742, 246374.074631546, 236004.574847187, 
228285.291130621, 220701.939675238, 216305.664446331, 205035.996752354, 
203592.420113099, 199684.648674524, 187699.916943355, 179863.305040014, 
181980.437767862, 179178.492676046, 176522.983603004, 183127.367236221, 
177936.314209299, 175390.86606603, 176969.80397964, 176026.327673378, 
175530.448632218, 176299.76511108, 179692.051095226, 178283.932334544, 
170194.461549687, 179486.464906891, 176025.94108693, 168185.697003624, 
173020.834245636, 169824.68021547, 162447.460927453, 166098.217547117, 
163039.500372587, 162340.60264281, 156001.800941735, 150449.501999813, 
152175.301772514, 148901.922981959, 149962.9572839, 148757.390734935, 
147836.111243281, 148837.237231223, 154563.483637924, 144609.002728569, 
148631.456513129, 152049.688131618, 152688.38636814, 162291.648555056, 
165499.626692532, 164806.348626159, 172475.342634516, 177480.532304788, 
180502.70381349, 181780.140013274, 186672.049412382, 190398.174597777, 
195210.819465264, 198620.803584801, 209688.172883621, 215301.05071958, 
216278.952605624, 212767.347684501, 217001.945710031, 225505.722191665, 
216130.134875928, 224376.4250221, 230125.402991916, 230187.01087712, 
236624.460163686, 230666.364352779, 234136.190763021, 235386.840124055, 
244509.870077768, 245311.76865067, 247252.467065234, 241738.590522588, 
263681.322834618, 262277.080161105, 264874.07429538, 265297.097202708, 
266977.787381232, 273185.299005992, 274533.498265018, 279723.711000575, 
275027.717854781, 283521.001985551, 287024.425436969, 287516.68750296, 
281696.446951941, 293216.445551162, 295730.559661054, 289153.384805077, 
292289.800405012, 298643.411790304, 297201.252740491, 304510.476947687, 
307320.451499811, 307592.951343233, 307256.835722165, 313926.00850201, 
311359.700058026, 310415.82927781, 308216.275525795, 314244.98505743, 
321236.469132338, 320365.61567768, 326585.503708381, 332923.652409942, 
334522.324923418, 345243.080421173, 348985.167214431, 341145.875527968, 
350041.52839689, 355126.587056377, 355585.619798078, 366778.863859834, 
370153.357953846, 375567.043369724, 375540.363471684, 372658.306591577, 
379744.740611563, 381864.717432884, 371316.907731576, 381175.129326891, 
383818.35305956, 380150.276063892, 375297.84684425)), row.names = c(NA, 
-132L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

尝试使用表的前几行,这至少会从“关闭”图例中删除该行。

主要变化是将geom_areaaes上的黑色边框移到labels之外,这意味着它不会产生图例行。另一个更小的技巧是,因为无论如何您都在使用带有手动值的手动比例尺,因此只需中断所需的标签就可以跳过指定library(tidyverse) df <- structure(list(Date = structure(c(13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 14123, 14153), class = "Date"), Closings = c(250.156142028049, 278.357976437638, 255.121081362418, 283.702250084887, 283.28935185905, 288.313623559134, 325.000699725773, 298.376264701902, 313.84570882404, 305.210255253672), Price = c(292992.425757596, 282232.785900843, 275389.834275573, 274500.109731967, 264944.862222162, 261706.592852482, 250524.250644742, 246374.074631546, 236004.574847187, 228285.291130621)), row.names = c(NA, -10L), class = "data.frame") ggplot(df, aes(x = Date)) + geom_area(aes(y = Closings, fill = "Closings"), colour = "black") + geom_line(aes(y = Price * 0.001333333, colour = "Price")) + scale_fill_manual(values = "yellow", breaks = "Closings", name = NULL) + scale_colour_manual(values = "green", breaks = "Price", name = NULL) 。现在,价格图例的背景将成为图表的背景,您可以使用主题对其进行修改。在Price图例中获得黑色边框可能会有些棘手,因为在ggplot2图例中它是有意义的。

if($_SESSION['tipo_usuario']){
 $tipo_usuario = $_SESSION['tipo_usuario'];
}

reprex package(v0.2.1)于2019-02-07创建