使用极坐标 (epsg:3995) 投影和 ggspatial 时如何在 ggplot 中的北纬 66 度线画一条线
How to draw a line on the 66th parallel north in ggplot when using a polar (epsg:3995) projection and ggspatial
我想在北纬 66 度线画一个蓝色圆圈。在我制作的地图上。
我通常 fortify()
一个 SpatialPolygonsDataFrame,然后使用 geom_polygon()
、coord_map("ortho" , ylim = c(50, 90))
和 geom_hline()
的组合来实现这一点。
但是,对于一个新项目,我将不得不在一个已经有 CRS 日期的对象上使用 ggspatial::layer_spatial()
,所以这种直线绘制和自动重新投影不起作用。
这是我常用方法的演示,它有效:
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")
看看这对你有用吗?
# 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))
我想在北纬 66 度线画一个蓝色圆圈。在我制作的地图上。
我通常 fortify()
一个 SpatialPolygonsDataFrame,然后使用 geom_polygon()
、coord_map("ortho" , ylim = c(50, 90))
和 geom_hline()
的组合来实现这一点。
但是,对于一个新项目,我将不得不在一个已经有 CRS 日期的对象上使用 ggspatial::layer_spatial()
,所以这种直线绘制和自动重新投影不起作用。
这是我常用方法的演示,它有效:
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")
看看这对你有用吗?
# 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))