Skip to content

Commit 4bb020c

Browse files
committed
update plotFun.heat
1 parent a96fb7c commit 4bb020c

File tree

5 files changed

+77
-58
lines changed

5 files changed

+77
-58
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: plotFun
22
Type: Package
33
Title: Helper functions for plotting data
4-
Version: 1.0.5
5-
Date: 2019-07-01
4+
Version: 1.0.6
5+
Date: 2019-08-14
66
Author: A. Casanueva
77
Maintainer: A.Casanueva <ana.casanueva@meteoswiss.ch>
88
Description: Helper functions for plotting data.

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Changes in v1.0.6
2+
3+
* Allow plotFun.heat to plot any data in in the coloures markers.
4+
15
Changes in v1.0.5
26

37
* Add documentation.

R/plotFun.heat.R

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
#' Plot heat index as a function of the input variables.
22
#'
3-
#' The index is calculated with the R package \code{HeatStress}.
3+
#' The index is calculated with the R package \code{HeatStress} or provided by the user.
44
#'
5-
#' @param hu vector with relative humidity data. \code{hu} or \code{td} are mandatory.
6-
#' @param td vector with dew point temperature data.\code{hu} or \code{td} are mandatory.
7-
#' @param ta vector with air temperature data.
8-
#' @param heat.index heat index to plot as a function of dew point/relative humidity and air temperature. Available (based on 2 variables): swbgt, hi, wbt.Stull, wbgt.Bernard, apparentTemp, effectiveTemp, humidex, discomInd.
5+
#' @param hu vector with data for the variable to be plotted in the X-axis or relative humidity data. \code{hu} or \code{td} are mandatory.
6+
#' @param td vector with data for the variable to be plotted in the X-axis or dew point temperature data.\code{hu} or \code{td} are mandatory.
7+
#' @param ta vector with with data for the variable to be plotted in the Y-axis or temperature data.
8+
#' @param heat.index vector with data to plot with colour markers or character with the heat index to plot as a function of the two input variables. Available (based on 2 variables): swbgt, hi, wbt.Stull, wbgt.Bernard, apparentTemp, effectiveTemp, humidex, discomInd.
99
#' @param xlim 2-element vector with X-axis limits for the density plot. Default: range of hu/td.
1010
#' @param ylim 2-element vector with Y-axis limits for the density plot. Default: range of ta.
1111
#' @param breaks.index vector of breaks for the heat index values. By default, breaks is adjusted to the minimum and maximum values of the index.
12-
#' @param n.bins number of bins for kernel density calculation. Default= 500
13-
#' @param add.contours logical. Add (default) or not contours for heat index values.
14-
#' @param add.points logical. Add (default) or not points with the actual time series.
12+
#' @param n.bins number of bins for kernel density calculation, only when 'heat.index' is a character. Default= 500
13+
#' @param add.contours logical. Add (default) or not contours for heat index values. It is only valid when 'heat.index' is a character.
14+
#' @param add.points logical. Add (default) or not points with the actual time series. It is only valid when 'heat.index' is a character
1515
#' @param xlab X-axis label.
1616
#' @param ylab Y-axis label.
1717
#' @param title title above the plot.
1818
#' @param unit.text character string to be placed in the colorbar with the units.
1919
#' @param cex.main Relative size of the plot title. Default:1.5.
2020
#' @param cex.unit numeric value giving the expansion factor of the units text. Default:1.
2121
#' @param cex.textcbar numeric value giving the expansion factor of the colorbar text. Default:1.3.
22-
#' @return Plot with the heat index values as a function of the input variables.
23-
#' @details The two input variables of the desired index need to be provided, tas and either td or hu. Needed packages: HeatStress, RColorBrewer.
22+
#' @return Scatter (if 'heat.index' is a vector) or image (if 'heat.index' is a character) plot with the heat index values as a function of the input variables.
23+
#' @details When 'heat.index' is a character, the index is calculated and the two input variables of the desired index need to be provided, tas and either td or hu. Needed packages: HeatStress, RColorBrewer.
24+
#' When 'heat.index' is a vector, the variable to be plotted in the X-axis should be included in 'hu' or 'td' and the plotted in the Y-axis in 'ta'.
2425
#' @import HeatStress RColorBrewer
2526
#' @author Ana Casanueva, 13.12.2018
2627
#' @export plotFun.heat
@@ -46,44 +47,51 @@
4647
plotFun.heat <- function(hu=NULL, td=NULL, ta, xlim=NULL, ylim=range(ta, na.rm=T), heat.index=NULL, breaks.index=NULL, title=NULL, cex.main=1.5, xlab=NULL, ylab=NULL, n.bins=500, add.points=TRUE, add.contours=TRUE, unit.text=NULL, cex.unit=1, cex.textcbar=1.3){
4748

4849

49-
if(n.bins>100) message("Patience: number of bins is ", n.bins)
50-
51-
assertthat::assert_that(any(match(heat.index, c("wbt", "wbgt.shade", "hi", "swbgt", "humidex", "discomInd","apparentTemp","effectiveTemp"), nomatch=FALSE)), msg = "Check heat index name.")
52-
53-
assertthat::assert_that((!is.null(hu) | !is.null(td)), msg="td or hu is mandatory")
54-
if(any(match(heat.index, c('swbgt', 'hi', 'wbt', 'apparentTemp', 'effectiveTemp', 'humidex', 'discomInd')), na.rm=T)) assertthat::assert_that(!is.null(hu), msg="hu is missing")
55-
if(match(heat.index, 'wbgt.shade', nomatch = F)) assertthat::assert_that(!is.null(td), msg="td is missing")
56-
57-
if(is.null(unit.text)){
58-
if(any(match(heat.index, c('swbgt', 'wbgt.shade', 'wbt', 'apparentTemp', 'effectiveTemp', 'humidex', 'discomInd')), na.rm=T)) unit.text <- "\260C"
59-
if(match(heat.index, 'hi', nomatch = F)) unit.text <- "\260F"
50+
if(is.character(heat.index) & n.bins>100) message("Patience: number of bins is ", n.bins)
51+
52+
if(is.character(heat.index)){
53+
assertthat::assert_that(any(match(heat.index, c("wbt", "wbgt.shade", "hi", "swbgt", "humidex", "discomInd","apparentTemp","effectiveTemp"), nomatch=FALSE)), msg = "Check heat index name.")
54+
55+
#assertthat::assert_that((!is.null(hu) | !is.null(td)), msg="td or hu is mandatory")
56+
if(any(match(heat.index, c('swbgt', 'hi', 'wbt', 'apparentTemp', 'effectiveTemp', 'humidex', 'discomInd')), na.rm=T)) assertthat::assert_that(!is.null(hu), msg="hu is missing")
57+
if(match(heat.index, 'wbgt.shade', nomatch = F)) assertthat::assert_that(!is.null(td), msg="td is missing")
58+
59+
if(is.null(unit.text)){
60+
if(any(match(heat.index, c('swbgt', 'wbgt.shade', 'wbt', 'apparentTemp', 'effectiveTemp', 'humidex', 'discomInd')), na.rm=T)) unit.text <- "C"
61+
if(match(heat.index, 'hi', nomatch = F)) unit.text <- "F"
62+
}
6063
}
61-
64+
6265
if(!is.null(td)) xvar <- td
6366
if(!is.null(hu)) xvar <- hu
64-
xlim <- range(xvar, na.rm=T)
67+
if(is.null(xlim)) xlim <- range(xvar, na.rm=T)
6568

6669
# *** Prepare data to plot ***
6770
x <- seq(floor(xlim[1]), ceiling(xlim[2]), length=n.bins) # refers to hu/td
6871
y <- seq(floor(ylim[1]), ceiling(ylim[2]), length=n.bins) # refers to ta
6972

70-
mat <- matrix(NA, ncol=length(x), nrow=length(y))
71-
for(i in 1:length(y)){
72-
for(j in 1:length(x)){
73-
if(heat.index=="wbt") mat[i,j]<- wbt.Stull(y[i], x[j])
74-
if(heat.index=="wbgt.shade") mat[i,j]<- wbgt.Bernard(y[i], x[j])$data
75-
if(heat.index=="apparentTemp") mat[i,j]<- apparentTemp(y[i], x[j])
76-
if(heat.index=="effectiveTemp") mat[i,j]<- effectiveTemp(y[i], x[j])
77-
if(heat.index=="humidex") mat[i,j]<- humidex(y[i], x[j])
78-
if(heat.index=="discomInd") mat[i,j]<- discomInd(y[i], x[j])
79-
if(heat.index=="swbgt") mat[i,j]<- swbgt(y[i], x[j])
80-
if(heat.index=="hi") mat[i,j]<- hi(y[i], x[j])
81-
}
73+
if(is.character(heat.index)){
74+
mat <- matrix(NA, ncol=length(x), nrow=length(y))
75+
for(i in 1:length(y)){
76+
for(j in 1:length(x)){
77+
if(heat.index=="wbt") mat[i,j]<- wbt.Stull(y[i], x[j])
78+
if(heat.index=="wbgt.shade") mat[i,j]<- wbgt.Bernard(y[i], x[j])$data
79+
if(heat.index=="apparentTemp") mat[i,j]<- apparentTemp(y[i], x[j])
80+
if(heat.index=="effectiveTemp") mat[i,j]<- effectiveTemp(y[i], x[j])
81+
if(heat.index=="humidex") mat[i,j]<- humidex(y[i], x[j])
82+
if(heat.index=="discomInd") mat[i,j]<- discomInd(y[i], x[j])
83+
if(heat.index=="swbgt") mat[i,j]<- swbgt(y[i], x[j])
84+
if(heat.index=="hi") mat[i,j]<- hi(y[i], x[j])
85+
}
86+
}
87+
df <- list(x=x, y =y, z=mat)
8288
}
83-
df <- list(x=x, y =y, z=mat)
84-
89+
if(is.numeric(heat.index)){
90+
df <- list(x=xvar, y =ta, z=heat.index)
91+
}
92+
8593
# *** Plot settings ***
86-
if(is.null(breaks.index)) breaks.index <- seq(floor(min(mat, na.rm=T)), ceiling(max(mat, na.rm=T)))
94+
if(is.null(breaks.index)) breaks.index <- seq(floor(min(df$z, na.rm=T)), ceiling(max(df$z, na.rm=T)))
8795
my.palette <- rev(brewer.pal(n = 11, name = "Spectral"))
8896
lev <- length(breaks.index)-1;
8997
cols <- colorRampPalette(my.palette)(lev)
@@ -96,20 +104,26 @@ plotFun.heat <- function(hu=NULL, td=NULL, ta, xlim=NULL, ylim=range(ta, na.rm=T
96104
heights=c(0.9,0.05),
97105
widths=c(0.08,0.78,0.15))
98106

99-
df$z[df$z>max(breaks.index)] <- max(breaks.index) # otherwise values below or above are not plotted
100-
df$z[df$z<min(breaks.index)] <- min(breaks.index)
101-
image(df, breaks=breaks.index, col=cols, las=1, cex.axis=1.5, main=title, cex.main=cex.main)
102-
103-
if(add.contours) contour(df, levels=breaks.index, add=T, col="slategrey", xlim= xlim, ylim=ylim, xaxs="i", yaxs="i" )
104-
if(add.points) points(xvar, ta, pch=21, xaxs="i", yaxs="i", xlim= xlim, ylim=ylim, las=1, cex=1)
107+
if(is.character(heat.index)){
108+
df$z[df$z>max(breaks.index)] <- max(breaks.index) # otherwise values below or above are not plotted
109+
df$z[df$z<min(breaks.index)] <- min(breaks.index)
110+
image(df, breaks=breaks.index, col=cols, las=1, cex.axis=1.5, main=title, cex.main=cex.main)
111+
if(add.contours) contour(df, levels=breaks.index, add=T, col="slategrey", xlim= xlim, ylim=ylim, xaxs="i", yaxs="i" )
112+
if(add.points) points(xvar, ta, pch=21, xaxs="i", yaxs="i", xlim= xlim, ylim=ylim, las=1, cex=1)
113+
114+
} else{
115+
class <- classInt::classIntervals(heat.index, lev, style = "fixed", fixedBreaks=breaks.index)
116+
colcode <- classInt::findColours(class,cols)
117+
plot(df$x, df$y, bg=colcode, pch=21, xaxs="i", yaxs="i", xlim= xlim, ylim=ylim,las=1, cex=2, lwd=1, xlab=NA, ylab=NA)
118+
}
105119

106120
# add labels for the axis
107121
par(mar=c(0.1,2,0.2,0.5))
108122
plot(c(0,1),c(0,1),type="n",axes=F, ann=F)
109123
text(0.6,0.5,ylab, srt=90,cex=1.5)
110124
plot(c(0,1),c(0,1),type="n",axes=F, ann=F)
111125
text(0.55,0.5,xlab, cex=1.5)
112-
126+
113127
# add color bar
114128
par(mar=c(3,0.6,3,4.5))
115129
plotFun.colorbar(round(breaks.index,4), palette=cols, unit.text, cex.unit, cex.textcbar)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ See e.g. ```?plotFun.stn``` for examples.
3030
[![DOI](https://zenodo.org/badge/165884416.svg)](https://zenodo.org/badge/latestdoi/165884416)
3131

3232
This package was used in the following publications:
33-
* Casanueva et al. 2019. Climate projections of a multi-variate heat stress index: the role of downscaling and bias correction, *Geoscientific Model Development*, https://www.geosci-model-dev-discuss.net/gmd-2018-294/
33+
* Casanueva et al. 2019. Climate projections of a multi-variate heat stress index: the role of downscaling and bias correction, *Geoscientific Model Development*, https://www.geosci-model-dev.net/12/3419/2019/
3434
* Casanueva et al. 2019. Escalating environmental heat exposure – a future threat for the European workforce, *Regional Environmental Change*.

man/plotFun.heat.Rd

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)