Search code examples
rggplot2legendggmap

ggmap and ggplot2 map putting lines across point legend symbols


My code works perfectly fine for making this heat map, but in the legend there are lines through the symbols for "First" and "Second." Has anyone encountered this before and know how to remove the lines?

library(ggmap)
library(ggplot2)

d <- data.frame(lat=c(32.754469, 32.758926, 39.78373, 39.78373, 32.758189, 32.754775, 32.75756, 39.78373, 32.7326038, 39.78373, 32.743025, 32.775465, 
        32.756845, 32.759155, 32.759651, 32.758805, 32.741087, 39.78373, 32.751075, 32.770205, 39.78373, 39.78373, 32.736632, 32.752218, 32.733958, 
        39.78373, 32.758586, 39.78373, 32.759155, 39.78373, 32.758861, 39.78373, 32.736632, 32.750376, 32.75301, 32.738772, 32.73412, 32.736597, 
        39.78373, 32.736632, 32.732869, 32.736632, 39.78373, 32.760497, 32.756845, 32.746779),
                lon=c(-97.819817, -97.796525, -100.445882, -100.445882, -97.750132, -97.800734, -97.799162, -100.445882, -97.758559, -100.445882, -97.777412, 
        -97.668538, -97.791722, -97.797588, -97.775083, -97.81442, -97.799367, -100.445882, -97.804373, -97.784773, -100.445882, -100.445882, 
        -97.796336, -97.785369, -97.785513, -100.445882, -97.813654, -100.445882, -97.797588, -100.445882, -97.793399, -100.445882, -97.796336, 
        -97.79245, -97.800555, -97.790529, -97.799786, -97.796319, -100.445882, -97.796336, -97.790895, -97.796336, -100.445882, -97.778662, -97.791722, -97.797658))

h <- data.frame(lat=c(32.742385, 32.741191, 32.724373, 32.731578, 32.73223, 32.730207, 32.73507, 32.759701, 32.767522, 32.762228),
                lon=c(-97.794423, -97.80461, -97.817111, -97.814845, -97.815078, -97.813889, -97.758229, -97.750443, -97.693563, -97.743093))

hd <- rbind(h,d)
hd$type <- c(rep("First",10),rep("Second",46))

mapImageData2 <- get_map(location=c(lon = -97.78, lat = 32.75), zoom=13, maptype='roadmap', color='color')
ggmap(mapImageData2) + stat_density2d(data=d, mapping=aes(x=lon, y=lat, fill = ..level..), 
geom="polygon", alpha=0.3) + scale_fill_continuous("Density", low="pink", high="red") + 
geom_point(data=hd, mapping=aes(lon, lat, shape = type, colour=factor(type)), size =3) + scale_colour_manual(values = c("black", "darkorchid")) +
labs(x = 'Longitude', y = 'Latitude', shape="Points of Interest", colour="Points of Interest") 

Solution

  • Add show_guide=FALSE to stat_density2d:

    stat_density2d(data=d, mapping=aes(x=lon, y=lat, fill = ..level..), 
                   geom="polygon", alpha=0.3, show_guide=FALSE)
    

    enter image description here