Re: [R] Multiple density curves

2006-08-11 Thread Gabor Grothendieck
From your description I assume you want both histograms
and the densities all on the same chart.  With existing R
graphics I am not sure that there really is a simple way to
do that.

That aside, note that the hist function returns a list of
components that includes

- breaks, defining the breakpoints of the histogram
- intensities defining the heights of the histogram bars

We can use these two to determine the breaks and y limits
of the combined plot and then use the breaks= and ylim=
arguments of hist to specify them so that both histograms
can be drawn on the same chart.  We also use freq=FALSE
in the hist calls to draw intensities rather than counts.  On
the second hist call we use add=TRUE to cause it to be drawn
on the existing plot.

The other problem is to distinguish the superimposition of
the bars and that can be handled by using shading lines of
different colors and angles using the col= and angle= and
density= arguments of hist.


# data
DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
A1, A2), class = data.frame, row.names = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13))

# determine breaks and y limits of the combined plot
breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks
ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities)
ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities)
ylim - c(0, max(ymax1, ymax2))

# draw the two histograms and two densities
hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10)
lines(density(DF$A1), col = red)
hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10)
lines(density(DF$A2), col = blue)

On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote:
 Hi,

 I am new to R...a recent convert from SAS.
 I have a dataset that looks like this:

 SEQA1A2
 A532.5554.5
 B25.535.5
 C265.2522.2
 D245.55521.56
 E546.52141.52
 F243.2532.56
 G452.55635.56
 H15.1416.54
 I543.4646.56
 J54.4654.5
 K646.564.54
 L645.4614.46
 M646.54634.46

 I want to make a histogram each for A1 and A2, with density curves, on the
 same plot so that I can see how they overlap.

 Please let me know some simple code for this.

 I looked at ldahist but it was complicated. Anything simpler?

 Thanks a lot,
 -DS.

[[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 density curves

2006-08-11 Thread Gabor Grothendieck
The code below was missing the breaks= argument to hist.
I had not noticed because coincidentally both give the same
breaks anways thus the following corrected version gives the
same plot in this case but might not in other cases.

# data
DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
A1, A2), class = data.frame, row.names = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13))

# determine breaks and y limits of the combined plot
breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks
ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities)
ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities)
ylim - c(0, max(ymax1, ymax2))

# draw the two histograms and two densities
hist(DF$A1, ang = 45, col = red, ylim = ylim,
breaks = breaks, freq = FALSE, density = 10)
lines(density(DF$A1), col = red)
hist(DF$A2, ang = -45, col = blue, add = TRUE,
breaks = breaks, freq = FALSE, density = 10)
lines(density(DF$A2), col = blue)



On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 From your description I assume you want both histograms
 and the densities all on the same chart.  With existing R
 graphics I am not sure that there really is a simple way to
 do that.

 That aside, note that the hist function returns a list of
 components that includes

 - breaks, defining the breakpoints of the histogram
 - intensities defining the heights of the histogram bars

 We can use these two to determine the breaks and y limits
 of the combined plot and then use the breaks= and ylim=
 arguments of hist to specify them so that both histograms
 can be drawn on the same chart.  We also use freq=FALSE
 in the hist calls to draw intensities rather than counts.  On
 the second hist call we use add=TRUE to cause it to be drawn
 on the existing plot.

 The other problem is to distinguish the superimposition of
 the bars and that can be handled by using shading lines of
 different colors and angles using the col= and angle= and
 density= arguments of hist.


 # data
 DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
 I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
 A1, A2), class = data.frame, row.names = c(1, 2, 3,
 4, 5, 6, 7, 8, 9, 10, 11, 12, 13))

 # determine breaks and y limits of the combined plot
 breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks
 ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities)
 ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities)
 ylim - c(0, max(ymax1, ymax2))

 # draw the two histograms and two densities
 hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10)
 lines(density(DF$A1), col = red)
 hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10)
 lines(density(DF$A2), col = blue)

 On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote:
  Hi,
 
  I am new to R...a recent convert from SAS.
  I have a dataset that looks like this:
 
  SEQA1A2
  A532.5554.5
  B25.535.5
  C265.2522.2
  D245.55521.56
  E546.52141.52
  F243.2532.56
  G452.55635.56
  H15.1416.54
  I543.4646.56
  J54.4654.5
  K646.564.54
  L645.4614.46
  M646.54634.46
 
  I want to make a histogram each for A1 and A2, with density curves, on the
  same plot so that I can see how they overlap.
 
  Please let me know some simple code for this.
 
  I looked at ldahist but it was complicated. Anything simpler?
 
  Thanks a lot,
  -DS.
 
 [[alternative HTML version deleted]]
 
  __
  R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 density curves

2006-08-11 Thread Gabor Grothendieck
Here is one more solution.  This one uses lattice.  Its a bit shorter
than the classic graphics solution.  In the classic graphics version we used
shading and color to distinguish the bars; however, grid, and therefore
lattice, do not easily support shading (its possible to simulate it using low
level vector graphics but that's beyond the scope of this) so we use
width (lwd), style (lty) and colour (col) to distinguish them.  Also note
that the for loop iterates over the groups since the lattice histogram function
does not use the groups= argument of lattice's xyplot.

library(lattice)
# data
DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
A1, A2), class = data.frame, row.names = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13))

