首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用极线(epsg:3995)投影和ggspatial时在ggplot中的66度平行北线上绘制一条线

如何在使用极线(epsg:3995)投影和ggspatial时在ggplot中的66度平行北线上绘制一条线
EN

Stack Overflow用户
提问于 2020-10-19 01:43:11
回答 1查看 66关注 0票数 0

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

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

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

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

代码语言:javascript
复制
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()没有正确的投影:

代码语言:javascript
复制
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")

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-19 15:17:32

看看这对你有没有用?

代码语言:javascript
复制
# 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))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64416400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档