受到this帖子的启发,我尝试使用罗宾逊投影绘制世界地图,并在地图上添加彩色圆点。 重新投影地图和点工作正常,但由于某种原因,我不明白,我不能改变点的颜色方案,并保持传奇。我尝试了以下内容:
首先,我在此处获得了形状文件:land和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")
但是,当我尝试更改色标时:
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")
任何想法如何解决这个问题?或者替代方法?
实际上我也有更多的点,其中一些重叠,我想确定它们的绘制顺序(例如,在IND之上的SAT)。我该怎么做?
答案 0 :(得分: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")