Changes between Version 3 and Version 4 of udg/ecoms/RPackage/examples/continentalSelection


Ignore:
Timestamp:
Feb 20, 2014 3:05:19 PM (8 years ago)
Author:
juaco
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • udg/ecoms/RPackage/examples/continentalSelection

    v3 v4  
    1 == Alternative visualization tools: Monsoon in the Indian subcontinent
     1= Alternative visualization tools: Monsoon in the Indian subcontinent
    22
    33So far we have shown plotting examples using the trellis plots generated by the spplot `method`. In this examples we show alternative plotting options using more standard R plotting functions for gridded data. To this aim, we load the precipitation data of 1997 for the lead month 1 forecast over the Indian subcontinent, considering the monsoon season from June to September:
     
    3939
    4040
     41The help files of functions `image` and `contour` for instance, indicate the type of data structure required for displaying three-dimensional or spatial data (''images'') by many R standard functions.
     42Essentially, this is a list of elements specifying the x and y positions of the elements to be displayed as a grid, being the field z a matrix of values whose positions coincide with those of the x and y elements.
    4143
     44As an illustration, the following function produces such a list from an input `SpatialGridDataFrame`, by indicating also the corresponding data column to be represented, as in the argument `zcol` of `spplot`. This function takes care of the appropriate ordering of the data for spatial consistency. Note that by default, if `zcol` is omitted, the function will represent the first column of the data slot.
     45
     46{{{
     47sgdf2xyz <- function(sgdf, zcol = 1) {
     48      coords <- coordinates(sgdf)
     49      z <- slot(sgdf, "data")[ ,zcol]
     50      aux <- cbind(coords, z)
     51      aux.ordered <- aux[order(aux[ ,2], aux[ ,1]), ]
     52      x <- unique(aux.ordered[ ,1])
     53      y <- unique(aux.ordered[ ,2])
     54      z <- t(matrix(aux.ordered[ ,3], nrow = length(y), ncol = length(x), byrow = TRUE))
     55      xyz.list <- list("x" = x, "y" = y, "z" = z)
     56      return(xyz.list)
     57}
     58}}}
     59
     60We create the xyz object for data visualization using several basic R functions:
     61
     62{{{
     63xyz <- sgdf2xyz(sgdf)
     64}}}
     65
     66== `image` function
     67{{{
     68image(xyz, asp = 1, col = rev(topo.colors(21)))
     69lines(wl)
     70}}}
     71
     72[[Image(image.png)]]
     73
     74
     75== `contour` function
     76
     77`contour` can be used alone or in combination with other plots by setting the argument `add = TRUE`
     78
     79{{{
     80par(mfrow = c(1,2))
     81contour(xyz, levels=seq(0,4000,200), labcex = 1.2)
     82image(xyz, col = terrain.colors(21))
     83contour(xyz, col = "blue", add = TRUE)
     84# Use dev.off() To restore the original par settings
     85}}}
     86
     87[[Image(contour.png)]]