[R] multiple plots using a loop

2011-02-21 Thread Darcy Webber
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.


Re: [R] multiple plots using a loop

2011-02-21 Thread Dennis Murphy
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.web...@gmail.comwrote:

 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]]

__
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.


Re: [R] multiple plots using a loop

2011-02-21 Thread ONKELINX, Thierry
It is also a one-liner with ggplot2

dataset - data.frame(Factor = rep(factor(letters[1:4]), each = 10),
Size = runif(40) * 100)
library(ggplot2)
ggplot(dataset, aes(x = Size)) + geom_histogram() + facet_wrap(~Factor)



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie  Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics  Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
  

 -Oorspronkelijk bericht-
 Van: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] Namens Dennis Murphy
 Verzonden: maandag 21 februari 2011 12:45
 Aan: Darcy Webber
 CC: r-help@r-project.org
 Onderwerp: Re: [R] multiple plots using a loop
 
 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.web...@gmail.comwrote:
 
  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]]
 
 __
 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.
 
__
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.


Re: [R] multiple plots using a loop

2011-02-21 Thread Gabor Grothendieck
On Mon, Feb 21, 2011 at 4:25 AM, Darcy Webber darcy.web...@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

__
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.


Re: [R] multiple plots using a loop

2011-02-21 Thread Iain Gallagher
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.web...@gmail.com wrote:

 From: Darcy Webber darcy.web...@gmail.com
 Subject: [R] multiple plots using a loop
 To: r-help@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@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.


__
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.