r - 如何更改 ggmap 中的 map 类型?

标签 r ggmap terrain

我在更改 ggmap 的背景时遇到问题。当我尝试更改 maptype 时,它总是显示为 terrain。我想将其更改为 satellite。有什么建议么?这是我的代码:

 library(ggmap)
 # Long and Lat Coordinates
 Cuba_all <- data.frame("Longitude" = c(-79.79623, -79.42313, -79.01722, -80.29218, -80.50040, -80.51981, -80.36674, -79.87957, -79.66906, -79.76122, -80.26587, -79.91689, -80.10454, -80.22530, -80.12910, -79.98889, -79.84307, -79.81694, -80.22201, -80.48088, -80.44482, -80.29068, -80.36213, -80.50879, -80.29634), "Latitude" = c(22.06622, 22.14845, 22.20900, 22.05258, 22.30107, 22.88154, 22.70679, 22.53541, 22.39237, 22.35697, 21.91868, 22.08949, 21.83760, 22.10561, 22.11061, 22.02766, 22.04936, 22.37516, 22.56684, 22.44313, 22.44416, 22.50470, 22.75872, 22.35473, 22.49178))
 # Create Cuba Dimensions
 sbbox <- make_bbox(lon = Cuba_all$Longitude, lat = Cuba_all$Latitude, f = .1)
 sbbox
 # Grab Map of Cuba
 sq_map <- get_map(location = sbbox, source = "google", maptype = "satellite")
 # Plot Map
 ggmap(sq_map)

最佳答案

@42 的评论部分正确(据我所知)。但问题不一定出在您的 API key 上,而是您的位置规范。 Google map 服务器希望将位置指定为中心 处的经/纬度,以及缩放系数。 get_map() 如果您以边界框格式提交位置,则静默 决定获取 Stamen 地形图;在我看来,这似乎是 get_map() 中的错误(或“infelicity”),并且至少是 two 的主题issuesggmap 问题列表上。

在任何情况下,当我指定了正确的经/纬度矢量,并尝试使用缩放因子直到它大致正确(除了反复试验之外我不知道该怎么做......),它起作用了对我来说。

sm <- with(Cuba_all,c(lon=median(Longitude),lat=median(Latitude)))
sq_map1 <- get_map(location = sm, zoom=9,
                   source = "google", maptype = "satellite")
ggmap(sq_map1)+
    geom_point(data=Cuba_all,aes(x=Longitude,y=Latitude),colour="red")
ggsave("cuba.png")

enter image description here


get_map() 中再深入一点,就会发现这可能是开发人员的疏忽。它似乎已在 ggmap 的开发版本中修复,但尚未进入 CRAN 版本(参见评论 here。)

这段代码块:

if (is.numeric(location) && length(location) == 4) {
    location_type <- "bbox"
    location_stop <- FALSE
    source <- "stamen"
    maptype <- "terrain"
    ## ...

检测到位置已作为边界框给出,并自动将源设置为“stamen”,将 maptype 设置为“terrain”。稍后有代码看起来应该在您通过 source=="google" 传递边界框时发出警告,但它永远无法到达(因为源将已更改为“花蕊”)...

if (source == "google") {
    if (location_type == "bbox") {
        warning("bounding box given to google - spatial extent only approximate.", 
            call. = FALSE, immediate. = TRUE)
        message("converting bounding box to center/zoom specification. (experimental)")
        user_bbox <- location
        location <- c(lon = mean(location[c("left", "right")]), 
            lat = mean(location[c("bottom", "top")]))
    }

关于r - 如何更改 ggmap 中的 map 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078242/

相关文章:

r - .call 如何对 data.frame 的行进行排序?

javascript - R 中 Shiny 的 STRING 交互网络

r - 如何将数据帧中的数据点添加到斯洛伐克行政区域的多边形 map ?

r - 如何使用 ggplot/ggmap 在 SpatialLinesDataFrame 中绘制街道并为其着色?

java - 平滑地形碰撞 - 3D

R:如何将移动平均值应用于数据框中的列子集?

r - 如何使用 ggplot2 创建堆叠直方图?

r - 使用 OpenStreetMap 从 data.frame 中绘制点

c++ - 如何用 C++ 编写菱形方 block 算法?

java - 如何将多个 3D 对象合并为单个网格以消除视觉故障?