knitr::opts_chunk$set(echo = TRUE,
                      message = FALSE,
                      warning = FALSE,
                      results='hide')

working.dir <- ''

Note

This tutorial is intended for those users that have at access to at least 30 months of historical daily data through aWhere's API. If you are currently operating with a trial account that is limited to 6 months of access please refer to the tutorial "3-Create_Charts_trialAccount.RMD"

Introduction

This document describes how to generate aWhere weather data charts for a given location and timespan. Current/forecasted weather data is pulled from the aWhere API along with long-term normals for that same time span. Displaying line plots for variables of interest such as maximum temperature and P/PET allows us to observe weather trends and how current conditions compare to the past.

Weather variables for additional selected years of interest can also be added to these charts for further analysis and comparison between years. This tutorial adds 2016 and 2017 data onto charts for Accumulated Precipitation, P/PET, and effective P/PET.

Setup

Install / load R packages

Install required packages from aWhere and CRAN (the official repository of R code).

# install CRAN packages
list.of.packages.CRAN = c("tidyr", "dplyr", "ggplot2", "ggthemes", "curl", "zoo")

new.packages.CRAN = list.of.packages.CRAN[!(list.of.packages.CRAN %in% 
                                              installed.packages()[,"Package"])]
if(length(new.packages.CRAN)) install.packages(new.packages.CRAN)


# install aWhere code packages
list.of.packages.Github = c("aWhereAPI", "aWhereCharts")

new.packages.Github = list.of.packages.Github[!(list.of.packages.Github %in% 
                                                  installed.packages()[,"Package"])]
if(length(new.packages.Github) > 0) {
  for (x in 1:length(new.packages.Github)) {
    if (new.packages.Github[x] == 'aWhereAPI') {
      devtools::install_github("aWhereAPI/aWhere-R-Library")
    } else if (new.packages.Github[x] == 'aWhereCharts') {
      devtools::install_github("aWhereAPI/aWhere-R-Charts")
    }
  }
} 

\newpage

Load the R packages that contain functions used in this script.

# load packages 
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(curl)
library(zoo)
library(knitr)
library(aWhereAPI)
library(aWhereCharts)

Define paths, input files, and parameters

Define your working directory. This is the place on your computer where your input files are located for this tutorial, and it is also where the output files will be written. Instead of typing out the entire path to every file that we need (“absolute” paths), you can just use their filenames or locations within this working directory (“relative” paths) since we are setting this working directory as our starting point for the rest of the tutorial.

NOTE For this tutorial and ease of use for learning, all files will be written to the directory where this file is run from. An example of how one would set the working directory is below for reference

#working directory - where input files are located and outputs will be saved.
working.dir <- c('~/aWhere/') 

# set the working directory
setwd(working.dir)

With R, it is also necessary to create the directory structure you will write data files too beforehand. If one fails to do this, an error will be thrown when the data files are written

# create necessary output directories
dir.create(path = 'outputCSVs/',showWarnings = FALSE, recursive = TRUE)
dir.create(path = 'figures/',showWarnings = FALSE, recursive = TRUE)

Use the source function to load functions within .R files on your local machine. For this tutorial, the WriteJpeg and ClipValues functions are utilized within the "0-supporting_functions.R" file. There is a zero in the beginning of this filename so it appears at the top when files are sorted alphabetically.

# load external R functions in local file
source(paste0(working.dir, "0-supporting_functions.R"))

To pull data from the aWhere API, you’ll need a Consumer Key and Secret, like a username and password. Put these in a text file with the following format: line 1 is the Consumer Key, line 2 is the Consumer Secret, and line 3 is a blank line. Set the credentials.file variable equal to the name of this text file. Place your credentials file in your working directory.

# filename containing your aWhere credientials (key and secret),
credentials.file <- paste0(working.dir,'credentials.txt')

# load the aWhere API credentials file 
aWhereAPI::load_credentials(credentials.file) 

What location do you want to produce weather data charts for?

Specify the latitude and longitude, along with a name describing the place. This name will be used for the chart names and output file names.

# latitude, longitude, and name of location 
lat <- 8.45
lon <- 80.4
location.name <- paste("Ana") 

\newpage

What timespan to you want to acquire weather data for? Specify the years and dates here. Years is a vector with the starting year in position 1 and the ending year in position 2, for the long-term analysis.

