Inspired by this post, I tried plotting a world map using Robinson projection and adding coloured dots to the map. Reprojecting the map and points works fine, but, for some reason I don't understand, I can't change the colour scheme of the dots and keep the legend. I've tried the following:
First I got shape files here: land and graticules
library(rgdal)
library(ggplot2)
library(sp)
library(yarrr)
setwd('~/Documents/worldshapefiles') # this is where the shapefiles are
# read the shapefile for the simple worldmap
wmap <- readOGR(dsn = 'ne_110m_land', layer = 'ne_110m_land')
wmap_df <- fortify(wmap)
# get bounding box
bbox <- readOGR("ne_110m_graticules_all", layer="ne_110m_wgs84_bounding_box") # read bounding box
bbox_df<- fortify(bbox)
site_locs <- cbind.data.frame(x = c(-5, -3, -3, 58, -112), y = c(68, -37, 35, 19, -4), ocean = c('NAT', 'IND', 'MDX', 'SAT', 'PAC'))
coordinates(site_locs) <- c("x", "y") # convert to spatialpointsdataframe
proj4string(site_locs) <- CRS("+proj=longlat + datum=WGS84")
# reproject everything
bbox_robin <- spTransform(bbox, CRS("+proj=robin"))
bbox_robin_df <- fortify(bbox_robin)
wmap_robin <- spTransform(wmap, CRS("+proj=robin"))
wmap_df_robin <- fortify(wmap_robin)
site_locs_robin <- spTransform(site_locs, CRS('+proj=robin'))
site_locs_robin_df <- as.data.frame(site_locs_robin)
col_pal <- piratepal('espresso', length.out = 5)
# plot
ggplot(bbox_robin_df, aes(long,lat)) +
geom_polygon(data = wmap_df_robin, aes(long,lat, group = group, fill = hole)) +
geom_point(data = site_locs_robin_df, aes(x, y, colour = ocean)) +
coord_equal() +
geom_polygon(linetype = 'solid', fill = NA, colour = 'black', size = 0.5) +
scale_fill_manual(values=c("black", "white"), guide="none")
This works fine and produces the image below:
However, when I try to change the colour scale:
ggplot(bbox_robin_df, aes(long,lat)) +
geom_polygon(data = wmap_df_robin, aes(long,lat, group = group, fill = hole)) +
geom_point(data = site_locs_robin_df, aes(x, y, colour = ocean)) +
scale_colour_manual(values = col_pal) +
coord_equal() +
geom_polygon(linetype = 'solid', fill = NA, colour = 'black', size = 0.5) +
scale_fill_manual(values=c("grey80", "white"), guide="none")
I get the following warning Removed 5 rows containing missing values (geom_point).
and an empty plot.
And when I change the colour scale like this:
ggplot(bbox_robin_df, aes(long,lat)) +
geom_polygon(data = wmap_df_robin, aes(long,lat, group = group, fill = hole)) +
geom_point(data = site_locs_robin_df, aes(x, y, colour = ocean), colour = col_pal) +
scale_colour_manual(values = col_pal) +
coord_equal() +
geom_polygon(linetype = 'solid', fill = NA, colour = 'black', size = 0.5) +
scale_fill_manual(values=c("grey80", "white"), guide="none")
the colours turn out fine (as specified in col_pal), but I lose the legend.
Any ideas how to solve this? Or alternative approaches?
In reality I also have more points, some of which overlap and I'd like to fix the order in which they are plotted (e.g. SAT on top of IND). How do I do this?