简体   繁体   中英

R: ggplot2 legend disappears when changing colour scheme

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 (eg SAT on top of IND). How do I do this?

The problem is that piratepal() returns a named vector of colors and scale_color_manual() interprets the names as scale breaks:

> piratepal('espresso', length.out = 5)
       blue      yellow         red       green      orange 
"#2366C0FF" "#E9D738FF" "#B91226FF" "#A3DA4BFF" "#FF6435FF" 

Since those breaks don't exist in your data, the points are removed.

The solution is to unname the colors:

col_pal <- unname(piratepal('espresso', length.out = 5))

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM