Data set description: Humidity data worldwide and in Australia
Source: NASA POWER: The POWER Project
Details on the retrieved data: Monthly and yearly relative humidity in Northern Territory and Western Australia (Australia).
Spatial and temporal resolution: Hourly, daily, monthly, or yearly data for regions defined by the lonlat parameter. Each cell corresponds to 1/2 x 1/2 degree.

Downloading humidity data with nasapower

The nasapower package aims at making it quick and easy to automate downloading NASA POWER (NASA Prediction of Worldwide Energy Resource) global meteorology, surface solar energy and climatology data.

Here, we will use the nasapower package to retrieve the relative humidity data for specific countries or for the world.

We have also used the nasapower package to retrieve rainfall data here. The rainfall tutorial includes an introduction on the nasapower package and how its functions work.

Installing nasapower package

We can install the package from CRAN and load it as follows.

install.packages("nasapower")
library(nasapower)

Using get_power() to fetch data

First let us have a look at how to get the daily data for humidity in agriculture. This can be done using the get_power() function.

Fetching daily data for single point

We use get_power() function arguments pars = "RH2M" which means relative humidity at 2 meters, temporal_api = "DAILY", and longlat equal to a single location.

data_RH <- get_power(
  community = "AG",
  lonlat = c(134.489563, -25.734968),
  dates = c("2010-09-23", "2010-12-23"),
  temporal_api = "DAILY",
  pars = "RH2M"
)

data_RH %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching daily data for an area

daily_humidity <- get_power(
  community = "AG",
  lonlat = c(150, -30, 155, -25),
  pars = "RH2M",
  dates = c("2004-09-19", "2004-09-29"),
  temporal_api = "DAILY"
)

daily_humidity %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching climatology data

For pars = "RH2M", and as in the rainfall tutorial we will focus on Australia territory and set community = "AG" and temporal_api = "CLIMATOLOGY".

flag <- 1
for (i in seq(110, 150, 5)) {
  for (j in seq(-40, -10, 5)) {
    climate_avg_RH_temp <- get_power(community = "AG",
                                  pars = "RH2M",
                                  lonlat = c(i, (j - 5), (i + 5), j),
                                  temporal_api = "CLIMATOLOGY")
    if (flag == 1) {
      climate_avg_RH <- climate_avg_RH_temp
      flag <- 0
    } else{
      climate_avg_RH <- rbind(climate_avg_RH, climate_avg_RH_temp)
    }
  }
}

climate_avg_RH %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Creating a map of annual humidity using all data retrieved

library(rnaturalearth)
library(raster)

# Getting world map
map <- ne_countries(country = 'australia', returnclass = "sf")

# Converting data to raster
r <- rasterFromXYZ(climate_avg_RH[, c("LON", "LAT", "ANN")])

# Converting the raster into a data.frame
r_df <- as.data.frame(r, xy = TRUE, na.rm = TRUE)

# Plot
ggplot() +
  geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
  geom_sf(data = map, inherit.aes = FALSE, fill = NA) +
  scale_fill_viridis() +
  labs(
    title = "Relative humidity",
    fill = "Humidity",
    subtitle = "Annual relative humidity in Australia"
  )

Creating a map of annual humidity using a subset of the data retrieved

library(rnaturalearth)

# Getting map for China
AUS <- ne_states(country = "Australia", returnclass = "sf")

# Getting administrative boundaries for regions
NT <- AUS[AUS$name == "Northern Territory", ]
WA <- AUS[AUS$name == "Western Australia", ]

# Converting data to raster
r <- rasterFromXYZ(climate_avg_RH[, c("LON", "LAT", "ANN")])

# Subset values for the region and converting the raster into a data.frame
rr <- mask(crop(r, NT), NT)
r_df <- as.data.frame(rr, xy = TRUE, na.rm = TRUE)

ggplot() +
  geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
  geom_sf(data = AUS, inherit.aes = FALSE, fill = NA) +
  scale_fill_viridis() +
  theme_minimal() +
  labs(title = "Relative Humidity in Northern Territory, Australia", fill = "Humidity")

# Subset values for the region and converting the raster into a data.frame
rr <- mask(crop(r, WA), WA)
r_df <- as.data.frame(rr, xy = TRUE, na.rm = TRUE)

