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

And*_*y W 1 r raster ggmap

我在ggmap包中使用Stamen背景地图.我想,以取代所有光栅背景图像中的黑色元素(即颜色"#000000"有发言权"#C0C0C0"-基本上看起来更像碳粉光背景地图).

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)
Run Code Online (Sandbox Code Playgroud)

如下所示替换颜色仅返回栅格部分,并剥离其ggmap类的对象.

class(troy)
troy[troy == "#000000"] <- "#C0C0C0"
ggmap(troy)
class(troy)
Run Code Online (Sandbox Code Playgroud)

有没有办法更换栅格单元而不改变其他属性?

Nat*_*ope 7

您可以手动更改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)
Run Code Online (Sandbox Code Playgroud)