ggsn::scalebar 没有被放在角落里

ggsn::scalebar not being placed in the corner

我正在尝试向从 google 获得的地图添加比例尺。我想要左上角的比例尺,但它没有正确放置(它有点偏离角落)。我知道它与地图的范围有关,但我不知道如何解决它。

library(ggplot2)
library(ggsn)
library(ggmap)

ca <- get_map(location = c(-124.29, 40.75, -123.91, 40.97), maptype = "terrain", source = "google", force = T)

ggmap(ca) + 
    coord_sf(crs = st_crs(4326), expand = FALSE) +
    scalebar(x.min = -124.29, x.max = -123.91, y.min = 40.75, y.max = 40.97, 
             location = "topleft", 
             dist = 5,
             dist_unit = "km", 
             transform = TRUE, 
             model = "WGS84",
             st.dist = 0.04)

我认为 get_map() 中调用的坐标与 scalebar 中称为限制的坐标不同。

可以使用anchor调用,使用attr()获取下载地图的经纬度限制。这些限制可用于锚点呼叫。

ggmap(ca) + 
  coord_sf(crs = st_crs(4326), expand = FALSE) +
  scalebar(x.min = -124.29, x.max = -123.91, y.min = 40.75, y.max = 40.97, 
           anchor = c(x =  attr(ca, "bb")$ll.lon+0.005,
                      y = attr(ca, "bb")$ur.lat-0.005),
           location = "topleft", 
           dist = 5,
           dist_unit = "km", 
           transform = TRUE, 
           model = "WGS84",
           st.dist = 0.04)