[R] Creating and arranging multiply plots
Jeff Reichman
re|chm@nj @end|ng |rom @bcg|ob@|@net
Wed Jul 1 18:44:27 CEST 2020
r-help forum
Need some guidance. I need to create 20 plots and then combine them into one
graphic. Normally this isn't a problem for me except this time I'm using the
holts function to perform a TS forecast. So I though I'd just write a
function which take the country name and then passes the name through the
function, then I'd take the name output and recombine the individual plots
into one. Well its not working like I though it would work. Any suggestions
of how I might tackle this quandary? The problem is I chose to make
individual plotly plots using the plot_forecast {TSstudio} function Here is
a sample dataset and code.
library(dplyr)
library(forecast)
library(ggplot2)
library(TSstudio)
library(xts)
library(zoo)
# data prep
sample_data <- read_excel("~/NGA Telework/USSOUTHCOM/Data/sample.xlsx")
sample_data$date <- as.Date(sample_data$date, format = c("%Y-%m-%d"))
sample_xts <- as.xts(samplef[ , -2], order.by = sample$date)
sample_zoo <- fortify(sample_xts)
sample_zoo$total_cases <- as.numeric(as.factor(sample_zoo$total_cases))
# plots function
plots <- function(i){
cases <- filter(sample_data, location == i) %>% select(date, total_cases)
xts <- as.xts(cases[ , -1], order.by = cases$date)
xts <- na.locf(xts)
forecast <- holt(xts, damped = TRUE, h = 2, PI = 0.9)
i <- plot_forecast(forecast,
title = i,
Xtitle = "Date",
Ytitle = "Cases")
return(i)
}
# create individual plots
atg <- plots("Antigua and Barbuda") # Antigua and Barbuda
arg <- plots("Argentina") # Argentina
brb <- plots("Barbados") # Barbados
blz <- plots("Belize") # Belize
Sincerely
Jeff Reichman
(314) 457-1966
More information about the R-help
mailing list