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
Hi:
Here's one way with the plyr package and function d_ply(); the hist()
function itself is not very elegant, but it 'works' for this example.
Factor <- rep(factor(letters[1:4]), each = 10)
Size <- runif(40) * 100
library(plyr)
par(mfrow = c(2, 2))
d <- data.frame(Factor, Size)
# Function to produce a histogram for a generic (subset of a) data frame
f <- function(df) {
hist(df$Size, main = df$Factor[1],
xlab = paste('n =', nrow(df)), ylab = '')
}
d_ply(d, 'Factor', f)
d2 <- data.frame(Factor = rep(LETTERS[1:4], c(10, 20, 30, 40)),
Size = runif(100, 0, 100))
d_ply(d2, 'Factor', f)
# Another way, using a loop with data frame d2 above:
par(mfrow = c(2, 2))
for(i in seq_along(levels(d2$Factor))) {
val <- levels(d2$Factor)[i]
df <- subset(d2, Factor == val)
hist(unlist(df['Size']), main = val,
xlab = paste('n =', nrow(df)), ylab = '')
}
par(mfrow = c(1, 1))
The advantage of the plyr method is that you can change the data frame, and
as long as it has the same variable names with the same classes, it should
work.
Except for the sample sizes as x-axis labels, this could be easily done in
lattice in one line:
library(lattice)
histogram(~ Size | Factor) # if using the original vectors
histogram(~ Size | Factor, data = d2) # if using a data frame instead
# change the panel ordering and use counts instead of percents
histogram(~ Size | Factor, data = d2, as.table = TRUE, type = 'count')
I'll give someone else the opportunity to show how to get the sample sizes
as x-labels in each panel; I'm not that good at writing panel functions in
lattice.
HTH,
Dennis
On Mon, Feb 21, 2011 at 1:25 AM, Darcy Webber
<darcy.webber@gmail.com>wrote:
> 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@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.
>
[[alternative HTML version deleted]]
On Mon, Feb 21, 2011 at 4:25 AM, Darcy Webber <darcy.webber at gmail.com> wrote:> 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. >In the above example Factor has length 40 so the for loop produces 40 plots and you see the last 4. You really want to loop over the levels of Factor, not Factor itself: for(i in levels(Factor)) { ... } -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
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.
>