[R] histogam plots

Dieter Menne dieter.menne at menne-biomed.de
Thu Feb 11 19:35:33 CET 2010



Dennis Murphy wrote:
> 
> 
> ## Histogram + density plots in lattice and ggplot2
> 
> 

There was a typo in your example (should be dd <- data.frame), but anyway.
ggplot2 needs 10 times as long. While Bill Venables (?) would argue "what
are you going to do with 3 seconds in your life", it adds up in lengthy
Sweave reports (yes... caching would be an option).

Still preferring lattice.

Dieter


# example data frame
library(ggplot2)
library(lattice)

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

# lattice
system.time(
print(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',...)
               } ))
)
# 0.33 seconds

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

# 3.7 seconds

-- 
View this message in context: http://n4.nabble.com/histogam-plots-tp1476859p1477632.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list