Question

I'm using a Stamen background map in the ggmap package. I would like to replace all of the black elements in raster background image (i.e. colors "#000000" with say "#C0C0C0" - to basically look more like the toner light background 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)

Replacing the colors as per below only returns the raster part, and strips the object of its ggmap class.

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

Is there a way to replace the raster cells while not altering the other attributes?

Was it helpful?

Solution

You can manually change class and attr to match the original raster.

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top