The starting and ending days should have the format “YYYY-MM-DD” and should not exceed a period greater than 365 days. If you want the ending date to be n days from now (including some forecasted weather data), add or subtract n to the current date (which can be acquired using the Sys.Date() function). In the code below, when n = 7, the end date is seven days from now.

# starting and ending years and days for the time series. each is a vector with 
# the starting value in position 1 and the ending value in position 2. 
years <- c(2010, 2017)

#starting and ending days with format "YYYY-MM-DD"
day.start <- "2018-08-01"

#day.end <- "2018-03-08" # specific date
day.end <- as.character(Sys.Date() + 7) # today plus n
day.end <- as.character(Sys.Date() + 6) # today plus n

# combine the days into a single vector 
days <- c(day.start, day.end)

Specify additional selected year(s) to add onto the Accumulated Precipitation, P/PET, and eP/PET charts to compare to the current year(s) (in the day.start and day.end range) and LTN data.

# add additional selected year(s) to charts. 
# If you do not want to add additional years, set add.year = NA
#add.years <- NA
add.years <- c(2016, 2017)

Some of the charts will require additional parameters. For instance, effective precipitation is calculated using a threshold (eP in the code below). Rolling average calculations require a window size or number of days to average over (roll.avg in the code below).

# effective precip amount for consistency across plots
eP <- 30    

# size of rolling average window for consistency across plots
roll.avg <- 30

Processing Steps

Some of the charts require rolling average calculations. When the roll.avg variable is set to 30, this means that the first data point of the rolling average calculation does not start until 30 days after the specified start date! To avoid having 30 days of no data in our rolling average charts, let's adjust the start date to be 30 days earlier for the rolling average calculation.

# adjust the start date to be "roll.avg" days earlier
day.start.extended <- as.character(as.Date(day.start) - roll.avg)

# substitute this earlier date into the "days" vector for pulling data
days[1] <- day.start.extended

The duration for our time range cannot exceed 365 days. Let’s make sure our day range is an acceptable length.

# check if time range is more than 365 days
duration <- as.Date(days[2]) - as.Date(days[1])

if(duration > 365) { 
  print ("Period too long")
}

Pull the aWhere weather data for the specified location and timespan.

The R function that we use to pull the data is generateaWhereDataset, which comes from the aWhereCharts R package. It’s good practice to specify which package each function is from, since multiple packages may have different functions with the same name. The syntax for doing this is to use two colons between the package and function names, like this: package::function().

# pull the datasets for the specified location and time period   
weather.df <- aWhereCharts::generateaWhereDataset(lat = lat, lon = lon, 
                                                  day_start = days[1], 
                                                  day_end = days[2], 
                                                  year_start = years[1], 
                                                  year_end = years[2])

Let’s take a look at the weather data using the head function from the utils R package.

# select the first five columns of the data frame using [,1:5]
# and show the first 10 rows using the "n" argument
utils::head(weather.df[,1:5], n = 10)

Pull the aWhere weather data for the additional selected year(s).

If additional selected year(s) are specified, pull the aWhere weather data for those years. Since we will be adding these years onto charts, store the data-to-be-charted in a data frame called add.years.df. This data frame is created using the "long" format, which is preferred for the ggplot graphing package.

Wide vs. Long data formats

To illustrate the difference between "wide" and "long" data formats, check out the example below where the exact same information is represented in both wide and long form.

"Wide" datasets are often easier to visually interpret. As shown in Table 1, each row contains multiple features that describe a single individual.

# create example data with wide format
data.wide <- data.frame(individual = c(1, 2, 3),
                        feature_A = c('red', 'green', 'blue'),
                        feature_B = c(24, 27, 31),
                        feature_C = c(2004, 2008, 2011))

# reshape the dataset to have long format.
# keep "individual" in the first column, use the "feature"
# number as the key (new variable) and fill in the values
# for each feature for 
data.long <- tidyr::gather(data = data.wide, 
                           key = feature, 
                           value = value, 
                           feature_A:feature_C)
# create nicely formatted example of "wide" data
knitr::kable(data.wide, 
             align=c(rep('c', times=4)),
             caption = "Example dataset with wide format")

\newpage

"Long" datasets stack all of the features into a single column and list their values in another. This means that a single individual will have data in multiple rows when using the long format.

