本文介绍了在栅格上绘制ggmap图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从栅格图的ggmap绘制地图.该地图上有很多白色区域,因此我想使白色区域透明,以便可以使用ggplot将其余元素绘制在彩色栅格的顶部.这是地图:
I'm trying to plot a map from ggmap in front of a raster. This map has a lot of white area, so I wanted to make the white area transparent so I can plot the remaining elements on top of a colorful raster, using ggplot. Here is the map:
Fulanus_bbox <- c(left = 17.250000, bottom = -3.916667, right = 36.916667, top = 8.416667)
Fulanus_big <- ggmap::get_map(location = Fulanus_bbox, source = "stamen", maptype = "toner-lite")
map <- ggmap::ggmap(Fulanus_big)
map
我所能想到的就是这两个解决方案,我没想到它们会起作用,而他们却没有.我尝试替换地图上的十六进制代码:
All I could think of were this two solutions, which I didn't expect to work and they didn't. I have tried replacing the hex codes on the map:
map_colors <- map$layers[[2]]$geom_params$raster
map_colors[map_colors == '#FFFFFF'] <- '#00FFFFFF'
map$layers[[2]]$geom_params$raster <- map_colors
map
我还尝试过将颜色转换成栅格以查看图案是否得到维持:
I have also tried transforming the colors into a raster to see if the pattern was maintained:
maprow <- nrow(map_colors)
mapcol <- ncol(map_colors)
map_factors <- as.factor(map_colors)
map_numeric <- as.numeric(map_factors)
map_matrix <- matrix(map_numeric, nrow = maprow, ncol = mapcol)
map_raster <- raster::raster(map_matrix)
map_raster <- raster::setExtent(map_raster, Fulanus_bbox)
raster::crs(map_raster) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 "
raster::plot(map_raster)
那有可能吗?如果是,怎么办?
Is that possible to do? If it is, how?
推荐答案
这似乎可行:
map <- ggmap::ggmap(Fulanus_big)
r <- map$layers[[2]]$geom_params$raster
r[r=="#FFFFFF"] <- NA
map$layers[[2]]$geom_params$raster <- r
map
这篇关于在栅格上绘制ggmap图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!