rggplot2ggmap

Trouble getting legend to show up using geom_point overlaid on ggmap


I have points overlaid on a map using the following code:

seizures <- get_googlemap(center="Chicago") %>% ggmap() +
geom_point(data=seizures.df, aes(x=Longitude, y=Latitude, color='blue'),alpha=.5, size=.2, color='blue', stroke=NA, show.legend=TRUE)+
theme(legend.justification=c("right", "top))

For some reason, the points are not showing up in the legend. I just want a simple legend with a blue dot and accompanying text "seizures".

What am I doing wrong here?


Solution

  • To get a legend you have to map on aesthetics. The issue is that you map on the color aes but then override the mapping by setting the color as a parameter. Hence, you won't get a legend. Additionally you have to drop stroke=NA which as you have set a small size will make both the points and the legend key nearly invisible. For the reprex I have still chosen a larger point size as otherwise the points are hardly visible.

    Using some fake example data:

    library(ggmap)
    library(ggplot2)
    
    seizures.df <- data.frame(
      Longitude = c(-87.7, -87.9),
      Latitude = c(41.7, 41.9)
    )
    
    get_googlemap(center = "Chicago") |>
      ggmap() +
      geom_point(
        data = seizures.df, aes(x = Longitude, y = Latitude, color = "seizures"),
        alpha = .5, size = 2
      ) +
      scale_color_manual(values = "red") +
      theme(legend.justification = c("right", "top"))