0

I want to put points on a map, but I want the legend to show only the fill. The code below shows the legend I want with plot1, but a point is overlaid on the fill in the plot2 legend. How can I get rid of that point in the plot2 legend? I want plot2 with the legend from plot1.

library(sp)
library(maptools)
library(rgeos)
library(ggplot2)
library(ggmap)


# prepare a map of USA and Canada
data(wrld_simpl)
canada.usa <- wrld_simpl[wrld_simpl$NAME %in% c("Canada", "United States"), ]

# fortify and add original data
can.us  <- fortify(canada.usa, region="NAME")
can.us2 <- merge(can.us, canada.usa@data, by.x="id", by.y="NAME") 

# first plot - legend looks perfect
plot1 <- ggplot(can.us2, aes(x=long, y=lat, group=group, fill=id)) + geom_polygon() + 
         coord_quickmap() + theme_nothing(legend=TRUE)
plot1 

# generate a simple set of two points
two.pts <- data.frame(gCentroid(canada.usa, byid=TRUE)@coords)

# add these two points to the original plot - not the legend I want
plot2 <- plot1
plot2 <- plot2 + geom_point(data=two.pts, aes(x=x, y=y, group=NULL, fill=NULL,  size=20)) + 
         guides(size=FALSE)
plot2
tonytonov
  • 25,060
  • 16
  • 82
  • 98
user13424
  • 111
  • 1
  • 4
  • Use `show_guide = FALSE` inside `geom_point`? [See here.](http://stackoverflow.com/questions/12746523/preventing-a-second-legend) – Axeman Jan 11 '16 at 13:30
  • 1
    Bingo! Actually `show_guide` has been deprecated. We must now use `show.legend`. So, including `show.legend=FALSE` in `geom_point` gave me what I wanted. Thanks. – user13424 Jan 11 '16 at 17:17
  • Ah yes, sorry, had an old version loaded. – Axeman Jan 11 '16 at 17:23

1 Answers1

1

Use show.legend = FALSE to suppress the legend for a geom.

ggplot(mtcars, aes(mpg, hp, col = as.factor(cyl))) + 
  geom_line() +
  geom_point(show.legend = FALSE)

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94