Removing ggplot2's geom_point icons from the legend

问题

注意:这是对这个问题的跟进。

首先是重现数据的代码:

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))

这里的问题是占据answer图例键的黑点。 我尝试使用以下图层将其删除:

guides(fill = guide_legend(override.aes = list(size = 0)))

我明白了:

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

回答1

你可以做

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")

标签

更多相关内容:请点击查看