Hi:

## Histogram + density plots in lattice and ggplot2

# example data frame
x <- data.frame(x = rnorm(600), g = factor(rep(1:6, each = 100)))

# lattice
library(lattice)
histogram(~ x | g, data = dd, type = 'density',
      panel = function(x, subscripts, ...) {
                panel.histogram(x, ...)
                panel.mathdensity(dnorm, col = 'red', ...)
                panel.densityplot(x, plot.points = FALSE, col = 'navy',...)
               } )

# ggplot2
library(ggplot2)
p <- ggplot(data = dd, aes(x = x, group = g))
p + geom_histogram(aes(y = ..density..), binwidth = 0.5) +
    geom_density(color = 'navy') + stat_function(fun = 'dnorm') +
    facet_wrap(~ g, ncol = 3) + ylab("Density")


If you prefer the ordering given by ggplot2, then in the lattice invocation,
add    as.table = TRUE,     in the line above the panel function call.


HTH,
Dennis

On Thu, Feb 11, 2010 at 7:50 AM, li li <hannah....@gmail.com> wrote:

> Thank you very much for your reply. That was very helpful.
> I also want to add two density curves on top of each histgram. One is the
> density curve of a standard normal random variable. The other is the
> denstity curve according to the histgram.
> I was trying to use the function
> "panel.mathdensity", but not successful.
>
> Can anyone give me some help?
>
> Thank you!
> 2010/2/11 <bill.venab...@csiro.au>
>
> > One way round this is to use lattice.  With your matrix 'w' you might
> >
> > W <- data.frame(w = as.vector(w),
> >                          r = factor(as.vector(row(w))) )
> >
> > require(lattice)
> > histogram(~w|r, W)
> >
> > Identical axis systems will be used for all panels.
> >
> >
> > Bill Venables
> > CSIRO/CMIS Cleveland Laboratories
> >
> >
> > -----Original Message-----
> > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> > On Behalf Of li li
> > Sent: Thursday, 11 February 2010 2:52 PM
> > To: r-help
> > Subject: [R] histogam plots
> >
> > Hi all,
> >   I want to draw a histgram for each row of a matrix and compare them.
> > However the plot I
> > got does not have the same y range and x range, which makes it difficult
> to
> > make the comparison.
> > Is there a  easy way to fix the x range and y range in a xy plot for
> > several
> > plots, instead of specifying
> > them for each plot.
> >  The following is my code for generalizing the matrix and draw the
> > histogram.
> >
> >
> > ############gen is the function to generate the dta
> > gen <- function(m, rho) {
> > library(MASS)
> > set.seed(103)
> > theta <- 0
> > theta1 <- 2
> > pi0 <- 0.9
> > mzero <- pi0*m
> > mean <- c(rep(theta, mzero), rep(theta1,m-mzero))
> > J <- rep(1, m)
> > var <- function(rho) {(1-rho)*diag(m)+ rho*J%*%t(J)}
> > t <- mvrnorm(1, mean, var(rho))
> > return(t)
> >                        }
> > ####### w is the matrix. A histgram is drawn for each of the rows.
> > n <- 1000
> > r <- seq(0,0.9, by=0.1)
> >
> > w <- matrix(0, ncol=n, nrow=length(r))
> > for (i in 1: length(r)){w[i,]<- gen(n,r[i])}
> >
> > par(mfrow=c(2,5))
> > hist(w[1,], breaks=100)
> > hist(w[2,], breaks=100)
> > hist(w[3,], breaks=100)
> > hist(w[4,], breaks=100)
> > hist(w[5,], breaks=100)
> > hist(w[6,], breaks=100)
> > hist(w[7,], breaks=100)
> > hist(w[8,], breaks=100)
> > hist(w[9,], breaks=100)
> > hist(w[10,], breaks=100)
> >
> > ##############################
> >  Thank you !
> >                              Li
> >
> >        [[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<
> 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.
>

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

Reply via email to