[R] Multiple plots per window

p at fo76.org p at fo76.org
Mon Sep 22 00:34:51 CEST 2008


Hi all,

I'm currently working through "The Analysis of Time Series" by Chris
Chatfield. In order to also get a better understanding of R, I play
around with the examples and Exercises (no homework or assignement,
just selfstudy!!).

Exercise 2.1 gives the following dataset (sales figures for 4 week
intervals):

> sales2.1.dataframe
    1995 1996 1997 1998
1   153  133  145  111
2   189  177  200  170
3   221  241  187  243
4   215  228  201  178
5   302  283  292  248
6   223  255  220  202
7   201  238  233  163
8   173  164  172  139
9   121  128  119  120
10  106  108   81   96
11   86   87   65   95
12   87   74   76   53
13  108   95   74   94

I want to plot the histograms/densities for all four years in one window.
After trying out a couple of things, I finally ended up with the following
(it took me two hours - Ouch!):

sales2.1 <- c(153,189,221,215,302,223,201,173,121,106,86,87,108,
133,177,241,228,283,255,238,164,128,108,87,74,95,
145,200,187,201,292,220,233,172,119,81,65,76,74,
111,170,243,178,248,202,163,139,120,96,95,53,94)
sales2.1.matrix <- sales2.1
dim(sales2.1.matrix) <- c(4,13)
sales2.1.dataframe <- as.data.frame(sales2.1.matrix)
names(sales2.1.dataframe) <- c("1995","1996","1997","1998")

X11()
split.screen(c(2,2))
for (i in 1:4)
{
     screen(i)
     hist(sales2.1.dataframe[[i]],
         probability=T,
         xlim=c(0,400),
         ylim=c(0,0.006),
         main=names(sales2.1.dataframe)[i],
         xlab="Sales")
     lines(density(sales2.1.dataframe[[i]]))
}
close.screen(all=TRUE)

Although I'm happy that I finally got something that is pretty close
to what I wanted, I'm not sure whether this is the best or most elegant
way to do it. How would you do it? What functions/packages should I
look into, in order to improve these plots?

Thanks in advance for your comments and suggestions,

Peter



More information about the R-help mailing list