histogram(~ unlist(DF[,-1]), type = density,
   panel = function(x, breaks, ...)
 for(j in 2:ncol(DF)) {
panel.histogram(DF[,j], border = j, lwd = j, lty = j,
breaks = breaks, col = transparent, ...)
panel.densityplot(DF[,j], col = j, ...)
   })

On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 The code below was missing the breaks= argument to hist.
 I had not noticed because coincidentally both give the same
 breaks anways thus the following corrected version gives the
 same plot in this case but might not in other cases.

 # data
 DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
 I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
 265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
 645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
 635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
 A1, A2), class = data.frame, row.names = c(1, 2, 3,
 4, 5, 6, 7, 8, 9, 10, 11, 12, 13))

 # determine breaks and y limits of the combined plot
 breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks
 ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities)
 ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities)
 ylim - c(0, max(ymax1, ymax2))

 # draw the two histograms and two densities
 hist(DF$A1, ang = 45, col = red, ylim = ylim,
breaks = breaks, freq = FALSE, density = 10)
 lines(density(DF$A1), col = red)
 hist(DF$A2, ang = -45, col = blue, add = TRUE,
breaks = breaks, freq = FALSE, density = 10)
 lines(density(DF$A2), col = blue)



 On 8/11/06, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  From your description I assume you want both histograms
  and the densities all on the same chart.  With existing R
  graphics I am not sure that there really is a simple way to
  do that.
 
  That aside, note that the hist function returns a list of
  components that includes
 
  - breaks, defining the breakpoints of the histogram
  - intensities defining the heights of the histogram bars
 
  We can use these two to determine the breaks and y limits
  of the combined plot and then use the breaks= and ylim=
  arguments of hist to specify them so that both histograms
  can be drawn on the same chart.  We also use freq=FALSE
  in the hist calls to draw intensities rather than counts.  On
  the second hist call we use add=TRUE to cause it to be drawn
  on the existing plot.
 
  The other problem is to distinguish the superimposition of
  the bars and that can be handled by using shading lines of
  different colors and angles using the col= and angle= and
  density= arguments of hist.
 
 
  # data
  DF - structure(list(SEQ = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  11, 12, 13), .Label = c(A, B, C, D, E, F, G, H,
  I, J, K, L, M), class = factor), A1 = c(532.5, 25.5,
  265.2, 245.55, 546.52, 243.25, 452.55, 15.14, 543.4, 54.4, 646.5,
  645.4, 646.54), A2 = c(554.5, 35.5, 522.2, 521.56, 141.52, 32.56,
  635.56, 16.54, 646.56, 654.5, 64.54, 614.46, 634.46)), .Names = c(SEQ,
  A1, A2), class = data.frame, row.names = c(1, 2, 3,
  4, 5, 6, 7, 8, 9, 10, 11, 12, 13))
 
  # determine breaks and y limits of the combined plot
  breaks - hist(c(DF$A1, DF$A2), plot = FALSE)$breaks
  ymax1 - max(hist(DF$A1, breaks = breaks, plot = FALSE)$intensities)
  ymax2 - max(hist(DF$A2, breaks = breaks, plot = FALSE)$intensities)
  ylim - c(0, max(ymax1, ymax2))
 
  # draw the two histograms and two densities
  hist(DF$A1, ang = 45, col = red, ylim = ylim, freq = FALSE, density = 10)
  lines(density(DF$A1), col = red)
  hist(DF$A2, ang = -45, col = blue, add = TRUE, freq = FALSE, density = 10)
  lines(density(DF$A2), col = blue)
 
  On 8/10/06, Davendra Sohal [EMAIL PROTECTED] wrote:
   Hi,
  
   I am new to R...a recent convert from SAS.
   I have a dataset that looks like this:
  
   SEQA1A2
   A532.5 

[R] Multiple density curves

2006-08-10 Thread Davendra Sohal
Hi,

I am new to R...a recent convert from SAS.
I have a dataset that looks like this:

SEQA1A2
A532.5554.5
B25.535.5
C265.2522.2
D245.55521.56
E546.52141.52
F243.2532.56
G452.55635.56
H15.1416.54
I543.4646.56
J54.4654.5
K646.564.54
L645.4614.46
M646.54634.46

I want to make a histogram each for A1 and A2, with density curves, on the
same plot so that I can see how they overlap.

Please let me know some simple code for this.

I looked at ldahist but it was complicated. Anything simpler?

Thanks a lot,
-DS.

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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 density curves

2006-08-10 Thread Liaw, Andy
Assuming dat is the name of the data frame, this should get you started:

library(lattice)
histogram(~ unlist(dat[-1]) | factor(rep(c(A1, A2), each=nrow(dat))),
  panel=function(...) {
  panel.histogram(...)
  panel.densityplot(...)
  }, type=density)

Andy

From: Davendra Sohal
 
 Hi,
 
 I am new to R...a recent convert from SAS.
 I have a dataset that looks like this:
 
 SEQA1A2
 A532.5554.5
 B25.535.5
 C265.2522.2
 D245.55521.56
 E546.52141.52
 F243.2532.56
 G452.55635.56
 H15.1416.54
 I543.4646.56
 J54.4654.5
 K646.564.54
 L645.4614.46
 M646.54634.46
 
 I want to make a histogram each for A1 and A2, with density 
 curves, on the same plot so that I can see how they overlap.
 
 Please let me know some simple code for this.
 
 I looked at ldahist but it was complicated. Anything simpler?
 
 Thanks a lot,
 -DS.
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.