# nicely formatted example of "long" data
knitr::kable(data.long, 
             align=c(rep('c', times=3)),
             caption = "Same example dataset with long format")

The best way to shape your data really depends on the application. The ggplot package used later in this tutorial accepts datasets with long format. Let's look at another example, this time with variables and values that are present in aWhere data: precipitation and PET data for additional selected years 2016 and 2017, covering the dates of May 1st through May 3rd:

# create a wide dataset
data.wide <- data.frame(date = c("2018-05-01", "2018-05-02","2018-05-03"),
                        precip_2016 = c(4.56, 24.28, 11.25),
                        precip_2017 = c(5.39,  8.36,  2.03),
                        PET_2016 = c(5.04, 5.08, 5.02),
                        PET_2017 = c(7.58, 6.80, 6.17))

# create a long dataset 
data.long <- data.frame(date = c("2018-05-01", "2018-05-02","2018-05-03",
                                 "2018-05-01", "2018-05-02","2018-05-03", 
                                 "2018-05-01", "2018-05-02","2018-05-03",
                                 "2018-05-01", "2018-05-02","2018-05-03"),
                        data = c(4.56, 24.28, 11.25,
                                 5.39,  8.36,  2.03,
                                 5.04, 5.08, 5.02,
                                 7.58, 6.80, 6.17),
                        variable = c("precip", "precip", "precip",
                                "precip", "precip", "precip",
                                "PET", "PET", "PET",
                                "PET", "PET", "PET"),
                        year = c(2016, 2016, 2016,
                                 2017, 2017, 2017,
                                 2016, 2016, 2016,
                                 2017, 2017, 2017))
# example of wide data format
knitr::kable(data.wide,
             caption="Example weather dataset with wide format")

# example of long data format 
knitr::kable(data.long,
             caption="Same example weather dataset with wide format")

Notice that the long dataset separates the weather variable and year into two separate columns. This means that we can more easily filter these data for analysis and plotting. Let's create a data frame to store the additional selected year weather data in the long format so it's ready for plotting with ggplot later.

\newpage

# create a data frame to store additional selected year data.
# the "long" format of this data frame is ideal for plotting with ggplot.
add.years.df <- data.frame(date = character(),
                           data = double(),
                           var = character(),
                           year = double())

We specified 2016 and 2017 in the add.years variable above. For each of these years, the code below modifies the starting and ending date for these new timespans. aWhere weather data for each additional year is stored temporarily in the weather.df.mod data frame, and the variables of interest are parsed out for writing to a .csv file and later chart creation.

