0

From an experiment I do, I get large files with time series data. Each column represents one series that I would like to plot in a graph. The ranges of the X and Y axis are not important as I only need it as an overview.

The problem is, I have from 150-300 columns (and 400 rows) per data frame and and I am not able to figure out how to plot more than 10 graphs at once.

library(ggplot2)
library(reshape2)
csv <- read.csv(file = "CSV-File-path", header = F, sep = ";", dec = ".")[,1:10]
df <- as.data.frame(csv)
plot.ts(df)

The moment I change [,1:10] to [,1:11] I get an error:

Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, : cannot plot more than 10 series as "multiple"

Ideally I would like an output of a multiple paged PDF file with at least 10 graphs per page. I am fairly new to R, I hope you are able to help me.

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 1
    What is the error you get? –  Nov 05 '15 at 08:42
  • Added the error message that I get – Muad Abd El Hay Nov 05 '15 at 09:39
  • Apparently `plot.ts` cannot plot more than 10 series. What you can do is use `ggplot2` or the `base` `plot` and `lines` function. Do you mind to add a `name(csv)` to your example? – David Nov 05 '15 at 09:47
  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269), also see `PDF()` and `apply()` loop functions. – zx8754 Nov 05 '15 at 10:03

2 Answers2

3

And here is a ggplot2 way to do it:

library(ggplot2)
library(reshape2)

nrow <- 200
ncol <- 24
df <- data.frame(matrix(rnorm(nrow*ncol),nrow,ncol))

# use all columns and shorten the measure name to mvar
mdf <- melt(df,id.vars=c(),variable.name="mvar") 

gf <- ggplot(mdf,aes(value,fill=mvar)) + 
         geom_histogram(binwidth=0.1) + 
         facet_grid(mvar~.)

print(gf) # print it out so we can see it

ggsave("gplot.pdf",plot=gf)   # now save it as a PDF

This is what the plot looks like: enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
2

Here is one way to do it. This one groups the columns in groups of 5 and then writes them as separate pages in a single PDF. But if it were me I would be using ggplot2 and doing them in a single plot.

nrow <- 18
ncol <- 20
df <- data.frame(matrix(runif(nrow*ncol),nrow,ncol))

plots <- list()

ngroup <- 5
icol <- 1
while(icol<=ncol(df)){
   print(icol)
   print(length(plots))
   ecol <- min(icol+ngroup-1,ncol(df))
   plot.ts(df[,icol:ecol])
   plots[[length(plots)+1]] <- recordPlot()
   icol <- ecol+1
}
graphics.off()

pdf('plots.pdf', onefile=TRUE)
for (p in plots) {
  replayPlot(p)
}
graphics.off()
Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 1
    Credit where credit is due - I used http://stackoverflow.com/questions/13273611/how-to-append-a-plot-to-an-existing-pdf-file – Mike Wise Nov 05 '15 at 10:25