Skip to content

Commit

Permalink
added 'ylabel' argument to forecast_viz function, to display species …
Browse files Browse the repository at this point in the history
…names on plot instead of sp codes
  • Loading branch information
emchristensen committed Oct 5, 2017
1 parent 35b8605 commit a321e99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 14 additions & 5 deletions forecast_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 12 additions & 4 deletions index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
```
Expand Down

0 comments on commit a321e99

Please sign in to comment.