Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedia committed Dec 20, 2017
2 parents 5ecd5b6 + 56793c4 commit bf54c7f
Show file tree
Hide file tree
Showing 21 changed files with 1,543 additions and 22 deletions.
16 changes: 11 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: visualizeR
Depends:
R(>= 3.1.0),
sm
sm,
transformeR(>= 1.2.0)
Imports:
transformeR,
methods,
fields,
RColorBrewer,
Expand All @@ -15,15 +15,21 @@ Imports:
SpecsVerification,
verification,
sp,
lattice
lattice,
latticeExtra,
magrittr,
padr,
grDevices,
data.table
Suggests:
loadeR.ECOMS,
loadeR,
easyVerification,
knitr
Type: Package
Title: Visualizing and Communicating Uncertainty in Seasonal Climate Prediction
Version: 1.0.0
Date: 2017-10-20
Version: 1.1.0
Date: 2017-12-20
Authors@R: as.person(c(
"Santander Meteorology Group <http://meteo.unican.es> [ctb]",
"Maria Dolores Frias <[email protected]> [aut, cre]",
Expand Down
33 changes: 33 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
export(MrQuantile)
export(QuantileProbs)
export(bubblePlot)
export(clim2sgdf)
export(detrend.data)
export(map.lines)
export(map.stippling)
export(reliabilityCategories)
export(rocss.fun)
export(seasMean)
export(skillMap)
export(spatialMean)
export(spatialPlot)
export(spreadPlot)
export(temporalPlot)
export(tercileBarplot)
export(tercileMap)
export(tercilePlot)
import(lattice)
import(latticeExtra)
import(methods)
import(sm)
import(verification)
Expand All @@ -20,8 +28,12 @@ importFrom(RColorBrewer,brewer.pal)
importFrom(RCurl,getURL)
importFrom(SpecsVerification,Auc)
importFrom(abind,abind)
importFrom(abind,asub)
importFrom(data.table,rleid)
importFrom(fields,image.plot)
importFrom(grDevices,col2rgb)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,colors)
importFrom(grDevices,gray)
importFrom(grDevices,grey.colors)
importFrom(grDevices,rgb)
Expand All @@ -40,29 +52,50 @@ importFrom(graphics,points)
importFrom(graphics,polygon)
importFrom(graphics,text)
importFrom(graphics,title)
importFrom(lattice,panel.grid)
importFrom(lattice,panel.polygon)
importFrom(lattice,panel.xyplot)
importFrom(lattice,xyplot)
importFrom(magrittr,"%>%")
importFrom(mapplots,add.pie)
importFrom(mapplots,draw.pie)
importFrom(padr,pad)
importFrom(scales,alpha)
importFrom(sp,GridTopology)
importFrom(sp,Line)
importFrom(sp,Lines)
importFrom(sp,Polygon)
importFrom(sp,Polygons)
importFrom(sp,SpatialGridDataFrame)
importFrom(sp,SpatialLines)
importFrom(sp,SpatialPoints)
importFrom(sp,SpatialPointsDataFrame)
importFrom(sp,SpatialPolygons)
importFrom(sp,over)
importFrom(sp,spplot)
importFrom(stats,coef)
importFrom(stats,complete.cases)
importFrom(stats,filter)
importFrom(stats,lm)
importFrom(stats,median)
importFrom(stats,na.omit)
importFrom(stats,qnorm)
importFrom(stats,quantile)
importFrom(transformeR,aggregateGrid)
importFrom(transformeR,array3Dto2Dmat)
importFrom(transformeR,climatology)
importFrom(transformeR,draw.world.lines)
importFrom(transformeR,getCoordinates)
importFrom(transformeR,getDim)
importFrom(transformeR,getGrid)
importFrom(transformeR,getShape)
importFrom(transformeR,getYearsAsINDEX)
importFrom(transformeR,interpGrid)
importFrom(transformeR,isRegular)
importFrom(transformeR,makeMultiGrid)
importFrom(transformeR,mat2Dto3Darray)
importFrom(transformeR,redim)
importFrom(transformeR,subsetGrid)
importFrom(utils,head)
importFrom(utils,packageDescription)
importFrom(utils,tail)
9 changes: 5 additions & 4 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
visualizeR 1.0.0
visualizeR 1.1.0
=================

* Internal adaptation of reliabilityCategories to be consistent with the other functions.
* Implemented the ROCSS significance in tercilePlot and tercileBarplot.
* Added CITATION file to Environmental Modelling & Software.
* New function `spatialPlot` (essentially a renaming of `transformeR::plotClimatology`, the latter planned to be deprecated in future `transformeR` releases)
* New plotting functions for seasonal forecast products:
* `tercileMap` for forecast tercile maps (based on C3S seasonal forecast graphical products, http://climate.copernicus.eu/s/charts/c3s_seasonal/)
* `skillMap`: A convenient wrapper of `SpatialPlot` and `map.stippling` for plotting skill maps
* Other minor changes and documentation updates.
1 change: 1 addition & 0 deletions R/auxiliaryFun.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ getSeason.S4 <- function(obj) {
#' @return The ROC area skill score and the significance (TRUE or FALSE)
#' @author M. D. Frias \email{mariadolores.frias@@unican.es} and J. Fernandez
#' @importFrom SpecsVerification Auc
#' @importFrom stats qnorm
#' @export
rocss.fun <- function (obs, pred, conf.level = 0.95){
no.nan <- complete.cases(obs, pred)
Expand Down
13 changes: 8 additions & 5 deletions R/reliabilityCategories.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#' @param n.bins (optional): number of probability bins considered. By default n.bins = 10
#' @param n.boot number of samples considered for bootstrapping. By default n.boot = 100
#' @param conf.level Confidence interval for the reliability line. By default \code{conf.level} = 0.75 (two sided), as in Weisheimer et al. 2014
#' @param na.rate Allowed proportion of NA values in each region. Regions with proportions higher than na.rate
#' are excuded from the analysis. Default is 0.75.
#' @param diagrams Logical (default = TRUE). Plotting results.
#' @param cex0 numeric (default is 0.5). Minimum size of the points shown in the reliability diagrams, i.e. size of the point
#' for the minimum n frequency (n = 1) (see parameter \code{n.bins}. The sizes for points that correspond to n > 1
Expand Down Expand Up @@ -108,6 +110,7 @@ reliabilityCategories <- function(hindcast,
n.bins = 10,
n.boot = 100,
conf.level = 0.75,
na.rate = 0.75,
diagrams = TRUE,
cex0 = 0.5,
cex.scale = 20,
Expand Down Expand Up @@ -166,7 +169,8 @@ reliabilityCategories <- function(hindcast,
for(l in 1:length(regs)){
o <- over(spoints, regs[l,])
w <- which(!is.na(o))
if(length(w)>0){
if(1-length(w)/length(o) > na.rate) w <- NULL
if(length(w) > 0) {
ob <- ob.full[, w, drop = F]
se <- array(dim = c(nmem, ntime, length(w)))
for(i in 1:nmem){
Expand Down Expand Up @@ -265,8 +269,6 @@ reliabilityCategories <- function(hindcast,
at = c(1, 2, 2.75, 3.25, 4, 5),
labels = c("dangerously useless", "not useful","marginally useful",
"marginally useful +","still useful","perfect"))))

print(pc)
}else{
x1 <- 1/n.events
y1 <- 1/n.events
Expand All @@ -289,7 +291,7 @@ reliabilityCategories <- function(hindcast,


# Customized Lattice Example
xyp <- xyplot(y~x|z, par.strip = list(lines = 1), ylim = c(0,1), strip = strip.custom(fg = rgb(0,0,0,0), strip.names = c(T,F), strip.levels = c(F,T), factor.levels = labels),
pc <- xyplot(y~x|z, par.strip = list(lines = 1), ylim = c(0,1), strip = strip.custom(fg = rgb(0,0,0,0), strip.names = c(T,F), strip.levels = c(F,T), factor.levels = labels),
scales=list(x = list(at = seq(0,1,round(1/n.bins, digits = 2)),
labels = seq(0,1,round(1/n.bins, digits = 2))),
y = list(at = seq(0,1,round(1/n.bins, digits = 2)),
Expand Down Expand Up @@ -336,7 +338,7 @@ reliabilityCategories <- function(hindcast,
layout = layout,
xlab = "Predicted probability", ylab= "Observed frequency",
main= list(cex = 1, font = 1, label = sprintf("n = %d years x %d points", nyear, npoint)))
print(xyp)

# update(xyp, par.settings = list(fontsize = list(text = 8, points = 10)))

#########################################################
Expand Down Expand Up @@ -394,6 +396,7 @@ reliabilityCategories <- function(hindcast,
###########################################################

}
print(pc)
}
result.grid <- mg
attr(result.grid$Data, "dimensions") <- c("cat", "var", "member", "time", "lat", "lon")
Expand Down
144 changes: 144 additions & 0 deletions R/skillMap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# skillMap.R Skill maps for seasonal forecasts
#
# Copyright (C) 2017 Santander Meteorology Group (http://www.meteo.unican.es)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#' @title A wrapper of \code{spatialPlot} for the creation of verification maps for seasonal forecast systems
#'
#' @description A wrapper of \code{spatialPlot} for the creation of verification maps for seasonal forecast systems.
#' It provides a convenient interface for \code{\link{map.stippling}}
#'
#'
#' @param easyVeriGrid A climatological grid with a verification output. See details
#' @param stippling A key-value list of arguments passed to \code{map.stippling}:
#' \code{threshold} and \code{condition}. Ignored by default, returning a map without stippling.
#' @param stippling.point.options Default to \code{NULL}. Further graphical arguments passed to points
#' (e.g. \code{cex}, \code{pch} etc.)
#' @param backdrop.theme See \code{\link{spatialPlot}}
#' @param title Title of the plot
#' @param ... Further graphical options passed to \code{\link{spatialPlot}}.
#'
#' @details The function applies the \code{\link{spatialPlot}} function, that in turn uses \code{lattice-methods}.
#'
#' \strong{Graphical options}
#'
#' Some examples of specific map graphical options are available in the help of function \code{\link[sp]{spplot}}.
#' In addtion, fine-tuning of the resulting plots can be obtained using the arguments of \pkg{lattice} plots. For
#' an overview, see the help of function \code{\link[lattice]{xyplot}}.
#'
#' @seealso The bridging function \code{\link[transformeR]{easyVeri2grid}} from package \pkg{transformeR} allows for
#' the conversion of verification outputs from package \pkg{easyVerification} to the \code{climate4R} data structure
#' used by the function.
#'
#' Many different aspects of the plot can be controlled passing the relevant arguments to
#' \code{\link[sp]{spplot}}.
#'
#' @return A lattice plot of class \dQuote{trellis}.
#'
#' @author J. Bedia
#' @export
#' @examples \dontrun{
#'
#' # The package 'easyVerification' will be used to calculate the RPSS:
#'
#' require(easyVerification)
#' require(transformeR)
#' # First of all, a data subset is done, considering a spatial domain centered on the North Atlantic:
#' tas.cfs2 <- subsetGrid(tas.cfs, lonLim = c(-100, 40), latLim = c(-5, 75))
#' # The same is done with the reanalysis dataset:
#' tas.ncep2 <- subsetGrid(tas.ncep, lonLim = c(-100, 40), latLim = c(-5, 75))
#' # In the next step, the reanalysis data are interpolated to the hindcast grid:
#' tas.ncep2.int <- interpGrid(tas.ncep2, new.coordinates = getGrid(tas.cfs2), method = "nearest")
#' # We compute the Ranked Probabiloity SKill Score using veriApply, and a cross-validation strategy:
#' ev <- easyVerification::veriApply(verifun = "EnsRpss",
#' fcst = tas.cfs2$Data,
#' obs = tas.ncep2.int$Data,
#' prob = 1:2/3,
#' tdim = 2,
#' ensdim = 1,
#' parallel = TRUE,
#' ncpus = 3,
#' strategy = "crossval")
#' # The bridging function 'easyVeri2grid' converts the object returned by 'veriApply'
#' # to a 'climate4R' climatological grid:
#'
#' easyVeriGrid <- easyVeri2grid(ev$skillscore,
#' obs.grid = tas.ncep2.int,
#' verifun = "EnsRpss")
#'
#' # A basic RPSS map, using the spatialPlot defaults:
#' skillMap(easyVeriGrid = easyVeriGrid, backdrop.theme = "coastline")
#'
#' # Stippling. Mark significant RPSS values at the 95% ci
#' thresh <- ev$skillscore.sd*qnorm(0.95)
#'
#' skillMap(easyVeriGrid = easyVeriGrid,
#' stippling = list(threshold = th, condition = "GT"), # GT = greater than
#' stippling.point.options = list(pch = 19, cex = .2, col = "black"),
#' backdrop.theme = "coastline")
#'
#' # Further customization: For instance a more elaborated title, colorblind-friendly palette etc.:
#'
#' cb.colors <- colorRampPalette(rev(RColorBrewer::brewer.pal(11, "Spectral")))
#' skillMap(easyVeriGrid = easyVeriGrid,
#' stippling = list(threshold = th, condition = "GT"),
#' stippling.point.options = list(pch = 19, cex = .2, col = "black"),
#' backdrop.theme = "coastline",
#' at = seq(-0.7, 0.7, .05),
#' set.max = 0.7, set.min = -0.7,
#' scales = list(draw = TRUE, alternating = 3),
#' colorkey = list(space = "bottom"),
#' col.regions = cb.colors(51),
#' title = paste("Ranked Probability Skill Score",
#' "Forecasting System: CFSv2 - 24 members",
#' "November Initializations",
#' "Observing System: NCEP/NCAR Reanalysis 1",
#' "t2m DJF 1983-2010", sep = "\n")
#' )
#' }

skillMap <- function(easyVeriGrid,
stippling = list(threshold = NULL, condition = NULL),
stippling.point.options = NULL,
backdrop.theme = NULL,
title = NULL,
...) {
arg.list <- list(...)
arg.list[["grid"]] <- easyVeriGrid
if (!is.null(stippling$threshold)) {
stippling[["clim"]] <- easyVeriGrid
if (!is.null(stippling.point.options)) {
stippling <- c(stippling, stippling.point.options)
}
stip.list <- list(do.call("map.stippling", stippling))
if ("sp.layout" %in% names(arg.list)) {
arg.list[["sp.layout"]] <- c(arg.list[["sp.layout"]], stip.list)
} else {
arg.list[["sp.layout"]] <- stip.list
}
}
if (!is.null(backdrop.theme)) {
arg.list[["backdrop.theme"]] <- backdrop.theme
}
if (!is.null(title)) {
arg.list[["main"]] <- list(label = title,
cex = .9,
col = "blue",
font = 1,
just = "left", x = 0.05)
}
map <- do.call("spatialPlot", arg.list)
print(map)
}
Loading

0 comments on commit bf54c7f

Please sign in to comment.