【问题标题】:Add raster to ggmap base map: set alpha (transparency) and fill color to inset_raster() in ggplot2将栅格添加到 ggmap 底图:在 ggplot2 中将 alpha(透明度)和填充颜色设置为 inset_raster()
【发布时间】:2015-11-04 19:07:39
【问题描述】:

我想在 ggplot2 中绘制一个栅格覆盖 GoogleMaps 底图的地图。因此,我像这样使用get_map()insert_raster()

library(ggplot2)
library(ggmap)

bm <- ggmap(get_map(location = "Bangkok", maptype = "hybrid"))

bm + inset_raster(as.raster(r), xmin = r@extent[1], xmax = r@extent[2],
                  ymin = r@extent[3], ymax = r@extent[4])

是否有可能设置alpha 并更改fill 颜色?

结果如下所示:

【问题讨论】:

    标签: r plot ggplot2 geospatial ggmap


    【解决方案1】:

    没有fortify 会更快:

    阅读下面的原帖了解更多信息

    this blog entry发现我们可以直接在ggplot::geom_polygon()中使用空间多边形

    r <- raster(system.file("external/test.grd", package="raster"))
    # just to make it reproducible with ggmap we have to transform to wgs84
    r <- projectRaster(r, crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
    
    rtp <- rasterToPolygons(r)
    
    bm <- ggmap(get_map(location = bbox(rtp), maptype = "hybrid", zoom = 13))
    bm + 
      geom_polygon(data = rtp, 
                   aes(x = long, y = lat, group = group, 
                       fill = rep(rtp$test, each = 5)), 
                   size = 0, 
                   alpha = 0.5)  + 
      scale_fill_gradientn("RasterValues", colors = topo.colors(255)) 
    

    如果您只需要可视化某些东西,如何解决绘图速度问题

    如下所述,这样的绘图可能会因大量像素而变得非常缓慢。因此,您可能会考虑在将其转换为多边形之前减少像素数量(在大多数情况下,这并不会真正减少地图中的信息量)。因此,raster::aggregate 可用于将像素数减少到合理的数量。

    该示例显示像素数如何减少 4 的数量级(即 2 * 2,水平 * 垂直)。如需更多信息,请参阅?raster::aggregate

    r <- aggregate(r, fact = 2)
    #  afterwards continue with rasterToPolygons(r)...
    

    原帖:

    过了一会儿,我找到了解决这个问题的方法。 栅格转多边形!这个想法基本上是在Marc Needham's blog post之后实现的。

    然而,有一个缺点:ggplot 在处理大量多边形时会变得非常慢,这是不可避免的。但是,您可以通过绘制到png()(或其他)设备来加快速度。


    这是一个代码示例:

    library(raster)
    library(ggplot2)
    library(ggmap)
    
    r <- raster(....) # any raster you want to plot
    rtp <- rasterToPolygons(r)
    rtp@data$id <- 1:nrow(rtp@data)   # add id column for join
    
    rtpFort <- fortify(rtp, data = rtp@data)
    rtpFortMer <- merge(rtpFort, rtp@data, by.x = 'id', by.y = 'id')  # join data
    
    bm <- ggmap(get_map(location = "Shanghai", maptype = "hybrid", zoom = 10))
    
    bm + geom_polygon(data = rtpFortMer, 
                      aes(x = long, y = lat, group = group, fill = layer), 
                      alpha = 0.5, 
                      size = 0) +  ## size = 0 to remove the polygon outlines
         scale_fill_gradientn(colours = topo.colors(255))
    

    结果如下:

    【讨论】:

      【解决方案2】:

      我自己一直在研究这个。我遇到的问题是尝试用栅格覆盖 ggmap 输出,出现以下错误:

      错误:geom_raster 仅适用于笛卡尔坐标。

      解决此问题的方法是使用 coord_cartesian(),如下所示:

      library(ggplot2)
      library(ggmap)
      
      bm <- ggmap(get_map(location = "Bangkok", maptype = "hybrid"))
      
      bm <- bm + geom_raster(...) # insert your raster here
      
      bm <- bm + coord_cartesian()
      
      plot(bm)
      

      我不确定您的栅格 r 来自何处。为此,只需将栅格 r 转换为数据框并根据 geom_raster() 指令添加数据,确保坐标为纬度/经度(即与地图相同)。

      要回答您的问题,您可以通过 geom_raster() 操作 alpha 和填充。

      希望这会有所帮助。

      顺便说一句,这个解决方法最初是在这个链接上提出的: https://groups.google.com/forum/embed/#!topic/ggplot2/nqzBX22MeAQ

      【讨论】:

      • 感谢您的回答。但是,如果使用笛卡尔坐标,则不会保持纵横比。因此,它不太适合地理应用
      • 尝试添加以下内容:bm
      • 或者您只需添加 bm
      猜你喜欢
      • 2015-11-17
      • 2012-12-17
      • 2019-04-27
      • 2015-07-10
      • 2015-05-30
      • 1970-01-01
      • 2016-06-13
      • 2020-09-06
      • 2014-07-09
      相关资源
      最近更新 更多