23

I have a plot with three different legends: one for linetype, one for color, and one for fill. In the color and fill legends there are also some lines which I wish to remove, but how?

Here is some example code:

# some data
hline_df <- data.frame(name = c('a', 'b'), y = c(1, 2))
df <- data.frame(x = c(1, 2), y = c(0.5, 1.5), con = c('a', 'b'), col = c('d', 'e'))

# the plot
ggplot(df, aes(x, y, fill = con)) +
  geom_bar(stat = 'identity') + 
  geom_point(aes(color = col)) +
  geom_hline(data = hline_df, aes(yintercept = y, linetype = name),
             color = 'red', show_guide = TRUE)

enter image description here

I get the "name" guide for both red lines, that is fine.
The "col" guide has red lines crossing the dots, I want to remove them!
The "con" guide also has red lines which should be removed.

I could modify parts of the legend with

guides(fill = guide_legend(override.aes = list(colour = NULL)),
       color = guide_legend(override.aes = list(colour = NULL)))

This removes the colour, but the lines are still there.

Thanks in advance!

Henrik
  • 65,555
  • 14
  • 143
  • 159
drmariod
  • 11,106
  • 16
  • 64
  • 110
  • Hm, rearranging to `ggplot(df, aes(x,y,fill=con)) + geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + geom_point(aes(color=col)) + geom_bar(stat='identity') + geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F)` seems to work for `con` but the red line is still in `col`. And to be honest, I don't understand, why it is working :-) – drmariod Apr 29 '15 at 11:30
  • 1
    Ahhh, I tried to set `linetype=NULL` and this didn't worked as expected... Also the trick with plotting the hline two times, on in the back and one in the front is great! Would you like to post an answer, so I can mark it as fixed? – drmariod Apr 29 '15 at 11:46

3 Answers3

32

You may set linetype = 0 or "blank" (on different linetypes here) for the filland color guides in your override.aes call.

Also note that I moved the fill aes from the 'top level' in ggplot to geom_bar.

ggplot(df, aes(x, y)) +
  geom_bar(aes(fill = con), stat = 'identity') + 
  geom_point(aes(color = col)) +
  geom_hline(data = hline_df, aes(yintercept = y, linetype = name), color = 'red', show_guide = TRUE) +
  guides(fill = guide_legend(override.aes = list(linetype = 0)),
         color = guide_legend(override.aes = list(linetype = 0)))

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Thanks, this is even a bit cleaner and explains the problem... It just sets the aes linetype to 0 for the fill and the color legend... Great! – drmariod Apr 29 '15 at 13:26
  • Excellent answer. I was working on a solution with extracting the several legends and then trying to put them together into one plot. However, this is a much better and much more elegant solution! +1 – Jaap Apr 29 '15 at 13:42
5

As suggested by user20650

ggplot(df, aes(x,y)) + 
  geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=TRUE) + 
  geom_point(aes(color=col), size=5) + 
  geom_bar(aes(fill=con), stat='identity') + 
  geom_hline(data=hline_df,aes(yintercept=y,linetype=name), color='red',show_guide=F) + 
  guides(color = guide_legend(override.aes = list(linetype = 0)))

So the first geom_hline creates the legend but the line is behind the bars...
the second call brings the line in front of the bars but does not print a legend (great idea).
The las guide is overwriting the aesthetics line type with 0... In this way it removes the line from the legends... I tried with NULL but this didn't worked before...

Thanks again.

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
drmariod
  • 11,106
  • 16
  • 64
  • 110
3

Using:

ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') + 
  geom_point(aes(color=col)) +
  geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE) +
  guides(linetype=FALSE,color=FALSE)

gives me this plot: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Thanks @Jaap, but I want to have the all guides... Just want to remove the red line from the `con` and the `col` guide... – drmariod Apr 29 '15 at 11:18
  • 1
    So, basically `ggplot(df, aes(x,y,fill=con)) + geom_bar(stat='identity') + geom_point(aes(color=col)) + geom_hline(data=hline_df,aes(yintercept=y,linetype=name),color='red',show_guide=FALSE)` is the expected result for `con` and `col`, but I need a guide for the red lines as well... – drmariod Apr 29 '15 at 11:23