Reputation: 627
I'm plotting a bar graph and a line/point graph on the same plot and I'm trying to have two legends, one for the points and one for the bars.
However, I end up with 3 legends (instead of two) and the legend for the bars has points in it. Can you help me? Here is a MWE
library("ggplot2")
df2 <- data.frame(Height=rep(c("low", "high"), each=3),
Month=rep(c("Jan", "Feb", "March"),2),
Trial =c(7, 4, 10, 2, 4, 12),
Success = c(2, 2, 7, 1, 3, 8))
ggplot(data=df2, aes(x=Month, y= Success, fill= Height)) +
ggtitle("Title") +
geom_bar(stat="identity", position=position_dodge()) +
geom_line(aes(x=Month, y=Success/Trial*max(Success), group = Height, linetype = Height, colour = Height) , size = 3, alpha = 0.7) +
geom_point(aes(x=Month, y=Success/Trial*max(Success), colour = Height), size = 11) +
geom_text(aes(label=round(Success/Trial,2), x=Month, y=Success/Trial*max(Success)), color="white", size=3.5, fontface = "bold") +
scale_y_continuous(sec.axis = sec_axis(~. *1/max(df2$Success) , name="Proporion of success")) +
labs(x = "Month", y="#Success", fill = "Number of Success", colour = "Proportion of success") +
scale_colour_manual(values=c('#909999','#E69000'))+
scale_fill_manual(values=c('#099999','#069F00'))
I tried using different versions of guides(colour = FALSE, group = FALSE, fill = FALSE)
but with no success. I want to remove the "Height" legend, and remove the big black dots in the "Number of success" legend.
Upvotes: 1
Views: 1777
Reputation: 206606
You are getting three legents because you are mapping color, fill, and linetype. So you want to turn off the guide for linetype and remove the shapes from the fill legend. You can do that with
guides(
linetype = FALSE,
fill = guide_legend(override.aes = list(shape = NA))
)
Upvotes: 3