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: > > > > SEQ A1 A2 > > A 532.5 554.5 > > B 25.5 35.5 > > C 265.2 522.2 > > D 245.55 521.56 > > E 546.52 141.52 > > F 243.25 32.56 > > G 452.55 635.56 > > H 15.14 16.54 > > I 543.4 646.56 > > J 54.4 654.5 > > K 646.5 64.54 > > L 645.4 614.46 > > M 646.54 634.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.