[R] How to draw partial grid in plot for spatial-binomial experiment?

Roger Bivand Roger.Bivand at nhh.no
Mon Dec 19 19:06:20 CET 2005


On Mon, 19 Dec 2005, Marc Schwartz (via MN) wrote:

> On Mon, 2005-12-19 at 11:17 -0200, Ruben Roa wrote:
> > DeaR comRades:
> > 
> > I have a 2D spatial binomial process as shown in the data and code below.
> > I am plotting the number of trials and the number of successes in the spatial
> > binomial experiments and would like to draw the spatial cells were the trials
> > and successes were counted, i.e. a partial grid in the plot only for those 
> > cells where there is a number. The cells are 2x2 km cells. The count of
> > Trials and Success should ideally appear in the middle of the square cell. 
> > I know there is the 'grid' package but it seems the plots made using 'graphics'
> > are not compatible with the plots made using 'grid' (as warned in the grid help
> > pages). Thanks in advance.
> > Ruben
> > 
> > "fri"<-structure(list(
> > coords=structure(c(606,606,608,608,608,608,608,610,610,610,610,610,610,610,612,612,612,612,612,612,614,614,
> > 614,614,614,614,614,614,614,616,616,616,616,616,616,616,618,618,618,618,618,620,620,620,622,624,
> > 4388,4390,4384,4386,4388,4390,4392,4380,4382,4384,4386,4388,4390,4392,4380,4382,4384,4386,4388,4390,4374,
> > 4376,4378,4380,4382,4384,4386,4388,4390,4372,4374,4376,4378,4380,4382,4384,4364,4366,4374,4376,4378,4368,
> > 4374,4376,4366,4366),.Dim=c(46,2)),
> > data=c(3,2,0,0,11,4,0,1,1,3,5,9,3,0,0,16,7,0,0,0,0,0,0,0,4,1,0,0,0,0,4,9,12,0,0,0,0,0,4,5,2,1,0,0,0,0),
> > units.m=c(4,6,1,1,12,7,1,2,3,4,5,11,5,2,2,17,8,1,1,1,1,1,1,3,6,4,2,2,1,2,8,11,15,1,1,1,2,1,8,6,5,1,2,2,1,1),),
> > class="geodata")
> > par(mfrow=c(1,2))
> > plot(fri$coords[,1],fri$coords[,2],type="n",xlab="Easting (km)",ylab="Northing (km)",main="Success")
> > text(fri$coords[,1],fri$coords[,2],format(fri$data),cex=.6)
> > plot(fri$coords[,1],fri$coords[,2],type="n",xlab="Easting (km)",ylab="Northing (km)",main="Trials")
> > text(fri$coords[,1],fri$coords[,2],format(fri$units.m),cex=.6)
> 
> 
> Is this what you want?:
> 
> 
> par(mfrow=c(1,2))
> 
> plot(fri$coords[,1],fri$coords[,2],type="n",
>      xlab="Easting (km)",ylab="Northing (km)",
>      main="Success")
> 
> text(fri$coords[,1],fri$coords[,2],format(fri$data),cex=.6)
> 
> # Use rect() to draw the grids around the values
> # The sides of each rectangle will be +/- 1 from the 
> # center point
> rect(fri$coords[,1] - 1, fri$coords[,2] - 1, 
>      fri$coords[,1] + 1 , fri$coords[,2] + 1)
> 
> 
> plot(fri$coords[,1],fri$coords[,2],type="n",
>      xlab="Easting (km)",ylab="Northing (km)",
>      main="Trials")
> 
> text(fri$coords[,1],fri$coords[,2],format(fri$units.m),cex=.6)
> 
> # Same here
> rect(fri$coords[,1] - 1, fri$coords[,2] - 1, 
>      fri$coords[,1] + 1 , fri$coords[,2] + 1)
> 
> 
> If so, see ?rect.

An alternative is to use functions in the sp package:

library(sp)
fri2 <- SpatialPoints(fri$coords)
fri2_SP <- SpatialPixels(fri2)
fri2_SPl <- as.SpatialPolygons.SpatialPixels(fri2_SP)
fri2_SPl_df <- SpatialPolygonsDataFrame(fri2_SPl, 
  data.frame(data=fri$data, units.m=fri$units.m, 
  row.names=IDvaluesSpatialPixels(fri2_SP)))
opar <- par(mfrow=c(1,2))
plot(fri2_SPl_df, axes=TRUE)
text(coordinates(fri2_SPl_df), label=format(fri2_SPl_df$data), cex=0.6)
title(xlab="Easting (km)", ylab="Northing (km)", main="Success")
plot(fri2_SPl_df, axes=TRUE)
text(coordinates(fri2_SPl_df), label=format(fri2_SPl_df$units.m), cex=0.6)
title(xlab="Easting (km)", ylab="Northing (km)", main="Trials")
par(opar)

which captures most of the polygon drawing for you. It has to go from 
points to pixels to polygons to induce the regular shapes.

Roger


> 
> The 'grid' packages is the basis of the lattice graphics functionality.
> It has nothing do to (directly) with drawing grid patterns on plots.
> 
> HTH,
> 
> Marc Schwartz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
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-help mailing list