简体   繁体   中英

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')")
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

在此处输入图片说明

a bit closer

在此处输入图片说明

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM