[R-sig-Geo] Annotating categories according with a SpatialPolygonsDataFrame factor

Luis Ferreira lferreira75.1 at gmail.com
Fri Oct 16 04:29:16 CEST 2015


Hello all.
I'm starting to use R, so not experienced at all.
I want to create boxplots grouped by zones given by several polygons.
The values are given by a raster DEM (1 band raster), in this case.

I'm lost in what to do when labelling/annotating the zones/categories.

Using rasterize(x,raster,field) and designating the factor/field from
SpatialPolygonsDataFrame doesn't make any difference: the final
zones/groups labels are given by consecutive numbers according to
SpatialPolygonsDataFrame objects order.

##############################################
library(rgdal)
library(maptools)
library(sp)
library(raster)

polyg1 <- readOGR("/data",layer="polygon_shapefile")
raster1 <- brick("/data/dem.tif")

extent(raster1) <- extent(polyg1)

polyg1_rst <- rasterize(polyg1, raster1,
polyg1 at data[["some_character_field"]], fun='last', background=NA,
mask=FALSE, update=FALSE, updateValue='NA')

#create RasterStack
tmp <- stack(raster1, polyg1_rst)
names(tmp) <- c('value', 'cat')

boxplot(tmp$value, tmp$cat, notch = FALSE, outline = TRUE, horizontal =
TRUE)
#############################################



Using extract(x,y), there are no factors from the
SpatialPolygonsDataFrame created on the resulting data frame. The
boxplot categories will be given also by consecutive numbers.

#############################################
library(rgdal)
library(maptools)
library(sp)
library(raster)

polyg1 <- readOGR("/data",layer="polygon_shapefile")
raster1 <- brick("/data/dem.tif")

##create data.frame
tmp <- extract(raster1, polyg1, method='simple', fun=NULL, na.rm=TRUE,
weights=FALSE, normalizeWeights=FALSE, cellnumbers=FALSE, small=FALSE,
df=TRUE, factors=TRUE, sp=FALSE)
names(tmp) <- c('cat', 'value')

boxplot(tmp$value ~ tmp$cat, notch = FALSE, outline = TRUE, horizontal =
TRUE)
############################


Do I need somehow to use is.factor() and as.factor() functions to create
value labels for categorical variables?? 

I'll appreciate any comments
Thank you.

Luís



	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list