gpt4 book ai didi

使用多个数据框从图中的ggplot图例中删除不正确的彩色轮廓?

转载 作者:行者123 更新时间:2023-12-04 08:14:39 25 4
gpt4 key购买 nike

我已经从多个数据帧中创建了一个 ggplot。其中一个数据框用于绘制填充的多边形/矩形,而另一个用于创建等高线图。如果我在等高线图元素中指定一种颜色,它会在多边形的图例中显示为轮廓(而不是绘制的多边形本身的轮廓)。
如何仅从单个不相关的图例元素中删除轮廓?
可重现的例子:

df1 <- data.frame(
person = c("Avery", "Doug", "Avery", "Doug", "Avery", "Doug", "Avery", "Doug"),
place = c("A","A","B","B","C","C","D","D"),
Coord1 = c(10, 30, 70, 90, 70, 90, 10, 30),
Coord2 = c(70, 90, 70, 90, 10, 30, 10, 30)
)

df2 <- structure(list(place = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D"), Coord1 = c(11.9321233389815,
5.65317891793307, 19.1326709658932, 27.9338809206353, 19.9660510683355,
21.0749048744809, 7.21913717149466, 20.1920548762508, 10.4543717875714,
25.757795926256, 12.1828845087838, -0.465353420455905, 7.11311351850768,
14.8155028526132, -2.1637642702189, 13.0813689628972, 4.7982168502595,
5.57686199262573, 4.00687188024056, 22.9457782880066, 18.3539124739061,
4.33984900298331, 17.8841935003471, -1.65929325512088, 4.69179993740943,
42.6151590341647, 27.615643609024, 10.0218739210389, 37.0107595807328,
42.2081901427891, 21.4907606643235, 24.3639224345837, 32.772343673693,
39.707188961395, 39.526308329454, 26.0304665678886, 29.9780452839205,
26.3307539307013, 30.3989518780307, 48.3076387197071, 18.9469498637539,
20.2952072925756, 16.6669862005188, 37.4700660304315, 17.8662145857614,
37.2600750819877, 25.7007521883461, 28.6742428655807, 40.314957702891,
30.0697139406434, 73.6583322470224, 79.3142178409643, 50.9322840242791,
70.5198852398892, 61.5150022794989, 66.3183258649759, 76.2469408002744,
82.2025917335132, 47.6021718929377, 66.3132462976689, 69.1291434239167,
83.4197156933065, 86.9407992718112, 76.4950208109435, 73.661826873959,
66.9162963609677, 75.1456713179674, 60.9887903971435, 71.9565881057142,
68.4266063305417, 57.6911530548141, 74.7773517795878, 69.6101844803799,
67.790228250546, 69.3010177449088, 102.70586821213, 70.9690781293641,
81.8211641205855, 89.1142208240172, 103.752341186373, 89.5593092939362,
80.6305724472789, 82.0007363853625, 65.8943427952359, 78.3936032524407,
83.0803062787287, 92.1760929466364, 78.9701119884041, 86.8932236845231,
97.1763793934393, 84.058342477451, 93.7849946833988, 83.1484361525486,
80.1491902221492, 85.9961991961048, 93.2550659070971, 78.3156355536891,
94.2129927101896, 93.4865526165191, 93.4934076661858, 75.4629285622502,
47.6731852393923, 72.88284171046, 75.438344587857, 58.0801523419401,
69.8230768794789, 60.5302214082446, 65.9778757219936, 70.5612025149866,
68.4370521177005, 58.1149422572855, 82.8226830631184, 65.7390566694014,
65.6285158427551, 64.1736763473425, 93.616329884223, 60.1827131711359,
48.6282110427849, 66.7586165042503, 75.5318338818184, 67.3206665449225,
59.0440215809036, 51.0189160405021, 84.6055490872487, 54.892120364794,
97.5277806090821, 87.3463009673921, 102.066058009738, 82.0203143022486,
90.9586925853003, 88.6949600210823, 103.884302040987, 87.4998069331291,
76.3591677407161, 81.8943250717613, 100.824382142449, 102.946636154418,
81.8333852359431, 94.7599932319231, 93.7316328376966, 79.8039952376426,
92.3881270619707, 71.2558895720234, 99.8119134521082, 112.401228355728,
83.0814548497863, 94.0539152929977, 79.4016592347326, 85.7829067433969,
75.948295755515, 3.73649805560632, 23.0903794794387, 8.67023149246646,
-6.07704226517408, -3.07641645158295, 9.54851319649848, 17.8889045992558,
5.10657758386499, 18.7016029853773, 25.4522147195668, 6.8023795377009,
15.3767651131782, 20.3683653225209, -20.3254139776893, 15.6691879496879,
9.45068374562864, 18.7361628847212, 12.623384426461, 10.17849269891,
-16.9581826824957, 17.4909319409575, 9.44644950565353, -5.38702617407262,
6.21805297402208, 14.1749418466783, 12.9105548124602, 27.1855440225952,
29.8482741922039, 29.7496920817558, 38.524389832422, 33.7023405759537,
45.3217768361544, 40.1308769909943, 34.4402395511235, 25.068583150376,
26.2353756696678, 21.8111750232141, 33.8629499457315, 29.7131703823705,
34.1556614684988, 34.1245105271038, 18.3147946286069, 27.3066155417929,
26.5207184421978, 34.0884687114446, 26.639481645307, 38.6173778182637,
37.8098024973006, 13.0741049202031, 45.5358046032432), Coord2 = c(69.9894126325745,
64.8743763485989, 82.4286751251657, 63.3941714859759, 71.6662421480457,
64.4679475985789, 70.9818141479016, 58.5333414006127, 57.5007274347188,
67.978883174351, 71.7750032484243, 83.0764732083471, 56.5706459545147,
78.6108303958077, 72.3287500312082, 80.4225709350104, 83.2909825415992,
79.4595795345076, 60.1437449584155, 57.8413316685676, 53.7079406423028,
70.9231623851271, 69.7788257690901, 75.1347791237738, 67.7044636198011,
94.3340903929599, 102.873693143552, 65.8173572622545, 76.2734860629939,
85.2060002479906, 90.1101352051085, 96.2874409924653, 109.760633210447,
94.6155820846595, 95.2601614162185, 69.4981680111657, 84.4916642548314,
84.207250490172, 95.2814397728582, 88.6024629127233, 105.45937855117,
101.73736873413, 96.3994065298907, 89.5148162082444, 85.656552291334,
87.1768687739584, 103.397297430617, 100.466406474931, 97.492149943699,
95.7799794375843, 58.689787023173, 91.7856309013427, 70.284597504488,
82.5264495002628, 72.563622499658, 67.9271039063317, 76.9752642043184,
77.0427295652995, 60.493215584824, 71.9177768464766, 85.2536338000138,
78.6037513000414, 79.9167528070661, 45.2538960490244, 73.7270619632537,
74.1775039120291, 79.3753619281975, 72.5802290852752, 77.6589441840394,
65.0442876775209, 75.9474471250248, 67.6324983845202, 66.7269504282668,
61.4684933637993, 84.9370241657568, 100.807408193669, 90.6253499380336,
82.4392258080415, 85.978657057683, 85.5556977940698, 80.0069018830692,
92.0205306026153, 92.2799374212157, 92.075887951686, 84.046647740422,
95.2469867954641, 90.4378832940894, 103.868141681032, 96.1678027516943,
97.556915790983, 75.8136031118073, 87.269403250105, 102.176170079899,
83.303319611484, 77.0422898434913, 94.9522487152654, 93.5056368803038,
99.6066565241826, 93.2540063907868, 75.3235503045194, 4.79683173913879,
-1.77333441979239, 22.2927646778249, 21.2002682830845, 9.2462559574228,
12.9328717922413, 9.71104299233956, 6.02183609528862, 16.5548144989934,
14.9138467115507, 26.4290598776307, 13.2859100121563, 24.6139296632551,
6.01920473648369, 6.44403579988305, 15.0356163044265, 14.0083469209857,
-0.983183926446912, 11.305792234271, 17.6465875981944, -8.94856332381213,
7.36961719889383, 7.92121709811615, -0.418766330916343, -5.42220613778122,
33.1278663709957, 22.5146385498018, 46.6482380411691, 21.1315055557743,
23.3535222367336, 37.0589046029771, 20.5508910389587, 34.3154424989857,
37.5906644856971, 28.9704448512218, 30.6411807554989, 35.4051444540154,
30.0311466912122, 28.7535914372174, 28.4106840578911, 36.8662136858688,
12.898132148862, 46.7570642830409, 30.8475454612592, 19.28210909481,
19.5978468800625, 34.6039683478465, 21.0831790287953, 34.2925742059241,
53.7786968993373, 19.0172370755723, 9.21397428746006, -2.37886313478754,
6.79453923040543, 22.6413009302484, 33.2099355799597, 6.58849285636098,
-11.7836723204909, 5.86738511098067, 3.81888634358306, 20.5825579992258,
9.8827774793369, 14.4441862792073, 12.3532766231854, 10.2334773998435,
22.8131908769118, 23.8680133376392, 9.59780868195676, 24.4068082968655,
11.2009206443726, 13.4244085452577, 22.0625356242069, 12.2526091738859,
-2.39754291555823, 2.00084520595361, 36.7097665883357, 16.9122643222195,
20.0379767031296, 41.4658882877744, 24.0612860080661, 24.7786616325539,
33.9205960391195, 32.0684115690426, 26.5208653130137, 47.3248746356669,
37.8352815877295, 42.4135950414131, 33.8021211637222, 28.309616360779,
24.4645546802529, 32.9897820259554, 17.8194684139265, 36.2084800081616,
37.6500899883031, 22.4961071408182, 47.6368585912597, 57.0362400846021,
43.7894593761563, 23.2634280784332, 54.7477709366998)), row.names = c(NA,
-200L), class = c("tbl_df", "tbl", "data.frame"))

