Skip to content

Multiple plots per window

3 messages · p at fo76.org, Gabor Grothendieck

#
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):
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
#
sorry, as Mark Leeds pointed out to me, the row/column numbers where
mixed up in my example... happens when you cut & paste like mad from
your history... it should read as follows:

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(13,4)

sales2.1.dataframe <- as.data.frame(sales2.1.matrix)
names(sales2.1.dataframe) <- c("1995","1996","1997","1998")

Peter

Quoting p at fo76.org:
#
Here are two ways: one using classic graphics and one much
shorter way using lattice.   ggplot2 would be a another short way
(not shown).

Lines <- "1995 1996 1997 1998
  153  133  145  111
  189  177  200  170
  221  241  187  243
  215  228  201  178
  302  283  292  248
  223  255  220  202
  201  238  233  163
  173  164  172  139
  121  128  119  120
  106  108   81   96
   86   87   65   95
   87   74   76   53
  108   95   74   94"

# read in data and remove the X from the column names
s <- read.table(textConnection(Lines), header = TRUE)
names(s) <- sub("X", "", names(s))

# 1. using classic graphics

# find overall ranges of x and y
h <- lapply(s, hist, probability = TRUE)
ylim <- range(unlist(lapply(h, "[[", "density")))
xlim <- range(unlist(lapply(h, "[[", "breaks")))

# plot
opar <- par(mfrow = c(2, 2))
for(i in 1:length(s)) {
 hist(s[[i]], main = names(s)[i], probability = TRUE,
	xlab = "Sales", xlim = xlim, ylim = ylim)
 lines(density(s[[i]]))
}
par(opar)

# 2. using lattice its a bit easier

library(lattice)
histogram( ~ values | ind, stack(s), type = "density",
	panel = function(...) {
		panel.histogram(...)
		panel.densityplot(...)
	}
)
On Sun, Sep 21, 2008 at 7:19 PM, <p at fo76.org> wrote: