[英]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. 受本文启发,我尝试使用Robinson投影绘制世界地图,并在地图上添加彩色点。 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).
我收到以下警告:
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. 颜色看起来很好(在col_pal中指定),但是我丢了图例。
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). 实际上,我还有更多点,其中有些是重叠的,我想确定它们的绘制顺序(例如,位于IND上方的SAT)。 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()
返回颜色的命名向量,而scale_color_manual()
将其解释为小数scale_color_manual()
中断:
> 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.