Creating and arranging multiply plots

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating and arranging multiply plots

reichmaj
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating and arranging multiply plots

Bert Gunter-2
I think you need to read about "faceting" in ggplot. This may necessitate
modifying your data structure.

?layout may be an alternative approach.

See also
https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html
for a fuller exegesis.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Jul 1, 2020 at 9:44 AM Jeff Reichman <[hidden email]>
wrote:

> 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
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.