R:ggplot2图例在更改颜色方案时消失

问题描述 投票:2回答:1

受到this帖子的启发,我尝试使用罗宾逊投影绘制世界地图并在地图上添加彩色圆点。重新投影地图和点工作正常,但由于某种原因,我不明白,我不能改变点的颜色方案,并保持传奇。我尝试过以下方法:

首先我在这里得到了形状文件:landgraticules

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

这很好,并生成下面的图像:enter image description here

但是,当我尝试更改色阶时:

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

我得到以下警告Removed 5 rows containing missing values (geom_point).和一个空的情节。

当我改变颜色比例时:

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

颜色变得很好(如col_pal中所指定),但我失去了传奇。 enter image description here

任何想法如何解决这个问题?或者替代方法?

实际上我也有更多的点,其中一些重叠,我想确定它们的绘制顺序(例如,在IND之上的SAT)。我该怎么做呢?

r ggplot2 map-projections
1个回答
3
投票

问题是piratepal()返回一个命名的颜色向量,scale_color_manual()将名称解释为缩放:

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

由于数据中不存在这些中断,因此将删除这些点。

解决方案是取消命名颜色:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.