Update to metvurst

Here's a quick update to metvurst in response to some issues encountered over the last weeks.

The most important change is that the strip() function now returns the plot object rather than printing it. This means that we can work with it afterwards. Given that all plotting is implemented using lattice/latticeExtra this seems natural and is now behaving accordingly. To highlight one advance of this new behaviour, let's revisit the earlier post, where I introduced the function and modify it slightly so that we end up with two graphs on one page to facilitate comparisons between variables.

First, we need some data (again from Fiji – this time only for 1993 and 1994)

## LOAD METVURST PACKAGE
library(metvurst)

## SET URL FOR DATA DOWNLOAD
url <- "http://www.bom.gov.au/ntc/IDO70004/IDO70004_"

## YEARS TO BE DOWNLOADED
yr <- 1993:1994

## READ DATA FOR ALL YEARS FROM URL INTO LIST
fijilst <- lapply(seq(yr), function(i) {
  read.csv(paste(url, yr[i], ".csv", sep = ""), na.strings = c(-9999, 999))
})

## TURN LIST INTO COMPLETE DATAFRAME AND CONVERT NA STRINGS TO NAs
fiji <- do.call("rbind", fijilst)
fiji[fiji == -9999.00] <- NA
fiji[fiji == -9999.0] <- NA
fiji[fiji == 999.0] <- NA

## CREATE POSIX DATETIME AND CONVERT UTC TO LOCAL FIJI TIME
Sys.setenv(TZ = "UTC") # set environment to UTC before conversion
dts <- as.POSIXct(strptime(fiji$Date...UTC.Time, 
                           format = "%d-%b-%Y %H:%M")) + 12 * 60 * 60

## CREATE CONDITIONING VARIABLE (IN THIS CASE YEAR)
year <- substr(as.character(dts), 1, 4)

We are now able to create plot objects (i.e. store the visualisation in an object) rather than print them straight away and use them later…

## CREATE STRIP FOR WATER TEMPERATURE
plot.water.temp <- strip(x = fiji$Water.Temperature, 
                         date = dts,
                         cond = year,
                         arrange = "long",
                         main = "Water Temperature [°C]")

## CREATE STRIP FOR AIR TEMPERATURE
plot.air.temp <- strip(x = fiji$Air.Temperature, 
                         date = dts,
                         cond = year,
                         arrange = "long",
                         main = "Air Temperature [°C]")

Now we can use these two objects and plot them on one page using grid

grid.newpage()

### define first plotting region (viewport)
vp1 <- viewport(x = 0, y = 1, 
                height = 0.5, width = 1,
                just = c("left", "top"),
                name = "top")

### enter vp1 
pushViewport(vp1)

### plot a plot - needs to be printed (and newpage set to FALSE)!!!
print(plot.water.temp, newpage = FALSE)

### leave vp1 - up one level (into root vieport)
upViewport(1)

### define second plot area
vp2 <- viewport(x = 0, y = 0, 
                height = 0.5, width = 1,
                just = c("left", "bottom"),
                name = "bottom")

### enter vp2
pushViewport(vp2)

### plot another plot
print(plot.air.temp, newpage = FALSE)

### destroy vp2 (as we're finished here)
popViewport()

plot of chunk unnamed-chunk-3

This is rather nice, as it enables direct comparisons between two variables. In this case we see that water temperature does exhibit the same seasonal behaviour as air temperature, whereas the diurnal signal is virtually non-existent… I hope this will spark some imagination for your own usage (e.g. comparisons of two climate station records or the like).

Note, the 3rd year we see here is a result of the conversion of UTC time to local Fiji time (we added 12 hours to UTC, hence end up with the first 12 hours of 1995)

sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=C                 LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] RWordPress_0.2-3    reshape_0.8.4       plyr_1.8           
## [4] latticeExtra_0.6-24 lattice_0.20-21     RColorBrewer_1.0-5 
## [7] metvurst_1.0        knitr_1.2          
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.3   evaluate_0.4.3 formatR_0.8    RCurl_1.95-4.1
## [5] stringr_0.6.2  tools_3.0.1    XML_3.98-1.1   XMLRPC_0.3-0
About these ads
This entry was posted in climatology, R, visualisation. Bookmark the permalink.

2 Responses to Update to metvurst

  1. Alexander says:

    Hi Tim,
    Thanks a lot for this update, I will try it :)

    my best regards,
    Alexander

  2. Alexander says:

    Hi Tim,
    I have update and tested with your example, and it works very good.

    I’m working with RStudio and at the first attempt it didn’t work, but after clean my workspace it started to work fine. I post this maybe someone has the same problem like me, then is necessary first of all to clean the workspace, here the code:

    rm(list=ls())

    regards,
    Alexander

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s