multiple plots using a loop
Hi Darcy
This works for me:
Factor <- rep(factor(letters[1:4]), each = 10)
Size <- runif(40) * 100
par(mfrow = c(2, 2))
for (i in unique(Factor)) {
hist(Size[Factor == i], main = i,
xlab = paste("n =",length(Size[Factor == i])), ylab = "")
}
I think that using for (i in Factor) cycles through every occurrence of a level and so you only get four plots of the last level rather than a plot for every level.
cheers
iain
--- On Mon, 21/2/11, Darcy Webber <darcy.webber at gmail.com> wrote:
From: Darcy Webber <darcy.webber at gmail.com>
Subject: [R] multiple plots using a loop
To: r-help at r-project.org
Date: Monday, 21 February, 2011, 9:25
Dear R users,
I am trying to write myself a loop in order to produce a
set of 20
length frequency plots each pertaining to a factor level. I
would like
each of these plots to be available on the same figure, so
I have used
par(mfrow = c(4, 5)). However, when I run my loop below, it
produces
20 plots for each factor level and only displays the last
factor
levels LF plots. I'm fairly new to loops in R, so any help
would be
greatly appreciated.
I have provided an example data set below if required with
just 4
factors and adjusted par settings accordingly.
Factor <- rep(factor(letters[1:4]), each = 10)
Size <- runif(40) * 100
par(mfrow = c(2, 2))
for (i in Factor) {
LFchart <- hist(Size[Factor == i], main = i,
xlab = c("n =",length(Size[Factor == i])), ylab = "")
}
P.S. Also just a quick annoying question. My xlab
displays:
n =
120
I would like it to display:
n = 120
but just cant get it to work. Any thoughts.
Regar
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.