我使用 sf 包和 ggplot2 生成了一个地图:
library(ggplot2)
library(sf)
library(rnaturalearth)
state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"), returnclass="sf")
x <- ggplot(data=state_prov) +
geom_sf()+
coord_sf(xlim=c(-170, -95), ylim=c(40, 75))
print(x)
Run Code Online (Sandbox Code Playgroud)
太棒了,但我需要添加一个比例尺。当我尝试使用 ggsn 修改代码时,我根本看不到比例尺。
library(ggplot2)
library(sf)
library(rnaturalearth)
state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"), returnclass="sf")
x <- ggplot(data=state_prov) +
geom_sf()+
coord_sf(xlim=c(-170, -95), ylim=c(40, 75)) +
ggsn::scalebar(state_prov, location="topleft", dist = 50, dist_unit = "km",
transform=TRUE, model="WGS84", height=0.1)
print(x)
Run Code Online (Sandbox Code Playgroud)
我尝试更改高度、距离和位置,但没有成功。当我删除对 coord_sf() 的调用时,我可以看到比例尺缩放得很差,这让我相信 ggsn 无法识别地图正在由 coord_sf() 放大。
我该如何解决?ggsn 似乎不容易修改。我愿意使用另一个包或方法,但我确实需要继续以类似的方式调用 ggplot,因为我有一个基于相同结构的更复杂的地图。
谢谢!