r - 来自 dismo::gmap() 和 ggplot2 的谷歌地图

标签 r google-maps ggplot2

我得到了一张带有 dismo::gmap() 函数的 map 并想用 ggplot2 绘制它,因为我想使用 geom_point 和其他 ggplot 函数添加不同的功能。我更喜欢使用 dismo::gmap而不是 ggmap::get_map()下载谷歌地图图层。这是因为 dismo::gmap() , 不像 ggmap::get_map() ,从包含完整 CRS 信息的包栅格中返回栅格图层,因此应该可以修改图层的投影。

> head(data_info$latitude, 20)
#[1] 49.11306 49.39333 48.78083 51.85000 53.57361 50.67806 52.69083 52.21389 53.46361 50.99917 53.99750 53.54528 53.61417 48.00556 48.01306 53.45000
#[17] 51.93667 54.53083 51.95500 54.29639
> head(data_info$longitude, 20)
#[1] 13.134722 12.323056 13.803889 12.177778 14.143611 13.175833 12.649444 13.454167 11.629722 10.906111 11.415556  8.426944  7.160000 11.123889 10.786111
#[16] 12.766667 11.987222 13.091389 10.967500 13.684167


   e = extent(-14 , 58 , 28 , 64)
mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, 
                      path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off")

mapImageData2_proj <- projectExtent(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84")

# plot the points on the map 
ggplot(mapImageData2_proj, extent = "device") + 
  geom_point(inherit.aes = FALSE, aes(x = data_info$longitude, y = data_info$latitude),
             data = gps, colour = "red", size = 1, pch = 20)

尝试此操作后,我收到以下错误:

Error: ggplot2 doesn't know how to deal with data of class RasterLayer



如果我试试这个
plot(mapImageData2_proj)

Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add, : no values associated with this RasterLayer

最佳答案

这个问题有两个问题。一个是如何让 ggplot2 绘制 Raster* 对象。另一个是如何在保留其值的同时重新投影栅格。

OP包含代码

library(dismo)
e = extent(-14 , 58 , 28 , 64)
mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, 
                      path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off")

如果我们运行它然后执行 plot(mapImageData2)我们得到了一个不错的情节。然后OP运行
mapImageData2_proj <- projectExtent(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84")

现在如果我们运行 plot(mapImageData2_proj)我们得到一个错误!那是因为 projectExtent返回没有值的 RasterLayer。我们需要使用 projectRaster()反而。见 ?projectExtent详情。所以我们运行:
mapImageData2_proj <- projectRaster(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84")
plot(mapImageData2_proj)

现在我们看到重新投影的 map 。进步!但我们仍然无法绘制 mapImageData2_proj使用 ggplot2 , 因为 ggplot2不知道如何处理 Raster* 对象。我们需要将栅格转换为数据框。有几种方法可以做到这一点,但不加载任何额外的包,一个不错的选择是raster::rasterToPoints() .例如:
myPoints <- raster::rasterToPoints(myRaster)
myDataFrame <- data.frame(myPoints)
colnames(myDataFrame) <- c("Longitude", "Latitude", "Values")

ggplot(data=myDataFrame, aes_string(y = "Latitude", x = "Longitude")) + 
   geom_raster(aes(fill = Values))

因此,将它们放在 OP 的示例中:
library(dismo)
e = extent(-14 , 58 , 28 , 64)
mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, 
                      path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off")

plot(mapImageData2)

mapImageData2_proj <- projectRaster(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84")

plot(mapImageData2_proj)

myRaster <- mapImageData2_proj
myPoints <- raster::rasterToPoints(myRaster)
myDataFrame <- data.frame(myPoints)
colnames(myDataFrame) <- c("X", "Y", "Values")

ggplot(data=myDataFrame, aes_string(y = "Y", x = "X")) + 
  geom_raster(aes(fill = Values))

关于r - 来自 dismo::gmap() 和 ggplot2 的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39657443/

相关文章:

r - 以 Shiny 的方式编辑数据表中的多个单元格

javascript - 谷歌地图 CSS 问题

r - 在 ggplot2 和其他 R 图形中创建新的形状调色板

r - tidymodels 列中发现的新级别

r - 应用 if else 函数循环遍历 2 个向量来操作数据表

c - 在子目录中构建共享库

r - 如何突出显示两行之间的区域? ggplot

android - 使用 GPS 映射 Activity 会导致 JellyBean 模拟器崩溃

google-maps - 带有到达时间的 Google Maps Transit Directions URL,不使用 API

r - 如何更改 ggplot2 中的线条颜色而不会使图例消失?