4

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

enter image description here

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:

enter image description here

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

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 4
    Since the `colour` legend only affects the lines, you could do: `guides(colour=guide_legend(override.aes=list(shape=NA)))`. – eipi10 May 30 '17 at 22:47

1 Answers1

3

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

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks, that would fit the bill. Can you explain the logic here? I'm not understanding how the `scale_fill_discrete` function fixes this. – Phil May 30 '17 at 22:30
  • 1
    Afaik, ggplot2 tries to combine scales with the same name (= normally the mapped variable name). Here I renamed the scales explicitly to get three instead two; then I specified not to display the 3rd one. – lukeA May 30 '17 at 22:33