palr

Colours can be frustrating to get just right in R. The palr package provides simple palette functions with standard colour schemes matched to real data values.

There are three main ways of working with palr palette functions.

Examples

Here we show examples of the use of palr functions.

The oisst data set is a subset the NOAA 1/4° daily Optimum Interpolation Sea Surface Temperature (Reynolds, 2007) for the 20 May 2015 obtained from the National Oceanic and Atmospheric Administration (NOAA).

library(raster)
library(palr)
load(system.file("extdata", "oisst.rda", package = "palr"))
plot(oisst)

The default plot colours uses a setting provided by the raster package, but we have SST data in degrees Celsius so we can use the sstPal function to give specific colours for particular temperatures. The full range of the temperatures is shown on the plot legend, even though our data only has values in the range -1.8, 23.5.

sstcols <- sstPal(palette = TRUE)
plot(oisst, col = sstcols$col, zlim = range(sstcols$breaks))

Because we have the palette colours and data in an absolute palette we can also plot other data correctly to scale.

Create a aggregated version of the SST data set and turn it into polygons.

smsst <- focal(aggregate(oisst, fact = 5, fun = mean), matrix(1, 3), fun = median, na.rm = FALSE)
psst <- rasterToPolygons(smsst, na.rm = FALSE)
layout(matrix(1:2, ncol = 2))
plot(oisst, col = sstcols$col, zlim = range(sstcols$breaks))
plot(oisst, col = "transparent", legend = FALSE)## plot twice to get exact alignment
plot(psst, col = sstPal(psst$layer), add = TRUE)
temps <- rev(c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))
op <- par(xpd = NA); legend("right", legend = temps, fill = sstPal(temps), inset = -0.2, cex = 0.65)

par(op)

References

Reynolds, R. W., T. M. Smith, C. Liu, D. B. Chelton, K. S. Casey, and M. G. Schlax, 2007: Daily high-resolution-blended analyses for sea surface temperature. Journal of Climate, 20, 5473–5496.

Appendix: notes on data sets

Full ncdump metadata from the data sets can be obtained with the following.

metadata(oisst)$filename
## [1] "eclipse.ncdc.noaa.gov/pub/OI-daily-v2/NetCDF/2015/AVHRR/avhrr-only-v2.20150520.nc"
writeLines(metadata(oisst)$ncdump)
## 
##      4 variables (excluding dimension variables):
##         short sst[lon,lat,zlev,time]   
##             long_name: Daily sea surface temperature
##             units: degrees C
##             _FillValue: -999
##             add_offset: 0
##             scale_factor: 0.00999999977648258
##             valid_min: -300
##             valid_max: 4500
##         short anom[lon,lat,zlev,time]   
##             long_name: Daily sea surface temperature anomalies
##             units: degrees C
##             _FillValue: -999
##             add_offset: 0
##             scale_factor: 0.00999999977648258
##             valid_min: -1200
##             valid_max: 1200
##         short err[lon,lat,zlev,time]   
##             long_name: Estimated error standard deviation of analysed_sst
##             units: degrees C
##             _FillValue: -999
##             add_offset: 0
##             scale_factor: 0.00999999977648258
##             valid_min: 0
##             valid_max: 1000
##         short ice[lon,lat,zlev,time]   
##             long_name: Sea ice concentration
##             units: percentage
##             _FillValue: -999
##             add_offset: 0
##             scale_factor: 0.00999999977648258
##             valid_min: 0
##             valid_max: 100
## 
##      4 dimensions:
##         time  Size:1
##             long_name: Center time of the day
##             units: days since 1978-01-01 00:00:00
##         zlev  Size:1
##             long_name: Sea surface height
##             units: meters
##             actual_range: 0, 0
##         lat  Size:720
##             long_name: Latitude
##             units: degrees_north
##             grids: Uniform grid from -89.875 to 89.875 by 0.25
##         lon  Size:1440
##             long_name: Longitude
##             units: degrees_east
##             grids: Uniform grid from 0.125 to 359.875 by 0.25
## 
##     7 global attributes:
##         Conventions: CF-1.0
##         title: Daily-OI-V2, Final, Data (Ship, Buoy, AVHRR: NOAA19, METOP, NCEP-ice)
##         History: Version 2.0
##         creation_date: 2015-06-04 11:10
##         Description: Reynolds, et al.(2007) Daily High-resolution Blended Analyses. Available at ftp://eclipse.ncdc.noaa.gov/pub/OI-daily/daily-sst.pdf  Climatology is based on 1971-2000 OI.v2 SST, Satellite data: Navy  NOAA19 METOP AVHRR, Ice data: NCEP ice
##         Source: NOAA/National Climatic Data Center
##         Contact: Dick Reynolds, email: Richard.W.Reynolds@noaa.gov & Chunying Liu, email: Chunying.liu@noaa.gov