How can I modify a legend in ggplot2 to suppress a line overlaying a symbol?

84 Views Asked by At

I'd like to plot two series as points, and add a smooth for each series. I'd like the legend to display the symbols. The unwanted behavior is drawing lines through the symbols in the legend (as result of the geom_smooth).

How can I get the lines out of the legend?

Here's an example:

library (ggplot2)


df = data.frame (x=1:6, y = c(1,4,3,8,5,12), z = c(1,2,1,2,1,2))
df$z = factor(df$z)

p = ggplot (df, aes(x=x, y=y, shape = z, color=z))
p = p + scale_color_manual (values = c("green", "blue"))
p = p + scale_shape_manual (values = c(16,17))
p = p + geom_point(size = 6)
p
#--legend is perfect
p = p + geom_smooth(aes(color=z))
p
#--symbols in legend have superimposed lines
1

There are 1 best solutions below

0
On

Set show_guide = FALSE:

p + geom_smooth(aes(color=z), show_guide  = FALSE)