# pull the data sets for additional selected year(s)
for (year in add.years){

  # print a status message to the console 
  print(paste("Pulling weather data for selected year:", year, sep = " "))

  # if there are no additional selected years to add, break out of loop
  if(is.na(year)){
    break
  }

  # for each additional selected year, modify the start/end day values.
  # combine the selected year with the month-day values.
  day.start.mod <- paste(year,
                        paste(strsplit(x = days[1], split = "-")[[1]][2:3],
                                    collapse='-'),
                         sep="-")

  ending.year <- as.numeric(strsplit(x = days[2], split = "-")[[1]][1])
  starting.year <- as.numeric(strsplit(x = days[1], split = "-")[[1]][1])

  # if the date range spans multiple years, make sure the end date 
  # includes the selected year + 1.  
  if(ending.year == (starting.year + 1)){
    day.end.mod <- paste(year+1,
                         paste(strsplit(x = days[2], split = "-")[[1]][2:3],
                               collapse='-'),
                         sep="-")
  } else {
    day.end.mod <- paste(year,
                         paste(strsplit(x = days[2], split = "-")[[1]][2:3],
                               collapse='-'),
                         sep="-")
  }

  # pull data for selected year with modified start/end days
  weather.df.mod <- aWhereCharts::generateaWhereDataset(lat = lat, lon = lon, 
                                                      day_start = day.start.mod, 
                                                      day_end = day.end.mod, 
                                                      year_start = years[1], 
                                                      year_end = years[2])

  # save the accumulated precip data needed for charts in "long" format
  temp.data <- data.frame(date = weather.df$date, 
                          data = weather.df.mod$accumulatedPrecipitation.amount,
                          var = 'accumulatedPrecipitation.amount',
                          year = year) 

  # combine the accumulated precip data with other additional year data
  add.years.df <- rbind(add.years.df, temp.data)                       

  # calculate P/PET rolling average values for chart in "long" format
  temp.data <- data.frame(date = weather.df$date, 
                          data = weather.df.mod$ppet.amount,
                          var = 'ppet.amount.rollAvg',
                          year = year) 
  temp.data$data <- zoo::rollapply(temp.data$data
                                           ,width = roll.avg
                                           ,align = "right"
                                           ,FUN = mean
                                           ,na.rm = TRUE
                                           ,fill = NA)
  # combine the rolling average P/PET with other additional year data
  add.years.df <- rbind(add.years.df, temp.data) 

  # calculate eP/PET rolling average. first, use the ClipValues function to
  # calculate effective precipitation, then divide eP by PET for eP/PET. 
  weather.df.mod$ePPET <- ClipValues(weather.df.mod$precipitation.amount, 
                                     max.thresh = eP) /
                          weather.df.mod$pet.amount
  temp.data$date <- weather.df$date
  temp.data$data <- weather.df.mod$ePPET
  temp.data$data <- zoo::rollapply(temp.data$data
                                   ,width = roll.avg
                                   ,align = "right"
                                   ,FUN = mean
                                   ,na.rm = TRUE
                                   ,fill = NA)
  temp.data$var <- 'eppet.amount.rollAvg'
  temp.data$year <- year

  # combine the rolling average eP/PET with other additional year data
  add.years.df <- rbind(add.years.df, temp.data) 


  # add the current selected year's accumulated precip and PPET columns to the
  # main weather dataframe to write to .csv

  # Accumulated Precipitation amount
  weather.df$accumulatedPrecipitation.amount.year <- 
                              weather.df.mod$accumulatedPrecipitation.amount

  # P/PET 
  weather.df$ppet.amount.year <- weather.df.mod$ppet.amount
  weather.df$precipitation.amount.year <- weather.df.mod$precipitation.amount
  weather.df$pet.amount.year <- weather.df.mod$pet.amount

  # rename the columns to have the current year value appended on the end,
  # such as precipitation.amount.year.2016

  # Accumulated Precipitation 
  colnames(weather.df)[colnames(weather.df) == 
                         "accumulatedPrecipitation.amount.year"] <- 
                        paste0("accumulatedPrecipitation.amount.year", year)

  # P/PET
  colnames(weather.df)[colnames(weather.df) ==
                        "ppet.amount.year"] <- paste0("ppet.amount.year", year)
  colnames(weather.df)[colnames(weather.df) ==
      "precipitation.amount.year"] <- paste0("precipitation.amount.year", year)
  colnames(weather.df)[colnames(weather.df) ==
                        "pet.amount.year"] <- paste0("pet.amount.year", year)
}

The variables of interest include accumulated precipitation, P/PET, PET and precipitation (to calculate effective precipitation). They are combined with the rest of the weather data and written to a .csv file for later analysis.

Let's take a look at the last few columns of the weather data frame. We will see the additional selected year data that was obtained in the loop above, and what the column names look like.

# reorder the columns in the data frame
weather.df <- weather.df %>% 
              dplyr::select(day, date, latitude, longitude, everything())

# look at the last few columns of the weather data frame. 
utils::head(weather.df[,(ncol(weather.df)-2):(ncol(weather.df))], n = 6)

Write weather data to .csv file

Write the weather data to a .csv file for further analysis. Use the latitude, longitude, date range, and year range in the filename so future-you will know exactly what’s inside this file.

# write forecast to .csv file 
utils::write.csv(weather.df, 
                 file = paste0('outputCSVs/',
                              paste(location.name, 
                              paste(days, collapse="_"),
                              paste(years, collapse="_"),
                              ".csv", sep="_")))

# since the rolling averages have already been calculated and the 
# weather data extending out to an earlier start date (30 days in this case)
# has been saved to file, let's filter our weather dataframe down to span
# the original date range for chart creation. 
weather.df.extended <- weather.df

weather.df <- weather.df %>% 
  dplyr::filter(as.Date(date) >= day.start)

# copy the data frame with extended dates for rolling averages
add.years.df.extended <- add.years.df

# filter the additional selected year data according to date
add.years.df <- add.years.df %>% 
  dplyr::filter(as.Date(date) >= day.start)

Create charts

