[R] lattice histogram log and non log values

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Jun 30 09:32:12 CEST 2012


On Thu, Jun 28, 2012 at 2:41 AM, LCOG1 <jroll at lcog.org> wrote:
> Hello all,
>  Please consider the following
>
> library(lattice)
> Colors. <-rep(brewer.pal(7, "Dark2"),2)
> color <- 1
>
> Data.X.. <- data.frame(UnitArea = c(rnorm(1000), rnorm(1000)), Type =
> c(rep("Base",1000),rep("Log",1000)))
>
>                        histogram( ~ UnitArea |  Type, data = Data.X..,
>          xlab = "Unit Area", type = "density",
>          panel = function(x, ... ){
>              panel.histogram(x, ...)
>              panel.mathdensity(dmath = dnorm, col = "black",
>                                args = list(mean=mean(x),sd=sd(x)))
>          }, col = Colors.[color], layout = c(1, 2),
>                   scales=list(log = c(F,T),tick.number=list(8), rot = c(0, 90),
>                 x = list(relation = 'free')))
>
> I want to plot on the same page distributions both observed values and the
> logged values.  I tried using the log parameter e.g. log = c(F,T) but I dont
> think this is right.    When I tried transforming the data before plotting
> the scales were all messed up. Guidance would be appreciated.  Thanks

The latter would be the better approach. You haven't given code, but
you probably didn't add 'breaks=NULL', and without it all panels will
have a common set of breakpoints, so scales effectively will be the
same in all panels.

This works for me:

xx <- exp(rnorm(1000))

DF <- data.frame(UnitArea = c(xx, log(xx)),
                 Type = c(rep("Base",1000), rep("Log",1000)))

histogram( ~ UnitArea |  Type, data = DF,
          xlab = "Unit Area", type = "density",
          panel = function(x, ... ){
              panel.histogram(x, ...)
              panel.mathdensity(dmath = dnorm, col = "black",
                                args = list(mean=mean(x), sd=sd(x)))
          },
          breaks = NULL,
          col = Colors.[color], layout = c(1, 2),
          scales = list(x = list(relation = 'free')))


> Also, is there a way to simply plot multiple panels like the base graphics
> package using  par(new = TRUE) in the following?  It just replaces the first
> plot so maybe I shouldn't be trying to use the lattice package with the base
> graphics package.

Yes, see ?plot.trellis.

-Deepayan



More information about the R-help mailing list