R 为什么我的ggmap比例尺在屏幕外显示/根本不显示?

R 为什么我的ggmap比例尺在屏幕外显示/根本不显示?,r,ggplot2,mapping,ggmap,R,Ggplot2,Mapping,Ggmap,我试图在地图上添加一个20公里的比例尺,但我尝试过的每个解决方案要么在屏幕外添加比例尺(你只能看到“公里”的底部),要么根本不添加 我最近使用的是scalebar(),它在屏幕外添加了一个比例尺,但不允许我将其移动到完全可见的位置。我也尝试过用geom_线等从零开始制作一个酒吧,但那根本没有效果 这是一张可复制的地图,不需要制作比例尺和一小组坐标 库(ggmap) 图书馆(ggsn) wd我最终能够使用锚参数来移动比例尺的位置。由于项目的范围,我使地图的锚定和边界变得灵活 #Create the

我试图在地图上添加一个20公里的比例尺,但我尝试过的每个解决方案要么在屏幕外添加比例尺(你只能看到“公里”的底部),要么根本不添加

我最近使用的是scalebar(),它在屏幕外添加了一个比例尺,但不允许我将其移动到完全可见的位置。我也尝试过用geom_线等从零开始制作一个酒吧,但那根本没有效果

这是一张可复制的地图,不需要制作比例尺和一小组坐标
库(ggmap)
图书馆(ggsn)

wd我最终能够使用锚参数来移动比例尺的位置。由于项目的范围,我使地图的锚定和边界变得灵活

#Create the data frame    
Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)

    Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)

    graphingdata <- cbind.data.frame(Latitude,Longitude)

#Set up bounding box
    height <- max(graphingdata$Latitude) - min(graphingdata$Latitude)
    width <- max(graphingdata$Longitude) - min(graphingdata$Longitude)
    sac_borders <- c(bottom  = min(graphingdata$Latitude)  - 0.1 * height, 
                     top     = max(graphingdata$Latitude)  + 0.1 * height,
                     left    = min(graphingdata$Longitude) - 0.1 * width,
                     right   = max(graphingdata$Longitude) + 0.1 * width)

 #Get the site map
 map <- get_stamenmap(sac_borders, zoom = 10, maptype = "terrain")    
 map <- ggmap(site_map, legend = "bottom")+ 
          scalebar(x.min = sac_borders[[3]]-1, x.max = sac_borders[[4]]+1,y.min = sac_borders[[1]]-1, y.max = sac_borders[[2]]+1,transform = TRUE,
                   dist = 20,dist_unit = "km",model = "WGS84",anchor = c(x=sac_borders[[3]]+0.6,y=sac_borders[[1]]+0.25), st.size = 2, border.size = 0.5)+
          geom_point(data = graphingdata, aes(x = as.numeric(Longitude), 
                                              y = as.numeric(Latitude)), color = "red", size = 2) + 
          ggtitle(paste0("This is the map title"), 
                  subtitle = paste0("This is the subtitle"))
#创建数据帧
也许你可以看看
#Create the data frame    
Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)

    Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)

    graphingdata <- cbind.data.frame(Latitude,Longitude)

#Set up bounding box
    height <- max(graphingdata$Latitude) - min(graphingdata$Latitude)
    width <- max(graphingdata$Longitude) - min(graphingdata$Longitude)
    sac_borders <- c(bottom  = min(graphingdata$Latitude)  - 0.1 * height, 
                     top     = max(graphingdata$Latitude)  + 0.1 * height,
                     left    = min(graphingdata$Longitude) - 0.1 * width,
                     right   = max(graphingdata$Longitude) + 0.1 * width)

 #Get the site map
 map <- get_stamenmap(sac_borders, zoom = 10, maptype = "terrain")    
 map <- ggmap(site_map, legend = "bottom")+ 
          scalebar(x.min = sac_borders[[3]]-1, x.max = sac_borders[[4]]+1,y.min = sac_borders[[1]]-1, y.max = sac_borders[[2]]+1,transform = TRUE,
                   dist = 20,dist_unit = "km",model = "WGS84",anchor = c(x=sac_borders[[3]]+0.6,y=sac_borders[[1]]+0.25), st.size = 2, border.size = 0.5)+
          geom_point(data = graphingdata, aes(x = as.numeric(Longitude), 
                                              y = as.numeric(Latitude)), color = "red", size = 2) + 
          ggtitle(paste0("This is the map title"), 
                  subtitle = paste0("This is the subtitle"))