【发布时间】:2021-03-23 19:25:51
【问题描述】:
我正在关注this guide 在地图上创建一些点。一切都很好,但我无法为这些点制作图例。我只是要复制代码以获得 MWE(但所有功劳归于 Valentin Stefan):
library(rgeos)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(dplyr)
library(ggplot2)
library(ggrepel)
theme_set(
theme_bw() +
theme(panel.background = element_rect(fill = "azure"),
panel.grid.major = element_blank(),
axis.title = element_blank(),
axis.text = element_text(size = 8))
)
world <- rnaturalearth::ne_countries(scale = 'medium', returnclass = "sp")
box_cut <- bbox2SP(n = 90, s = -90, w = -70, e = 120, proj4string = world@proj4string)
world_crop <- gDifference(world, box_cut)
pacific_crop <- world_crop %>%
st_as_sf() %>% # change from sp to sf object/class
st_shift_longitude() %>%
st_crop(c(xmin = st_bbox(.)[["xmin"]],
xmax = st_bbox(.)[["xmax"]],
ymin = -50,
ymax = 30))
ggplot() +
geom_sf(data = pacific_crop)
tiny_countries <- rnaturalearthdata::tiny_countries50 %>%
st_as_sf() %>%
st_shift_longitude() %>%
st_crop(c(xmin = 120, xmax = 250, ymin = -50, ymax = 30)) %>%
# Also adds the coordinates to be used for labeling with geom_text_repel
bind_cols(st_coordinates(.) %>% as.data.frame())
rbPal <- colorRampPalette(c('red','blue'))
Col <- rbPal(18)
ggplot() +
geom_sf(data = pacific_crop) +
geom_sf(data = tiny_countries, size = 2, color = Col, show.legend = "point")
如何获得颜色图例?
我试过改变图例的位置,但没有成功:
theme(legend.position = c(.5,.5))
【问题讨论】: