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)
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!
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 forcon
but the red line is still incol
. And to be honest, I don't understand, why it is working :-) – drmariodlinetype=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