ggplot() +
stat_density_2d(data = df2,
aes(x = Coord1, y = Coord2, group=place, shape="Yuki"),
geom = "polygon", contour = TRUE, colour="darkorchid3", bins = 7, alpha=0.2, show.legend=TRUE) +
geom_polygon(data = df1, aes(x = Coord1, y = Coord2, fill = person), alpha = 0.6) +
geom_point(data = df1, aes(x = Coord1, y = Coord2), size = 1.5, show.legend=FALSE) +
geom_text_repel(data = df1, aes(x = Coord1, y = Coord2, label = place), colour="black") +
ggtitle("Combined Plot", subtitle = "Avery, Doug, Yuki") +
labs(fill = "Person", shape = "") +
guides(shape = guide_legend(order = 2), fill = guide_legend(order = 1))

上面的代码产生了这个图:
imgdesc: a ggplot generated from the cold listed above, representing two filled rectangles and a collection of contour plots
这就是我想要的样子(将第二个元素向上移动或以其他方式组合图例会很棒,但有点超出我的问题范围。)
imgdesc: A plot similar to the one generated by the code above, except the purple outline around two of the three legend elements has been removed. The space between the elements is also even.
我尝试过的事情:重新排列元素,强制在 geom_polygon 中调用其他颜色,并使用额外的元素来尝试掩盖轮廓。我所实现的最接近的是从 stat_density_2d 中删除“颜色”代码从图例中的所有元素中删除轮廓,但它也从绘图本身中删除了轮廓线,这并不理想。从 stat_density_2d 中删除“虚拟”形状 aes 会从图例中删除等高线图,但不会从其他图例元素中删除轮廓。在这种情况下,组合数据帧也不是理想/可行的解决方案(抱歉)。
我的实际绘图还使用自定义 scale_colour 和填充值,这些值不容易包含在最小可重现示例中。出于这个原因,我在尝试使用分配给这些的自定义标签方面还没有走得太远,如果有一个不依赖于 scale_color_manual 等的解决方案,我会很感激。但是,我也知道乞讨者不能选择和我将不胜感激!