ggplot() +
  geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
  geom_sf(data = AUS, inherit.aes = FALSE, fill = NA) +
  scale_fill_viridis() +
  theme_minimal() +
  labs(title = "Relative Humidity in Western Australia, Australia", fill = "Humidity")

Creating maps of monthly humidity

r <- list()
for (k in colnames(climate_avg_RH)[-c(1:3, 16)]) {
  r[[k]] <- rasterFromXYZ(climate_avg_RH[, c("LON", "LAT", k)])
}
r <- stack(r)
plot(r)

References


Last updated: 2021-11-30
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/humidity.Rmd

Tutorial was complied using: (click to expand)
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] raster_3.5-2              broom_0.7.9              
##  [3] rgdal_1.5-27              sp_1.4-5                 
##  [5] nasapower_4.0.0           rgeoboundaries_0.0.0.9000
##  [7] viridis_0.6.2             viridisLite_0.4.0        
##  [9] rnaturalearth_0.1.0       terra_1.4-11             
## [11] DT_0.19                   forcats_0.5.1            
## [13] stringr_1.4.0             dplyr_1.0.7              
## [15] purrr_0.3.4               readr_2.0.2              
## [17] tidyr_1.1.4               tibble_3.1.5             
## [19] ggplot2_3.3.5             tidyverse_1.3.1          
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.0                 sf_1.0-3                 bit64_4.0.5             
##  [4] lubridate_1.8.0          httr_1.4.2               tools_4.1.1             
##  [7] backports_1.3.0          bslib_0.3.1              utf8_1.2.2              
## [10] R6_2.5.1                 KernSmooth_2.23-20       rgeos_0.5-8             
## [13] DBI_1.1.1                colorspace_2.0-2         withr_2.4.2             
## [16] tidyselect_1.1.1         gridExtra_2.3            bit_4.0.4               
## [19] curl_4.3.2               compiler_4.1.1           cli_3.1.0               
## [22] rvest_1.0.2              xml2_1.3.2               labeling_0.4.2          
## [25] triebeard_0.3.0          sass_0.4.0               scales_1.1.1            
## [28] classInt_0.4-3           proxy_0.4-26             rappdirs_0.3.3          
## [31] digest_0.6.28            rmarkdown_2.11           pkgconfig_2.0.3         
## [34] htmltools_0.5.2          highr_0.9                dbplyr_2.1.1            
## [37] fastmap_1.1.0            htmlwidgets_1.5.4        rlang_0.4.11            
## [40] readxl_1.3.1             rstudioapi_0.13          httpcode_0.3.0          
## [43] farver_2.1.0             jquerylib_0.1.4          generics_0.1.1          
## [46] jsonlite_1.7.2           crosstalk_1.1.1          vroom_1.5.5             
## [49] magrittr_2.0.1           s2_1.0.7                 Rcpp_1.0.7              
## [52] munsell_0.5.0            fansi_0.5.0              lifecycle_1.0.1         
## [55] stringi_1.7.5            yaml_2.2.1               grid_4.1.1              
## [58] parallel_4.1.1           crayon_1.4.2             lattice_0.20-44         
## [61] haven_2.4.3              hms_1.1.1                knitr_1.36              
## [64] pillar_1.6.4             codetools_0.2-18         wk_0.5.0                
## [67] crul_1.1.0               reprex_2.0.1             glue_1.5.0              
## [70] evaluate_0.14            hoardr_0.5.2             modelr_0.1.8            
## [73] vctrs_0.3.8              tzdb_0.1.2               urltools_1.7.3          
## [76] cellranger_1.1.0         gtable_0.3.0             assertthat_0.2.1        
## [79] cachem_1.0.6             xfun_0.26                countrycode_1.3.0       
## [82] e1071_1.7-9              rnaturalearthhires_0.2.0 class_7.3-19            
## [85] memoise_2.0.0            units_0.7-2              ellipsis_0.3.2

Corrections: If you see mistakes or want to suggest changes, please create an issue on the source repository or submit a pull request
Contributions: If you want to contribute or collaborate on the project, please see the guidelines for collaborating
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.