Within the aWhereCharts package, there are two functions used here to create time series charts.

(1) generateaWhereChart - makes line plots comparing the current and long-term normals for each variable.

(2) generateaWhereStdDevChart - plots aWhere weather data with one standard deviation's shading above and below the long-term normalline. This function is meant to help users understand whether the current weather conditions are significantly outside the norm.

Plots with standard deviation are named with "1" at the end of the variable, and those without standard deviation are named with "2".

Create chart: maximum temperature with standard deviation

Contruct the title for this chart using the variable name, location name, and the latitude/longitude.

# create a variable containing the lat and lon, for plot titles
lat.lon <- paste0("(", lat, ", ", lon, ")") 

# construct title
max.temp.1.title <- paste0(location.name, "_Maximum Temp w StdDev")

Generate the plot using the generateaWhereStdDevChart function from the aWhereCharts package.

# generate the plot 
max.temp.1 <- aWhereCharts::generateaWhereStdDevChart(data = weather.df, 
                                              variable = "maxTemp", 
                                              title = paste0(max.temp.1.title,
                                                              lat.lon))

To display the plot, use the plot object name.

# display the plot
max.temp.1

In the following sections, these same steps are performed for a series of other variables in the aWhere weather data set.

Minimum temperature with standard deviation

min.temp.1.title <- paste0(location.name, "_Minimum Temp w StdDev")

min.temp.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                              "minTemp", 
                                              title = paste0(min.temp.1.title,
                                                              lat.lon))

Potential evapotranspiration (PET) with standard deviation

pet.1.title <- paste0(location.name, "_PET w StdDev")

pet.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                                 "pet", 
                                                 title = paste0(pet.1.title,
                                                                lat.lon))

Daily precipitation with standard deviation

precip.1.title <- paste0(location.name, "_Daily Precipitation w StdDev")

precip.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                              "precipitation",  
                                              title = paste0(precip.1.title,
                                                              lat.lon))

Daily precipitation without standard deviation

precip.2.title <- paste0(location.name, "_Daily Precipitation")

precip.2 <- aWhereCharts::generateaWhereChart(weather.df,
                                              "precipitation",    
                                              title = paste0(precip.2.title,
                                                             lat.lon))

Accumulated Precipitation with StdDev but no Effective Precipitation

no.eprecip.1.title <- paste0(location.name, 
                             "_Accumulated Precipitation w StdDev")

no.eprecip.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                            "accumulatedPrecipitation",
                                            title = paste0(no.eprecip.1.title,
                                                            lat.lon))

Precipitation and Effective Precipitation, Accumulated

eprecip.1.title <- paste0(location.name, "_Precipitation and",
                          " Effective Precipitation, Accumulated w Std Dev")

eprecip.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                              "accumulatedPrecipitation",
                                              title = paste0(eprecip.1.title,
                                                            lat.lon),
                                              e_precip = TRUE, 
                                              e_threshold = eP)

Accumulated Precipitation

acc.precip.2.title <- paste0(location.name, "_Accumulated Precipitation")

acc.precip.2 <- aWhereCharts::generateaWhereChart(weather.df, 
                                            "accumulatedPrecipitation", 
                                            title = paste0(acc.precip.2.title,
                                                            lat.lon))

Add additional selected year line(s) to the accumulated precipitation chart.

# filter the add.years data frame for the accumulated precip data.
add.years.accPrecip <- add.years.df %>% 
  dplyr::filter(var == "accumulatedPrecipitation.amount")

# generate color scale to use including the colors of the original chart:
# list of original colors for current and LTN lines 
colors.orig <- c("#1F83B4", "#FF810E")

# list of unique colors for additional lines added 
colors.additional <- c("black", "red", "yellow", "purple")

# just take the number of line colors that are needed for the final chart
colors.final <- c(colors.additional[1:length(add.years)], colors.orig)

acc.precip.2.addyears.title <- paste0(location.name, 
                                      "_Accumulated Precipitation ",
                                      "with additional selected years")

# add the additional selected year lines to the acc precip chart
acc.precip.2.addyears <- acc.precip.2 + 
  geom_line(aes(x = as.Date(add.years.accPrecip$date),
                y = add.years.accPrecip[,'data'],
                color = as.factor(add.years.accPrecip[,'year'])),
            size = 1.5) +
  ggtitle(acc.precip.2.addyears.title) + 
  scale_color_manual(values = colors.final)