最佳答案

将图例放在正确的布局中,但在边界方面做任何事情似乎真的很棘手。我没有使用您的颜色,而是使用 white颜色仍然独特但不会显示在图例边框上。

fill_color <- c("green", "red", "#444444")
names(fill_color) <- c("Avery", "Doug", "Yuki")
line_color <- c("green", "red", "darkorchid3")
names(line_color) <- c("Avery", "Doug", "Yuki")


ggplot() +
stat_density_2d(data = df2,
# Here using the fill aes instead of shape aes like your
# original input which cause the legend was put into
# two different categories.
aes(x = Coord1, y = Coord2, group=place, fill = "Yuki", color="Yuki",),
geom = "polygon", contour = TRUE,
bins = 7, alpha=0.2, show.legend=TRUE) +
geom_polygon(data = df1, aes(x = Coord1, y = Coord2, fill = person, color = person),
colour = "transparent", alpha = 0.6) +
geom_point(data = df1, aes(x = Coord1, y = Coord2), size = 1.5,
show.legend=FALSE) +
geom_text_repel(data = df1, aes(x = Coord1, y = Coord2, label = place),
colour="black") +
ggtitle("Combined Plot", subtitle = "Avery, Doug, Yuki") +
scale_color_manual(values = line_color, guide = FALSE) +
scale_fill_manual(values = fill_color) +
labs(fill = "Person", shape = "") +
guides(shape = guide_legend(order = 2), fill = guide_legend(order = 1)) +
theme(legend.key = element_rect(fill = NA))
enter image description here
[使用 scale_fill_manual 更新操作调色板和 scale_color_manual ]

关于使用多个数据框从图中的ggplot图例中删除不正确的彩色轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65767760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com