AG1
AG1

Reputation: 6774

R: quantmod, chartseries and PDF

What is the best way to graph 8 charts at once using quantmod?

Here are some options:
1. collate all 8 charts in memory, then combine into a 8.5" x 11" pdf
2. save each chart as a png, then combine into a pdf

I would prefer option #1 to #2.

library(tidyverse)
library(quantmod)
s = c("AAL","DAL","UAL","LUV","FDX","ALK","JBLU","HA")

# example of charts to graph
getSymbols("AAL", src="yahoo")
chartSeries(AAL, type="line",subset='last 60 months',
  TA="addSMA(200,col='orange');addSMA(65,col='red')")
getSymbols("DAL", src="yahoo")
chartSeries(DAL, type="line",subset='last 60 months',
  TA="addSMA(200,col='orange');addSMA(65,col='red')")

Upvotes: 1

Views: 1037

Answers (1)

Wimpel
Wimpel

Reputation: 27772

library(quantmod)
s = c("AAL","DAL","UAL","LUV","FDX","ALK","JBLU","HA")

symbols <- list (getSymbols(s, source = "yahoo"))

pdf(file = "charts.pdf")
par(mfrow = c( 8, 1 ) )
chartSeries(AAL, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(DAL, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(UAL, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(LUV, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(FDX, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(ALK, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(JBLU, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
chartSeries(HA, type="line",subset='last 60 months', TA="addSMA(200,col='orange');addSMA(65,col='red')")
dev.off()

screenshots of the pdf

Completely zoomed out

enter image description here

a bit closer

enter image description here

UPDATE

You can use chart_Series(), shich respects the settings in par(). But I am not getting the arguments right, so the charts do not look as nice as with chartSeries.

pdf(file = "charts.pdf")
par(mfrow = c( 4, 2 ) )
chart_Series(AAL)
chart_Series(DAL)
chart_Series(UAL)
chart_Series(LUV)
chart_Series(FDX)
chart_Series(ALK)
chart_Series(JBLU)
chart_Series(HA)
dev.off()

enter image description here

Upvotes: 4

Related Questions