\newpage

Display the chart.

acc.precip.2.addyears

Accumulated PET

acc.pet.1.title <- paste0(location.name, "_Accumulated PET w StdDev")

acc.pet.1 <- aWhereCharts::generateaWhereStdDevChart(weather.df, 
                                                 "accumulatedPet",
                                                 title = paste0(acc.pet.1.title,
                                                               lat.lon))

P/PET

# ppet rarely is interpretable on a daily chart 
ppet.2.title <- paste0(location.name,"_PPET ")

ppet.2 <- aWhereCharts::generateaWhereChart(weather.df, 
                                             "ppet", 
                                             title = paste0(ppet.2.title,
                                                            lat.lon))

30-day rolling average eP/PET and P/PET

# no eprecip/PET shows up if all rainfall events are less than the e_threshold
rolling.avg.ppet.2.title <- paste0(location.name,
                                   "_30 day rolling avg eP PET and P PET")

# use the "extended" weather data, since it contains dates 30 days prior
# to the original start date and will allow for rolling average values to
# be calculated from the original start date. 
rolling.avg.ppet.2 <- aWhereCharts::generateaWhereChart(weather.df.extended, 
                                      "rollingavgppet",
                                      title = paste0(rolling.avg.ppet.2.title,
                                                     lat.lon),
                                      e_precip = TRUE, 
                                      e_threshold = eP, 
                                      rolling_window = roll.avg) + 
                        xlim(as.Date(day.start), as.Date(day.end))

30-day rolling average P/PET chart with additional selected years

rolling.avg.ppet.2.addyears.title <- paste0(location.name,
                                 "_30 day rolling avg P PET ",
                                 "with additional selected years")

# get a chart of P/PET without effective precip (set e_precip to FALSE).
# as for the previous chart, use the weather date with "extended" dates
# for this rolling average calculation. 
rolling.avg.ppet <- aWhereCharts::generateaWhereChart(weather.df.extended, 
                                                        "rollingavgppet",
                                  title = paste0(rolling.avg.ppet.2.addyears.title,
                                                 lat.lon),
                                  e_precip = FALSE, 
                                  e_threshold = eP, 
                                  rolling_window = roll.avg)
# filter the add.years data frame for just the rolling average P/PET data
add.years.rollAvgPPet <- add.years.df %>% dplyr::filter(var == "ppet.amount.rollAvg")

# add P/PET lines to the chart for additional selected years.
# set the x-axis limits to reflect the specified start and end dates.
rolling.avg.ppet.2.addyears <- rolling.avg.ppet + 
  geom_line(aes(x = as.Date(add.years.rollAvgPPet$date),
                y = add.years.rollAvgPPet[,'data'],
                color = as.factor(add.years.rollAvgPPet[,'year'])),
            size = 1.5) +
  ggtitle(rolling.avg.ppet.2.addyears.title) + 
  scale_color_manual(values = colors.final) +
  xlim(as.Date(day.start), as.Date(day.end))

# display the chart. 
rolling.avg.ppet.2.addyears

30-day rolling average eP/PET chart with additional selected years

rolling.avg.eppet.2.addyears.title <- paste0(location.name,
                                          "_30 day rolling avg eP PET ",
                                          "with additional selected years")

# get a chart of eP/PET. Since there is no way to isolate the eP/PET curve 
# within the generateaWhereChart function, let's cap the precipitation using 
# the eP threshold and then supply this to the function to chart eP/PET for 
# the current year and LTN. 
weather.df$precipitation.amount <- ClipValues(weather.df$precipitation.amount,
                                              max.thresh = eP)

rolling.avg.eppet <- aWhereCharts::generateaWhereChart(weather.df.extended, 
                          "rollingavgppet",
                          title = paste0(rolling.avg.eppet.2.addyears.title,
                                         lat.lon),
                          e_precip = FALSE, 
                          e_threshold = eP, 
                          rolling_window = roll.avg) + 
                     xlim(as.Date(day.start), as.Date(day.end))

# filter the add.years data frame for just the rolling average P/PET data
add.years.rollAvgePPet <- add.years.df %>% 
  dplyr::filter(var == "eppet.amount.rollAvg")

