[R-sig-Geo] contours of a SpatialPixelsDataFrame?

Roger Bivand Roger.Bivand at nhh.no
Wed Sep 27 17:51:07 CEST 2006


On Wed, 27 Sep 2006, Horacio Samaniego wrote:

> I am unsure on how to deal with this... (this is usually easy on  
> grass), however cuz all me figs have been done in R I need to be  
> consistent.
> 
> The question is how to to get a contour plot from a  
> SpatialPixelsDataFrame.
> 
> So far I have generated I seem to be succesfully generating the plot  
> using :
>  >    contour(seq(-2423411.1,271933.9,length=674),seq 
> (248129.5,3239402.6,length=748),matrix(grPC at data$PC1,ncol=748))
> 
> but the plot seem to be upside down. It looks ok, but wen adding the  
> map lines with :
>  > plot(usaca,add=T)
> 
> I notice that only the 'y' axis is shifted.... a transformation of  
> the matrix won't do the trick.  So, I wonder is what the best  
> approach is for this. I tried making an image of it, but thinking  
> that  as.image.SpatialGridDataFrame will create a usable matrix don't  
> seem to be quite right
> 

No, because it creates the input list of x, y, z used by image() and 
contour - the z component is the matrix.

> anyhow, I hope that some more experienced user will be able to tell  
> me the correct way to do this,
> 
> thanks,
> 

It's not hard (from the example for image.SpatialGridDataFrame):

data(meuse.grid)
coordinates(meuse.grid) = c("x", "y")
gridded(meuse.grid) = TRUE
class(meuse.grid) # is a "SpatialPixelsDataFrame"
xyz <- as.image.SpatialGridDataFrame(meuse.grid["dist"]) # choose the 
                                          # variable you need
image(meuse.grid["dist"], axes=TRUE)
contour(xyz, add=TRUE)

(if you just do contour() on the matrix, the coordinates and aspect get 
blown away) and if you want the contours as a SpatialLinesDataFrame:

SLDF <- contourLines2SLDF(contourLines(xyz))
image(meuse.grid["dist"], axes=TRUE)
plot(SLDF, add=TRUE)

Roger

> 
> PD: here is the definition of my sp object:
> 
>  > str(grPC)
> Formal class 'SpatialPixelsDataFrame' [package "sp"] with 7 slots
>    ..@ data       :Formal class 'AttributeList' [package "sp"] with 1  
> slots
>    .. .. ..@ att:List of 6
>    .. .. .. ..$ PC1: num [1:504152] 0.299 0.301 0.302 0.303 0.305 ...
>    .. .. .. ..$ PC2: num [1:504152] -0.218 -0.219 -0.220 -0.220  
> -0.221 ...
>    .. .. .. ..$ PC3: num [1:504152] -0.0222 -0.0226 -0.0230 -0.0233  
> -0.0237 ...
>    .. .. .. ..$ PC4: num [1:504152] -0.0290 -0.0288 -0.0286 -0.0285  
> -0.0283 ...
>    .. .. .. ..$ PC5: num [1:504152] 0.0607 0.0608 0.0609 0.0610  
> 0.0611 ...
>    .. .. .. ..$ PC6: num [1:504152] 0.0681 0.0683 0.0685 0.0687  
> 0.0688 ...
>    ..@ coords.nrs : num(0)
>    ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3  
> slots
>    .. .. ..@ cellcentre.offset: Named num [1:2] -2421412   250129
>    .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>    .. .. ..@ cellsize         : Named num [1:2] 3999 3999
>    .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>    .. .. ..@ cells.dim        : Named int [1:2] 674 748
>    .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>    ..@ grid.index : int [1:504152] 1 2 3 4 5 6 7 8 9 10 ...
>    ..@ coords     : num [1:504152, 1:2] -2421412 -2417413 -2413414  
> -2409414 -2405415 ...
>    .. ..- attr(*, "dimnames")=List of 2
>    .. .. ..$ : NULL
>    .. .. ..$ : chr [1:2] "s1" "s2"
>    ..@ bbox       : num [1:2, 1:2] -2423411   248130   271934  3239403
>    .. ..- attr(*, "dimnames")=List of 2
>    .. .. ..$ : chr [1:2] "s1" "s2"
>    .. .. ..$ : chr [1:2] "min" "max"
>    ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>    .. .. ..@ projargs: chr NA
> 
> --
> Horacio Samaniego
> Dept. Biology
> University of New Mexico
> Albuquerque 87106, NM
> 
> http://www.unm.edu/~horacio
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
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