| 43 | It is possible to load now the reference observations for the spatio-temporal domain selected, using the same values in the corresponding arguments: |
| 44 | |
| 45 | {{{ |
| 46 | > ex2.obs <- loadECOMS(dataset = "WFDEI", var = "tasmin", lonLim = c(-15,35), latLim = c(32, 75), season = c(12,1,2), years = 2001:2010) |
| 47 | [2014-06-17 16:28:13] Defining homogeneization parameters for variable "tasmin" |
| 48 | [2014-06-17 16:28:13] Defining geo-location parameters |
| 49 | [2014-06-17 16:28:13] Defining time selection parameters |
| 50 | [2014-06-17 16:28:32] Done |
| 51 | > print(object.size(ex2.obs), units = "Mb") |
| 52 | 60.6 Mb |
| 53 | }}} |
| 54 | |
| 55 | This is the map of the observed mean minimum surface temperature observed for DJF 2001-2010: |
| 56 | |
| 57 | {{{ |
| 58 | > observed <- apply(ex2.obs$Data, FUN = mean, MARGIN = c(1,2)) |
| 59 | > x.obs <- ex2.obs$xyCoords$x |
| 60 | > y.obs <- ex2.obs$xyCoords$y |
| 61 | > image.plot(x.obs, y.obs, observed, asp = 1, xlab = "", ylab = "", main = "Mean minimum surface temp observed") |
| 62 | > world(add=TRUE) |
| 63 | }}} |
| 64 | |
| 65 | [[Image(image-20140617-163609.png)]] |
| 66 | |
| 67 | Note that WFDEI provides data for land areas only. |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
54 | | So 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: |
55 | | |
56 | | {{{ |
57 | | monsoon <- loadSeasonalForecast("CFS", var="tp", members=16, lonLim=c(65,92), latLim=c(5,37), season=6:9, years=1997) |
58 | | }}} |
59 | | |
60 | | The georeferencing of the data is stored as a `SpatialGrid`, which has several convenient attributes for an effective description of a gridded field, including the possibility of defining a coordinate reference system, highly useful -sometimes indispensable- for many geospatial operations. |
61 | | Plotting objects inheriting from this class or related classes (e.g. `SpatialGridDataFrame`) is straightforward using the `spplot` methods, as described in [wiki:../Trellis the previous example], but it is not directly usable as input for other plotting methods in R. |
62 | | Next, we present some typical plotting functions and how to extract the spatial coordinates in a suitable format for plotting. |
63 | | |
64 | | The total precipitation field is calculated as the total accumulated precipitation during the selected period for each model grid cell: |
65 | | |
66 | | {{{ |
67 | | tp <- colSums(monsoon$MemberData$Member_16) |
68 | | }}} |
69 | | |
70 | | And this is the classical way of displaying the data using the `spplot` method: |
71 | | |
72 | | {{{ |
73 | | sgdf <- SpatialGridDataFrame(monsoon$LonLatCoords, as.data.frame(tp)) |
74 | | data(world_map) |
75 | | wl <- as(world_map, "SpatialLines") |
76 | | wlines <- list("sp.lines", wl) |
77 | | spplot(sgdf, scales = list(draw = TRUE), sp.layout = list(wlines), col.regions = rev(topo.colors(41))) |
78 | | }}} |
79 | | |
80 | | [[Image(spplot.png)]] |
81 | | |
82 | | It is also straightforward to represent the precipitation using contour lines: |
83 | | |
84 | | {{{ |
85 | | spplot(sgdf, scales = list(draw = TRUE), contour = TRUE, col = "red", col.regions = rev(topo.colors(41))) |
86 | | }}} |
87 | | |
88 | | [[Image(spplot_contour.png)]] |
89 | | |
90 | | |
91 | | |
92 | | The 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. |
93 | | Essentially, 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. |
94 | | |
95 | | As 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. |
96 | | |
97 | | {{{ |
98 | | sgdf2xyz <- function(sgdf, zcol = 1) { |
99 | | coords <- coordinates(sgdf) |
100 | | z <- slot(sgdf, "data")[ ,zcol] |
101 | | aux <- cbind(coords, z) |
102 | | aux.ordered <- aux[order(aux[ ,2], aux[ ,1]), ] |
103 | | x <- unique(aux.ordered[ ,1]) |
104 | | y <- unique(aux.ordered[ ,2]) |
105 | | z <- t(matrix(aux.ordered[ ,3], nrow = length(y), ncol = length(x), byrow = TRUE)) |
106 | | xyz.list <- list("x" = x, "y" = y, "z" = z) |
107 | | return(xyz.list) |
108 | | } |
109 | | }}} |
110 | | |
111 | | We create the xyz object for data visualization using several basic R functions: |
112 | | |
113 | | {{{ |
114 | | xyz <- sgdf2xyz(sgdf) |
115 | | }}} |
116 | | |
117 | | == `image` function |
118 | | {{{ |
119 | | image(xyz, asp = 1, col = rev(topo.colors(21))) |
120 | | lines(wl) |
121 | | }}} |
122 | | |
123 | | [[Image(image.png)]] |
124 | | |
125 | | |
126 | | == `contour` function |
127 | | |
128 | | `contour` can be used alone or in combination with other plots by setting the argument `add = TRUE` |
129 | | |
130 | | {{{ |
131 | | par(mfrow = c(1,2)) |
132 | | contour(xyz, levels=seq(0,4000,200), labcex = 1.2) |
133 | | image(xyz, col = terrain.colors(21)) |
134 | | contour(xyz, col = "blue", add = TRUE) |
135 | | # Use dev.off() To restore the original par settings |
136 | | }}} |
137 | | |
138 | | [[Image(contour.png)]] |
139 | | |
140 | | == `filled.contour` function |
141 | | |
142 | | Filled contour produces a nice output with a graduated colorbar, but placing lines or other elements on the plot is not straightforward... |
143 | | |
144 | | |
145 | | [[Image(filled_contour.png)]] |
146 | | |