0

Why is the result an 8-page PDF instead of 1-page PDF with 8 charts?

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

### get symbols
for (i in s) { x[[i]] <- getSymbols(i, src="yahoo", auto.assign=FALSE, return.class="xts") }

### create pdf
pdf('foo.pdf')
par(mfrow = c( 4, 2 ) )
for (i in x) { chartSeries(i) }
dev.off()

enter image description here

AG1
  • 6,648
  • 8
  • 40
  • 57
  • See https://stackoverflow.com/questions/13538151/addsma-not-drawn-on-graph-when-called-from-function and the 3 links in [@GSee's comment](https://stackoverflow.com/questions/13538151/addsma-not-drawn-on-graph-when-called-from-function#comment18563311_13538394). The problem is that autoprinting does not occur inside functions or loops (and in other situations). – Joshua Ulrich Oct 28 '18 at 11:22

1 Answers1

2

You can use lapply and chart_Series to get multiple charts on 1 page:

pdf(file = "charts.pdf")
par(mfrow=c(4,2))
lapply(x,function(x) chart_Series(x))
dev.off()

Edit to get ticker symbols:

To put the correct ticker names on the charts you can add the name argument to chart_Series like so:

name = unlist(strsplit(names(x[[1]])[1],'[.]'))[1]

enter image description here

hvollmeier
  • 2,956
  • 1
  • 12
  • 17
  • I tried to get this working ... would you mind writing out the full listing? – AG1 Dec 26 '18 at 08:41
  • Take the code from the question and substitute the code part starting after the comment line ( „### create pdf“) with the code from the solution. – hvollmeier Dec 26 '18 at 08:50