[R-sig-Geo] Transparency of Raster* objects

Oscar Perpiñan Lamigueiro oscar.perpinan at upm.es
Sun Aug 14 16:42:06 CEST 2011


Hi,

You can also use the "overlaying" mechanism of latticeExtra, 
with the levelplot method of rasterVis:

library(raster)
library(rasterVis)
library(lattice)
library(latticeExtra)

r <- raster(system.file("external/test.grd", package="raster"))

data(meuse.grid)
coordinates(meuse.grid) <- ~x+y
proj4string(meuse.grid) <- CRS("+init=epsg:28992")
gridded(meuse.grid) <- TRUE
d <- raster(meuse.grid, 'dist')

rPlot <- levelplot(r)
dPlot <- levelplot(d, par.settings=GrTheme, alpha.regions=0.4)

rPlot + dPlot

Besides, you can also add contour lines:

## In the next version of rasterVis a new contourplot method will be
## added as a wrapper to levelplot with these options.
dContour <- levelplot(d, region=FALSE, labels=TRUE, contour=TRUE,
			pretty=TRUE, colorkey=FALSE)

rPlot + dContour

rPlot + dPlot + dContour

Best,

Oscar.

El Fri, 29 Jul 2011
01:50:49 -0700 (PDT) Robert Hijmans <r.hijmans at gmail.com> escribió:
> Pierre, 
> 
> > The trick is I'm using RColorBrewer, who does not provides this 
> > posibility. 
> 
> No, but the alpha argument to plot (with a Raster* object) does:
> 
> r <- raster(system.file("external/test.grd", package="raster"))
> library(RColorBrewer)
> col <- brewer.pal(9, 'GnBu')
> plot(r, col=col, alpha=0.3)
> 
> > However, what I'm still stuck at using a variable transparency in 
> > space, eg in the meuse.grid dataset, how to make transparency a 
> > function of the distance to the river (the dist attribute) ? 
> 
> I do not know; the below is ugly, but perhaps it provides some ideas:
> 
> 
> 
> library(raster)
> 
> data(meuse.grid)
> coordinates(meuse.grid) <- ~x+y
> proj4string(meuse.grid) <- CRS("+init=epsg:28992")
> gridded(meuse.grid) <- TRUE
> data(meuse.riv)
> meuse.sr =
> SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
> 
> r <- raster(meuse.grid, 'dist')
> riv <- rasterize(meuse.sr, r)
> 
> d <- distance(riv)
> d[d > 1000] <- 1000
> d <- 254 * d/1000
> 
> a <- c(0:9, LETTERS[1:6])
> 
> bw <- paste("#000000", rep(a, each=16), rep(a, times=16), sep='')
> plot(r)
> plot(d, col=bw, add=T)
> 
> x11()
> ter <- paste(substr(terrain.colors(254), 1, 7), rep(a, each=16),
> rep(a, times=16), sep='')
> plot(r, col=gray(1:255/255))
> plot(d, col=ter, add=T)
> 
> Best, Robert
> 
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/Transparency-of-Raster-objects-tp6628801p6632801.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list