R语言 如何旋转ggplot2制作的Map并添加指北针?

utugiqy6  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在用ggplot2制作一个Map,并试图稍微旋转Map。在previous stackoverflow问题之后,我将CRS调整为斜墨卡托投影。效果很好。
但是,我使用ggsn包来包含一个指北针,指北针要么完全消失,要么随新CRS投影沿着旋转。
如何旋转使用ggplot2创建的Map并包含实际指向北方的指北针?
下面是一个可重复的例子:

library(sf)
library(ggplot2)
library(tigris)
library(ggsn)

places<-places(state = 42)
places<-places[places$NAME=="Philadelphia",]
roads<-roads(state = "42",county = "101")

ggplot()+
  geom_sf(data = places,fill=NA,color='black',linewidth = 1)+
  geom_sf(data = roads[roads$RTTYP=="S",],color="black",linewidth=1)+
  north(data = places,
        location = 'topleft',
        scale = .1,
        symbol = 11)+
  coord_sf(datum = NA)

得到:

将CRS更改为斜墨卡托将生成旋转图像,但没有指北针:

crs_string = "+proj=omerc +lat_0=39.95276 +lonc=-75.16340, +alpha=0 +k_0=1 +datum=WGS84 +units=m +no_defs +gamma=-11"

ggplot()+
  geom_sf(data = places,fill=NA,color='black',linewidth = 1)+
  geom_sf(data = roads[roads$RTTYP=="S",],color="black",linewidth=1)+
  north(data = places,
        location = 'topleft',
        scale = .1,
        symbol = 11)+
  coord_sf(datum = NA, crs = crs_string)

最后,更改指北针的数据投影会使指北针返回,但生成的指北针直接指向“上”而不是正北。

ggplot()+
  geom_sf(data = places,fill=NA,color='black',linewidth = 1)+
  geom_sf(data = roads[roads$RTTYP=="S",],color="black",linewidth=1)+
  north(data = st_transform(places,crs = "+proj=omerc +lat_0=39.95276 +lonc=-75.16340, +alpha=10 +k_0=1 +datum=WGS84 +units=m +no_defs +gamma=0"),
        location = 'topleft',
        scale = .1,
        symbol = 11)+
  coord_sf(datum = NA, crs = crs_string)

w8ntj3qf

w8ntj3qf1#

好吧,不是使用ggsn,而是使用ggspatial,我能够让它工作:

library(sf)
library(ggplot2)
library(tigris)
library(ggspatial)

places<-places(state = 42)
places<-places[places$NAME=="Philadelphia",]
roads<-roads(state = "42",county = "101")

crs_string = "+proj=omerc +lat_0=39.95276 +lonc=-75.16340, +alpha=0 +k_0=1 +datum=WGS84 +units=m +no_defs +gamma=-11"

ggplot()+
  geom_sf(data = places,fill=NA,color='black',linewidth = 1)+
  geom_sf(data = roads[roads$RTTYP=="S",],color="black",linewidth=1)+
  annotation_north_arrow(which_north = "true",style = north_arrow_minimal(),location ="tl")+
  coord_sf(datum = NA, crs = crs_string)

相关问题