gpt4 book ai didi

从图例中删除 ggplot2 的 geom_point 图标

转载 作者:行者123 更新时间:2023-12-01 01:51:22 28 4
gpt4 key购买 nike

注意:这是对 this question 的跟进.

首先是重现数据的代码:

mydf <- data.frame(year = c(rep(2000, 3), rep(2002, 3), rep(2004, 3), rep(2006, 3), rep(2008, 3), rep(2010, 3), rep(2012, 3), rep(2014, 3), rep(2016, 3)),
answer = rep(c("A great deal", "Hardly any", "Only some"), 9),
result = c(0.3015940, 0.1399303, 0.5584757, 0.2269548, 0.1792754, 0.5937698, 0.2955301, 0.1309859, 0.5734840, 0.3008197, 0.1344499,
0.5647303, 0.1919454, 0.2026290, 0.6054256, 0.1059793, 0.4190533, 0.4749674, 0.1190636, 0.3631279, 0.5178085, 0.1518314,
0.3181203, 0.5300483, 0.1424715, 0.3094615, 0.5480669))
mydf$year <- factor(mydf$year)
mydf$answer <- factor(mydf$answer)

triangles <- data.frame(year = c(2002, 2004, rep(2008, 2), rep(2010, 3), 2012),
answer = c(rep("A great deal", 3), "Hardly any", "A great deal", "Only some", rep("Hardly any", 2)),
direction = c("Decrease", "Increase", "Decrease", "Increase", rep("Decrease", 2), "Increase", "Decrease"),
result = c(0.2269548, 0.2955301, 0.1919454, 0.2026290, 0.1059793, 0.4749674, 0.4190533, 0.3631279))
triangles$year <- factor(triangles$year)
triangles$answer <- factor(triangles$answer)
triangles$direction <- factor(triangles$direction)

当我运行以下 ggplot2代码,我最终得到了这个:
ggplot() + 
geom_line(data = mydf, aes(x = year, y = result, colour = answer, group = answer)) +
geom_point(data = triangles, aes(x = year, y = result, fill = answer, shape = direction), size = 3) +
scale_shape_manual(values = c(25, 24))

enter image description here

这里的问题是占据 answer的黑点图例键。我尝试使用以下图层将其删除:
guides(fill = guide_legend(override.aes = list(size = 0)))

我明白了:

enter image description here

我仍然在传说中看到一个小黑点。我还能做些什么来完全删除它?

最佳答案

你可以做

ggplot() + 
geom_line(data = mydf, aes(x = year, y = result, colour = answer, group = answer)) +
geom_point(data = triangles, aes(x = year, y = result, fill = answer, shape = direction), size = 3) +
scale_shape_manual(values = c(25, 24), name = "direction") +
scale_color_discrete(name = "answer") +
scale_fill_discrete(name = "c", guide = "none")

enter image description here

关于从图例中删除 ggplot2 的 geom_point 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272804/

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