[R-sig-Geo] How to plot the grid figure using R?

Roger Bivand Roger.Bivand at nhh.no
Wed Dec 12 10:30:15 CET 2007


On Wed, 12 Dec 2007, Jian Zhang wrote:

> Now I have the forest plot data with x, y locations, and I measured the DBH for every indivicuals.
>
> The data looks like that:
> x=runif(100,0,100)
> y=runif(100,0,100)
> dbh=runif(100,1,100)
> rdata=data.frame(x,y,dbh)
>> rdata[1:5,]
>           x         y      dbh
> 1 99.5354145  1.412844 34.10112
> 2  0.8259361 87.737036 39.12914
> 3  6.5678613 65.699032 22.55990
> 4 67.2987881 72.053877 45.83978
> 5  2.2491372 23.622898 68.77065
>
> My question is: How can I plot the grid (25¡Á25,5¡Á5,...) figure by DBH? 
> Can you introduce a simply method to do it by R language? I know that it 
> can be done very easy by the software ArcGis.

Yes, but these are vector data, a DBH value at each point. To plot a grid 
(25 by 25, ...), you must assign the values to a grid, and there are lots 
of ways of doing that. Continuing the answer by Thierry Onkelinx:

set.seed(1)
x=runif(100,0,100)
y=runif(100,0,100)
dbh=runif(100,1,100)
rdata=data.frame(x,y,dbh)

library(sp)
coordinates(rdata) <- ~ x + y

grd <- GridTopology(c(2,2), c(4,4), c(25,25))
SP <- as(SpatialGrid(grd), "SpatialPixels")
whre <- overlay(SP, rdata)
SP_rdata <- SpatialPixelsDataFrame(SP, data=data.frame(dbh=rep(NA,
   25*25)))
SP_rdata$dbh[whre] <- rdata$dbh
# this takes the last dbh value is more than one falls in each grid cell
# so a bit more work is needed here
pts <- list("sp.points", rdata)
spplot(SP_rdata, "dbh", sp.layout=list(pts))

Or interpolate, and mask out empty cells:

library(gstat)
SP_rdata2 <- idw(dbh ~ 1, rdata, newdata=SP, idp=2.0, nmax=4)
is.na(SP_rdata2$var1.pred) <- !(1:625 %in% whre)
spplot(SP_rdata2, "var1.pred", sp.layout=list(pts))

for example.

Roger

>
> ¡¡¡¡¡¡¡¡
> ¡¡¡¡¡¡¡¡
> ¡¡¡¡¡¡¡¡
> ¡¡¡¡¡¡¡¡
>
>
> Thanks very much.
>
 	[[alternative HTML version deleted]]

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list