geotheory
geotheory

Reputation: 23680

Preventing grey background to raster gplot with log scaled colour

Plotting a raster using gplot() yields a nice map, e.g: enter image description here

Since the variable scale (population) covers many orders of magnitude, a log scale is preferred. But when this is applied to the colour variable (by adding a trans='log' argument to scale_fill_gradient), the areas of zero population end up grey:

enter image description here

Does anyone know how to prevent this? This is the code I'm using:

require(raster, rgdal, ggplot2)
pop = readGDAL("usa_population.tif")
p = raster(pop, layer=1, values=TRUE)
s <- stack(p)
gplot(s) + geom_tile(aes(fill = value)) +
  scale_fill_gradient(low = 'white', high = 'blue', trans='log')

Thanks in advance.

Upvotes: 1

Views: 706

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60974

Just set the na.value argument of scale_fill_gradient to the appropriate value, e.g. 'white'. The NA's are caused by log, i.e. log(0) = -Inf which is interpreted as NA in ggplot2.

Upvotes: 3

Related Questions