简体   繁体   中英

Removing ggplot2's geom_point icons from the legend

Note: This is a follow-up to this question .

First the code to reproduce the data:

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)

When I'm running the following ggplot2 code, I end up with this:

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

在此输入图像描述

The problem here is that the black dots that take up the answer legend key. I attempted to remove it using the following layer:

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

And I get this:

在此输入图像描述

I'm still getting a tiny black dot in the legend. What else can I do to remove it entirely?

You could do

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM