How to invert the colors of a ggmap raster image in R?
Asked Answered
P

1

6

I'm assuming the Raster package has what I need... I'm simply wanting to invert the colors in a Raster image.

The actual scenario is this: I want to invert the raster image returned by a ggmap call:

 library(ggmap)
 ggmap(get_stamenmap(maptype = "toner"))

Regular toner map

I want to invert the colors to get a white-on-black version of the Stamen Toner map:

Inverted toner map

Papageno answered 8/9, 2015 at 18:18 Comment(0)
N
11

This inverts the raster object returned by get_stamenmap()

library("ggmap")
m <- get_stamenmap(maptype = "toner")

# invert colors in raster
invert <- function(x) rgb(t(255-col2rgb(x))/255)    
m_inv <- as.raster(apply(m, 2, invert))

# copy attributes from original object
class(m_inv) <- class(m)
attr(m_inv, "bb") <- attr(m, "bb")

ggmap(m_inv)

ggmap inverted

Narine answered 8/9, 2015 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.