[R-sig-Geo] Raster Zonal Histogram

Robert J. Hijmans r.hijmans at gmail.com
Tue Jan 14 19:23:43 CET 2014


Arnold,

Here is an alternative to the rasterVis / levelplot approach that
Oscar suggested. Three approaches to make a matrix with the counts by
class and zone. These can then be used with base/plot commands, or
perhaps with ggplot; here illustrated with barplot.

Robert

library(raster)
# example data (taken from ?zonal)
r <- raster(ncols=10, nrows=10)
set.seed(0)
# random values between 0 and 65
slope <- setValues(r, runif(ncell(r)) * 65)
# landuse zones
landuse <- setValues(r, rep(1:5, each=20))

# approach 1
# make individual layers for each land use class
z <- layerize(landuse, falseNA=TRUE)
# make slope by land use layers
sz <- z * slope
m <- matrix(c(0,5,1, 5,15,2, 15,30,3, 30,45,4, 45,65,5), ncol=3, byrow=TRUE)
rr <- reclassify(sz, m, include.lowest=TRUE, right=FALSE)
f1 <- freq(rr, merge=TRUE, useNA='no')
f1 <- as.matrix(f1[,-1])

# approach 2
m <- matrix(c(0,5,1, 5,15,2, 15,30,3, 30,45,4, 45,65,5), ncol=3, byrow=TRUE)
sl <- reclassify(slope, m, include.lowest=TRUE, right=FALSE)
ct <- crosstab(sl, landuse, long=TRUE)
f2 <- reshape(ct, direction='wide', idvar='layer.1', timevar='layer.2')
f2 <- as.matrix(f2[,-1])

# approach 3
z <- layerize(landuse, falseNA=TRUE)
sz <- z * slope
x <- hist(sz, breaks=c(0,5,15,30,45,65))
f3 <- sapply(x, function(i) i$counts)

# f1, f2, f3, can be plotted like this
x11()
f <- f1
colnames(f) <- paste0('landuse', 1:ncol(f))
barplot(f, beside=TRUE, col=terrain.colors(5), ylim=c(0,10))
legend(1.5, 10, c('0-5', '5-15', '15-30', '30-45', '45-60'),
fill=terrain.colors(5), cex=.75)



On Mon, Jan 13, 2014 at 2:49 PM, Oscar Perpiñan
<oscar.perpinan at gmail.com> wrote:
> Hello,
>
> In rasterVis there is a method to produce histograms with a formula and a
> Raster* object. The last example of the help page of rasterVis::histogram
> illustrates this usage.
> In the formula, you can include names of layers, "x" and "y" (coordinates),
> and "dirXY" if you supply this argument in histogram.
>
> Best,
>
> Oscar.
>
> Oscar Perpiñán Lamigueiro
> Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
> Grupo de Sistemas Fotovoltaicos (IES-UPM)
> URL: http://oscarperpinan.github.io
> Twitter: @oscarperpinan
> LinkedIn: http://es.linkedin.com/in/oscarperpinan
> El 13/01/2014 15:28, "Arnold Salvacion" <arnold_salvacion at yahoo.com>
> escribió:
>
>> Dear Colleagues,
>>
>> Good day!
>>
>> Just want to ask if there is already a similar package or function in R
>> which do the same as the Zonal Histogram (
>> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Using_the_ArcGIS_Spatial_Analyst_toolbar_to_create_a_Zonal_Histogram)
>> in ArcGIS?
>>
>> Best regards,
>>
>> Arnold
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list