Using the layout() function to generate multiple plots

The par() function's mfrow= argument is often used to generate multiple plots on one page. However, as you've probably noticed, with mfrow=, it is difficult to 'skip' plotting slots and to make plots of unequal sizes.

The layout() function is an alternative to the par() function's mfrow= argument, which allows you to do both of these quite easily.

As an example, let's generate three bar plots on one page using the datta.csv data file. Specifically, the three barplots will occupy two 'rows' on the page, with the first barplot centered on the first row.

Let's first read in the data set, and generate some needed subsets of the data. library(Hmisc) x <- read.table("datta.csv", sep=",", header=TRUE)

age1 <- as.matrix(subset(x, subset=agegrp=="0-5 months", select=Cs(nashville, rochester, cincinnati))) rownames(age1) <- levels(x$year) colnames(age1) <- Cs(Nashville, Rochester, Cincinnati)

age2 <- as.matrix(subset(x, subset=agegrp=="6-23 months", select=Cs(nashville, rochester, cincinnati))) rownames(age2) <- levels(x$year) colnames(age2) <- Cs(Nashville, Rochester, Cincinnati)

age3 <- as.matrix(subset(x, subset=agegrp=="24-59 months", select=Cs(nashville, rochester, cincinnati))) rownames(age3) <- levels(x$year) colnames(age3) <- Cs(Nashville, Rochester, Cincinnati)

Now let's generate the plots. With the layout() function, the first argument (and the only required argument) is a matrix. The number of rows and columns in the matrix determine the number of rows and columns in the layout. The contents of the matrix are integer values that determine which rows and columns each figure will occupy. A value of 0 can also be used, which means that no figure will occupy the specified region. For this example, we generate a 2 row X 4 column matrix. The first barplot will occupy the middle two columns of the first row, while the second and third barplots will each occupy two columns on the second row. layout(matrix(c(0,1,1,0,2,2,3,3), ncol=4, byrow=TRUE)) barplot(t(age1), beside=TRUE, ylim=c(0, 12), axis.lty=1, ylab="Hospitalizations per 1000 children", main="0-5 months", xlab="Year", col=Cs(gray, white, black)) legend(1, 12, fill=Cs(gray, white, black), legend=Cs(Nashville, Rochester, Cincinnati))

barplot(t(age2), beside=TRUE, ylim=c(0, 12), axis.lty=1, ylab="Hospitalizations per 1000 children", main="6-23 months", xlab="Year", col=Cs(gray, white, black))

barplot(t(age3), beside=TRUE, ylim=c(0, 12), axis.lty=1, ylab="Hospitalizations per 1000 children", main="24-59 months", xlab="Year", col=Cs(gray, white, black))

An alternative of generating three different barplots would be to generate one plot with lines illustrating the heights of the bars. For this example, we'll use the figure3data.csv data file. y <- read.table("figure3data.csv", header=TRUE, sep = ",") contents(y)

Before we plot anything, we need to change the order of the levels of the Group variable (i.e., '0-5 months', '6-23 months', and '24-59 months'), and to sort the data according to this order. We also need to return the layout of the plotting region. levels(y$Group) <- c("0-5 months", "6-23 months", "24-59 months") y <- y[order(y$Group),]

layout(matrix(1, ncol=1))

Now we can generate the line plot. with(y, plot(rate ~ as.numeric(Group), type="n", axes=FALSE, ylab="Hospitalizations per 1000 children", xlab="Age Group (months)", main="Nashville")) axis(side=2) axis(side=1, at=1:3, labels=c("0-5", "6-23", "24-59")) box()

with(subset(y, location=="Nashville" & year=="2000-2001"), lines(rate ~ as.numeric(Group), pch=19, type="b", col=1)) with(subset(y, location=="Nashville" & year=="2001-2002"), lines(rate ~ as.numeric(Group), pch=19, type="b", col=2)) with(subset(y, location=="Nashville" & year=="2002-2003"), lines(rate ~ as.numeric(Group), pch=19, type="b", col=4)) with(subset(y, location=="Nashville" & year=="2003-2004"), lines(rate ~ as.numeric(Group), pch=19, type="b", col=5)) legend(3, 10, legend=levels(y$year), lty=1, col=c(1, 2, 4, 5), pch=19, xjust=1)
Topic revision: r2 - 14 Nov 2006, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback