0

我正在尝试在北纬 66 度线上画一个蓝色圆圈。在我正在制作的地图上。

我通常是fortify()一个SpatialPolygonsDataFrame,然后使用组合geom_polygon()coord_map("ortho" , ylim = c(50, 90))geom_hline()实现这一点。

但是,对于一个新项目,我将不得不ggspatial::layer_spatial()在一个已经有 CRS 日期的对象上使用,所以这种直线绘制和自动重新投影不起作用。

这是我常用方法的演示,该方法有效:

library(tidyverse)

map_data <- rnaturalearth::ne_countries()
map_data_df <- fortify(map_data)

ggplot() + 
  geom_polygon(data = map_data_df, 
               aes(x = long,
                   y = lat,
                   group = group),
               col = "black",
               fill = "lightgrey") + 
  coord_map("ortho" , ylim = c(50, 90))  +
  geom_hline(yintercept = 66, col = "blue")

在此处输入图像描述

我试图在这段代码中复制它,但这显然不起作用,因为geom_hline()没有正确的投影:

map_data_transformed <- sp::spTransform(map_data, sp::CRS("+init=epsg:3995"))
# needs some fixing to close all the polygons
map_data_transformed <- rgeos::gBuffer(map_data_transformed, byid=TRUE, width=0)


ggplot() + 
  ggspatial::layer_spatial(data = map_data_transformed, 
                           fill = "lightgrey", col = "black", size = .2)+
  coord_sf( xlim = c(-4500000 , 4500000),
            ylim = c(-4500000 , 4500000)) +
  geom_hline(yintercept = 66, col = "blue")

在此处输入图像描述

4

1 回答 1

2

看看这对你有用吗?

# define parallel north line as a spatial polygon with latitude / longitude projection
new_line <- sp::Polygon(coords = matrix(c(seq(-180, 180),
                                          rep(66, times = 361)), # change this for other
                                                                 # intercept values
                                        ncol = 2))
new_line <- sp::SpatialPolygons(Srl = list(sp::Polygons(srl = list(new_line), 
                                                        ID = "new.line")),
                                proj4string = sp::CRS("+proj=longlat +datum=WGS84"))

# change projection to match that of data
new_line <- sp::spTransform(new_line, sp::CRS("+init=epsg:3995"))

# plot
ggplot() + 
  ggspatial::layer_spatial(data = map_data_transformed, 
                           fill = "lightgrey", col = "black", size = .2) +
  ggspatial::layer_spatial(data = new_line,
                           fill = NA, col = "blue") +
  coord_sf( xlim = c(-4500000 , 4500000),
            ylim = c(-4500000 , 4500000))

阴谋

于 2020-10-19T07:17:32.007 回答