0

I have 50 files containing 3 time series in each of them with identical tab separated format. To be specific each file contains 250 observations and looks like below:

1 8.83229 0.02544 0.02544
2 2.95561 0.02544 0.02544
3 1.27395 0.02544 0.02544
4 2.01115 0.02544 0.02544
5 2.38058 0.02383 0.02383
6 1.10755 0.02383 0.02383
7 1.16735 0.02013 0.02013
8 1.57755 0.02013 0.02013
9 1.81942 0.02013 0.02013
10 1.45921 0.01611 0.01611
...
246 0.04564 0.02383 0.01611
247 0.04357 0.02383 0.01611
248 0.03651 0.02383 0.01611
249 0.03334 0.02383 0.01611
250 0.03438 0.02383 0.01611

The first column is obviously index and the other three columns are time series. I've written a gnuplot script to be called from another shell script in order to plot all these 50 files. But i'd like to organise these plots in such a manner that, 3x4 or 4x5 of them, to be in one A4, in a publication written with LaTeX. Is there a LaTeX package or gnuplot trick for this? Maybe it is easier to do this with R? Any suggestion is welcome.

4

2 回答 2

5

Something like this,

library(reshape2)
library(plyr)
library(ggplot2)

setwd("/Users/baptiste/data/")

lf <- list.files(pattern=".txt")

read_file <- function(f, ...){

  result <- read.table(f, ...)
  names(result) <- c("id", "ts1", "ts2", "ts3")
  result

}

## read all files in a list
all <- llply(lf, read_file, .progress="text")
names(all) <- paste0("file", seq_along(all))

m <- melt(all, id = "id") # to long format
str(m)

## facetted plot
ggplot(m) + facet_wrap( ~ L1, ncol = 4 ) +
  geom_path(aes(id, value, colour = variable))

ggsave("facetted_plot.pdf", width = 10, height = 10)

## alternative: multiple plots
library(gridExtra)
plots <- dlply(m, "L1", function(d) {
  ggplot(d) + geom_path(aes(id, value, colour = variable))
})
ml <- do.call(marrangeGrob, c(plots, list(nrow=3, ncol=4)))

ml
## multipage pdf with 3x4 on each page
ggsave("multipage.pdf", ml)

(untested)

于 2012-05-03T22:26:32.657 回答
3

Making these kinds of composite plots is really easy to do using either lattice or ggplot2. I favor ggplot2, where these kinds of composite plots are constructed using facetting. A visual teaser of how such a plot would look like (from here):

p <- ggplot(diamonds, aes(carat, ..density..)) + 
  geom_histogram(binwidth = 0.2) + facet_grid(clarity ~ cut) 

enter image description here

This is a histogram, but you can quite easily draw timeseries like this. Or take a look at figure 3.6 from a report I wrote. Note that there is no need to create separate plots, ggplot2 takes care of it all, it is one big plot with a lot of subplots. Take a look at the ggplot2 docs here to get more detail.

于 2012-05-03T21:50:04.340 回答