R语言 更改罗宾逊投影Map中的海洋颜色

qxsslcnc  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(153)

我正在制作罗宾逊投影(即椭圆形)的世界Map,想通过将海洋的颜色设置为蓝色来区分海洋和陆地。如何在非矩形投影中做到这一点?

library("ggplot2")
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

world <- ne_countries(scale = "medium", returnclass = "sf")

# Creates map with land and ocean as color white
ggplot(data = world) +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', linetype = "dashed", size = 0.25),
        panel.border = element_blank(),
        panel.background = element_blank())

# Creates map with a blue ocean however, the blue extends to a rectangle shape
# Would be ideal if the blue ocean fill was confined to the oval shape of the projection
ggplot(data = world) +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', linetype = "dashed", size = 0.25),
        panel.border = element_blank(),
        panel.background = element_rect(fill = 'aliceblue'))
gmol1639

gmol16391#

将海洋创建为多边形并首先对其进行绘图。

ocean <- st_polygon(list(cbind(c(seq(-180, 179, len = 100), rep(180, 100), 
                        seq(179, -180, len = 100), rep(-180, 100)),
                      c(rep(-90, 100), seq(-89, 89, len = 100),
                        rep(90, 100), seq(89, -90, len = 100))))) |>
  st_sfc(crs = "WGS84") |>
  st_as_sf()

ggplot(data = world) +
  geom_sf(data = ocean, fill = "#8080ff80") +
  geom_sf(color = "gray60", fill = "white") +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 
           +datum=WGS84 +units=m +no_defs", expand = F) +
  theme_bw() +
  theme(panel.grid.major = element_line(color = 'gray75', 
                                        linetype = "dashed",
                                        linewidth = 0.25),
        panel.border = element_blank(),
        panel.background = element_rect(fill = 'white'))

相关问题