From a321e99f3785d4d7378c829c256d86780b01ab68 Mon Sep 17 00:00:00 2001 From: Erica Christensen Date: Thu, 5 Oct 2017 12:29:42 -0400 Subject: [PATCH] added 'ylabel' argument to forecast_viz function, to display species names on plot instead of sp codes --- forecast_tools.R | 19 ++++++++++++++----- index.Rmd | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/forecast_tools.R b/forecast_tools.R index 35a78a8a61..5227bc9c70 100644 --- a/forecast_tools.R +++ b/forecast_tools.R @@ -324,17 +324,26 @@ all_forecasts$date=as.Date(all_forecasts$date) #' Visualize a time-series forecast #' Plots the observed time-series and the 1-step forecasts within it #' Plots the forecast time-series along with the prediction interval for future observations -#' obs_data is a data.frame -#' date_col_name is a string with the name for the date column -#' val_col_name is a string with the name for the column of the value being forecast +#' @param obs_data is a data.frame (observed data) +#' @param obs_date_col_name is a string: name of the date column from obs_data +#' @param obs_val_col_name is a string: name of the column of the value being forecast +#' @param for_data is a data.frame (forecast data) +#' @param for_date_col_name is a string: name of the date column from for_data +#' @param for_val_col_name is a string: name of the column of value being forecast, from for_data +#' @param for_model_name is a string: name of the model to be used from model column in for_data +#' @param for_lowerpi_col_name is a string: name of the column of the lower confidence interval from for_data +#' @param for_upperpi_col_name is a string: name of the column of the upper confidence interval from for_data +#' @param start_newmoon is numeric: first new moon number to be plotted +#' @param ylabel is a string: title for y-axis forecast_viz <- function(obs_data, obs_date_col_name, obs_val_col_name, for_data, for_date_col_name, for_val_col_name, for_model_name, - for_lowerpi_col_name, for_upperpi_col_name, start_newmoon){ + for_lowerpi_col_name, for_upperpi_col_name, start_newmoon, + ylabel){ for_data_sub = filter(for_data, species == obs_val_col_name, model == for_model_name) obs_data_sub = filter(obs_data, newmoonnumber >= start_newmoon) ggplot(obs_data_sub, aes_string(x = obs_date_col_name)) + geom_ribbon(data = for_data_sub, mapping = aes_string(x = for_date_col_name, ymin = for_lowerpi_col_name, ymax = for_upperpi_col_name), fill = "lightblue") + geom_line(aes_string(y = obs_val_col_name)) + geom_line(data = for_data_sub, mapping = aes_string(x = for_date_col_name, y = for_val_col_name), color = "blue") + - theme(axis.title.x=element_blank()) + labs(x='',y=ylabel) } diff --git a/index.Rmd b/index.Rmd index 4d722261ea..e4eea959ec 100644 --- a/index.Rmd +++ b/index.Rmd @@ -40,7 +40,8 @@ forecast_viz(obs_data = obs_data_newmoon, for_model_name = "ensemble", for_lowerpi_col_name = "LowerPI", for_upperpi_col_name = "UpperPI", - start_newmoon = 300) + start_newmoon = 300, + ylabel = 'Total Abundance') ``` ## Species-Level Forecasts @@ -61,17 +62,24 @@ most_abund_sp = sp_predictions %>% head(3) %>% select(species) -for (species in as.vector(most_abund_sp$species)) { +# load in rodent species table to get scientific names to display on plots +species_table = read.csv( + text = RCurl::getURL( + "https://raw.githubusercontent.com/weecology/PortalData/master/Rodents/Portal_rodent_species.csv"),stringsAsFactors = F,na.strings = '') +most_abund_sp_names = filter(species_table,speciescode %in% most_abund_sp$species) %>% select(speciescode,scientificname) + +for (n in seq(dim(most_abund_sp_names)[1])) { species_forecast = forecast_viz(obs_data = obs_data_newmoon, obs_date_col_name = "censusdate", - obs_val_col_name = species, + obs_val_col_name = most_abund_sp_names$speciescode[n], for_data = for_data, for_date_col_name = "forecastdate", for_val_col_name = "estimate", for_model_name = "ensemble", for_lowerpi_col_name = "LowerPI", for_upperpi_col_name = "UpperPI", - start_newmoon = 300) + start_newmoon = 300, + ylabel = most_abund_sp_names$scientificname[n]) plot(species_forecast) } ```