Reputation: 121
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
Upvotes: 0
Views: 829
Reputation: 35242
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)
Upvotes: 1