Search code examples
rlegendggmap

adding legend to ggmap with geom_point positions from different datasets


I've been plotting two sets of positional data with geom_point from different data sets (MR + MRTag).

MR
detect_date        Latitude      Longitude    species
12/04/2016 11:08    -6.6524      71.3475      Manta Ray
12/04/2016 11:09    -6.6524      71.3475      Manta Ray
12/04/2016 11:10    -6.6524      71.3475      Manta Ray
16/04/2016 21:27    -6.6524      71.3475      Manta Ray

MRTag
species    taggingdate   deploy_lat  deploy_lon
Manta Ray   3/24/2016   -5.4191      71.83855
Manta Ray   02/05/2013  -5.2568      71.65768333
Manta Ray   02/07/2013  -5.33448     71.9812
Manta Ray   02/08/2013  -5.3046      71.94231667

I then used the code below to make a base map

library(ggmap)

MR_bbox <- make_bbox(lon = MR$Longitude, lat = MR$Latitude, f=1)
MR_map <- get_map(location = MR_bbox, source = "google", maptype = "satellite")

Then plotted my location data from Mr + MRTag onto the map

  MR_plot <-
    ggmap(MR_map) + 
     geom_point(data = MR, mapping = aes(x = Longitude, y = Latitude), 
        shape = 1, color = "white") +
     geom_point(data = MRTag, mapping = aes(x = deploy_lon, y = deploy_lat),
       shape = 25, fill = "#00FF00", color = "#00FF00") +
     xlab("Longitude") + 
     ylab("Latitude")

This gave me this plot

enter image description here

I'd like to add a legend for the two shapes to the map but I'm not sure of the best way to do it, and methods so far haven't worked


Solution

  • The solution is actually really simple: just move the relevant aesthetic arguments into the aes().

    MR_plot <- ggmap(MR_map) + 
        geom_point(data = MR, aes(x = Longitude, y = Latitude,
                                  color = 'detect', shape = 'detect', fill = 'detect')) +
        geom_point(data = MRTag, aes(x = deploy_lon, y = deploy_lat,
                                     color = 'tag', shape = 'tag', fill = 'tag')) +
        xlab("Longitude") + 
        ylab("Latitude") +
        scale_color_manual(name = 'legend', values = c(detect = 'white', tag = '#00FF00')) +
        scale_fill_manual(name = 'legend', values = c(detect = NA, tag = '#00FF00')) +
        scale_shape_manual(name = 'legend', values = c(detect = 1, tag = 25))
    

    It's actually slightly confusing: if you use color= outside the aes(), then you set the actual color of those points and there is no legend because it's purely an aesthetic choice.

    If you use color= (or fill= or shape=) inside the aes(), however, what you're doing is defining another variable that controls the color of the points. If you wanted to color those points based on species, for example, your aes() would be: aes(x = Longitude, y = Latitude, color = species). This is same thing: except by passing a single string you set the color variable for all points in that geom_point to a single value.

    You then have to use a scale_color_* function (and corresponding functions for fill and shape) to set those color variables to the specific colors you want. By default, each scale defined in the aes() will get it's own legend. But if you name the legends and give them the same name, then they will be combined into one.