# add P/PET lines to the chart for additional selected years 
rolling.avg.eppet.2.addyears <- rolling.avg.eppet + 
  geom_line(aes(x = as.Date(add.years.rollAvgePPet$date),
                y = add.years.rollAvgePPet[,'data'],
                color = as.factor(add.years.rollAvgePPet[,'year'])),
            size = 1.5) +
  ggtitle(rolling.avg.eppet.2.addyears.title) + 
  scale_color_manual(values = colors.final) + 
  xlim(as.Date(day.start), as.Date(day.end))

# display the chart. 
rolling.avg.eppet.2.addyears

\newpage

Mulitplot

Select any of the above charts to display together in a multiplot using their variable names:

# set the graphics device parameters to write a .JPEG
jpeg(paste0('figures/',location.name,"_4chart.jpeg"), 
     width = 12, height = 6, 
     units = 'in', res = 500)

# generate the multiplot & write to JPEG
aWhereCharts::generateMultiplot(acc.precip.2.addyears, 
                                rolling.avg.ppet.2.addyears, 
                                max.temp.1, 
                                pet.1, 
                                cols = 2, fontsize = 10, 
                                title = paste0("Current vs LTN at ", 
                                               location.name," (", 
                                               lat, ", ", lon, ")", 
                                               "   eP = ",eP,"mm"))
# close the current plot object
invisible(dev.off())

Display the multiplot. In each of the four charts, the vertical dashed line marks the current date, beyond which the weather data is forecasted.

knitr::include_graphics(paste0('figures/',location.name,"_4chart.jpeg"))

\newpage

Write charts to file

# Maximum temperature
#max.temp.1 

# write the plot to file using the WriteJpeg function, an external R function
# in the "supporting_functions.R" file.
WriteJpeg(plt = max.temp.1, plt.title = paste0('figures/',max.temp.1.title))


# Minimum temperature with standard deviation
#min.temp.1 
WriteJpeg(plt = min.temp.1, plt.title = paste0('figures/',min.temp.1.title))


# Potential evapotranspiration (PET) with standard deviation 
#pet.1 
WriteJpeg(plt = pet.1, plt.title = paste0('figures/',pet.1.title))


# Daily precipitation with standard deviation  
#precip.1 
WriteJpeg(plt = precip.1 , plt.title = paste0('figures/',precip.1.title))


# Daily precipitation without standard deviation  
#precip.2 
WriteJpeg(plt = precip.2, plt.title = paste0('figures/',precip.2.title))


# Accumulated Precipitation with StdDev but no Effective Precipitation
#no.eprecip.1
WriteJpeg(plt = no.eprecip.1, plt.title = paste0('figures/',no.eprecip.1.title))


# Precipitation and Effective Precipitation, Accumulated 
#eprecip.1
WriteJpeg(plt = eprecip.1, plt.title = paste0('figures/',eprecip.1.title))


# Accumulated Precipitation 
#acc.precip.2
WriteJpeg(plt = acc.precip.2, plt.title = paste0('figures/',acc.precip.2.title))


# Accumulated Precipitation with additional selected years
#acc.precip.2.addyears
WriteJpeg(plt = acc.precip.2.addyears, 
          plt.title = paste0('figures/',acc.precip.2.addyears.title))


# Accumulated PET 
#acc.pet.1
WriteJpeg(plt = acc.pet.1, plt.title = paste0('figures/',acc.pet.1.title))


# P/PET 
#ppet.2
WriteJpeg(plt = ppet.2, plt.title = paste0('figures/',ppet.2.title))


# 30-day rolling average eP/PET and P/PET 
#rolling.avg.ppet.2
WriteJpeg(plt = rolling.avg.ppet.2, 
          plt.title = paste0('figures/',rolling.avg.ppet.2.title))


# 30-day rolling average P/PET with additional selected years
#rolling.avg.ppet.2.addyears
WriteJpeg(plt = rolling.avg.ppet.2.addyears, 
          plt.title = paste0('figures/',rolling.avg.ppet.2.addyears.title))


# 30-day rolling average eP/PET with additional selected years
#rolling.avg.eppet.2.addyears
WriteJpeg(plt = rolling.avg.eppet.2.addyears, 
          plt.title = paste0('figures/',rolling.avg.eppet.2.addyears.title))


aWhereAPI/aWhere-R-Charts documentation built on Dec. 30, 2021, 12:58 p.m.