r - 如何替换 ggmap 对象中的颜色?

标签 r raster ggmap

我在 ggmap 包中使用了 Stamen 背景 map 。我想用 "#C0C0C0" 替换光栅背景图像中的所有黑色元素(即颜色 "#000000" - 基本上看起来更像 toner light 背景 map )。

library(ggmap)
loc <- c(left = -73.706, bottom = 42.6940, right = -73.648, top = 42.7921)
troy <- get_map(location = loc, zoom = 13, maptype = "toner", source = "stamen")
ggmap(troy)

按照下面的方法替换颜色只会返回光栅部分,并去除其 ggmap 类的对象。

class(troy)
troy[troy == "#000000"] <- "#C0C0C0"
ggmap(troy)
class(troy)

有没有办法在不改变其他属性的情况下替换栅格像元?

最佳答案

您可以手动更改 classattr 以匹配原始栅格。

library(ggmap)
loc <- c(left = -73.706, bottom = 42.6940, right = -73.648, top = 42.7921)
troy <- get_map(location = loc, zoom = 13, maptype = "toner", source = "stamen")

attr_troy <- attr(troy, "bb")    # save attributes from original

# change color in raster
troy[troy == "#000000"] <- "#C0C0C0"

# correct class, attributes
class(troy) <- c("ggmap", "raster")
attr(troy, "bb") <- attr_troy
ggmap(troy)

关于r - 如何替换 ggmap 对象中的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18859809/

相关文章:

r - 如何检查我们在excel中具有相同唯一ID的每个唯一值

r - R中的浮点问题?

r - 如何使用 `` ggplot 2`` 从数字矩阵绘制栅格

r - ggmap 路线查找 - 不停留在道路上

r - 在 ggplot2 上覆盖 ggmap 、 geom_polygon(shape file)

r - 在一行中从数据框中提取值

python - Python 命令 requests.post() 的 R 对应项是什么?

重新分类二维星阵列

r 读取 NetCDF 并导出为 shapefile

r - 如何在R中反转ggmap栅格图像的颜色?