R 添加距离比例尺

R 添加距离比例尺,r,ggplot2,R,Ggplot2,我想在我的图表中添加一个比例尺,我尝试使用其他人建议的ggsn包,但没有成功(我无法获得正确宽度和位置的比例尺) 感谢您的帮助 代码: base1=get_map(位置=c(-75,44.5,-72,47),maptype=“碳粉背景”) map2=ggmap(base1) 站点您意外设置了x.min=-72,而x.max=-75,它们必须相反,因为-75

我想在我的图表中添加一个比例尺,我尝试使用其他人建议的ggsn包,但没有成功(我无法获得正确宽度和位置的比例尺)

感谢您的帮助

代码:

base1=get_map(位置=c(-75,44.5,-72,47),maptype=“碳粉背景”)
map2=ggmap(base1)

站点您意外设置了
x.min=-72
,而
x.max=-75
,它们必须相反,因为
-75<-72
。经过一些调整后,我可以这样绘制:

# library(ggmap)
#
# base1 = get_map(location = c(-75, 44.5, -72, 47), maptype = "toner-background")
# map2 = ggmap(base1)
#
# set.seed(53125597)
#
# sites <- data.frame(lon = sample(seq(-75, -72, .1), 6), 
#                     lat = sample(seq(44.5, 47, .1),  6)
#                     )
#

limts <- c(xmin =  .2 + min(map2$data$lon),
           xmax = -.2 + max(map2$data$lon),
           ymin =  .1 + min(map2$data$lat),
           ymax = -.1 + max(map2$data$lat)
           )
map2 + 
  geom_point(data = sites, aes(x = lon, y = lat), color = "blue") +
  labs(title = "Collection sites", x = "Longitude", y = "Latitude") + 
  ggsn::scalebar(dist = 40, dd2km = TRUE, model = 'WGS84',
                 x.min = limts[1], x.max = limts[2],  
                 y.min = limts[3], y.max = limts[4], 
                 location = "topleft", st.size = rel(2.5)
                 ) + 
  theme_bw() + 
  theme(
    axis.text  = element_text(size = rel(1.33)),
    axis.title = element_text(size = 15, face = "bold"),
    axis.text.x = element_text(angle = 45, vjust = 0.5)
    )
#库(ggmap)
#
#base1=get_map(位置=c(-75,44.5,-72,47),maptype=“碳粉背景”)
#map2=ggmap(base1)
#
#种子集(53125597)
#
#地点
# library(ggmap)
#
# base1 = get_map(location = c(-75, 44.5, -72, 47), maptype = "toner-background")
# map2 = ggmap(base1)
#
# set.seed(53125597)
#
# sites <- data.frame(lon = sample(seq(-75, -72, .1), 6), 
#                     lat = sample(seq(44.5, 47, .1),  6)
#                     )
#

limts <- c(xmin =  .2 + min(map2$data$lon),
           xmax = -.2 + max(map2$data$lon),
           ymin =  .1 + min(map2$data$lat),
           ymax = -.1 + max(map2$data$lat)
           )
map2 + 
  geom_point(data = sites, aes(x = lon, y = lat), color = "blue") +
  labs(title = "Collection sites", x = "Longitude", y = "Latitude") + 
  ggsn::scalebar(dist = 40, dd2km = TRUE, model = 'WGS84',
                 x.min = limts[1], x.max = limts[2],  
                 y.min = limts[3], y.max = limts[4], 
                 location = "topleft", st.size = rel(2.5)
                 ) + 
  theme_bw() + 
  theme(
    axis.text  = element_text(size = rel(1.33)),
    axis.title = element_text(size = 15, face = "bold"),
    axis.text.x = element_text(angle = 45, vjust = 0.5)
    )