s-news
[Top] [All Lists]

Re: trellis plots question

To: "'Dave Evens'" <devens8765@yahoo.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: trellis plots question
From: <Rich@Mango-Solutions.com>
Date: Mon, 12 Mar 2007 09:30:11 -0000
In-reply-to: <71567.71571.qm@web58310.mail.re3.yahoo.com>
Thread-index: Acdj8fTg7XYRp79sS4qgGcZM+rRkcgAk6BHg
Hi Dave,

You could do this in the panel function.  Unfortunately, the panel.histogram
function doesn't accept "the data", so we have to use a few ugly S tricks:

> myDf <- data.frame(X = sample(LETTERS[1:4], 100, T), Y = rnorm(100),
stringsAsFactors = F)
> panel.mango <- function(x, y, col = trellis.par.get("bar.fill")$col,
border = 1, myMeans = NULL, ...)
{
        panel.histogram(x, y, col, border, ...)
        whichCell <- get("cell", frame = sys.parent())
        abline(v = myMeans[whichCell], col = 8, lwd = 3)
}
> histogram( ~ Y | X, data = myDf, as.table = T, panel = panel.mango, strip
= function(...) strip.default(..., style = 1), myMeans = tapply(myDf$Y,
myDf$X, mean))

In the above, please note that it is vital the means are passed in in the
correct order (told you it was ugly).

Quick alternative: you could print the mean in each header by creating a new
trellis variable containing the means you want.  Here is a quick and dirty
piece of code ...

> # Create the data
> myDf <- data.frame(X=sample(LETTERS[1:4], 100, T), Y=rnorm(100),
stringsAsFactors=F)

> # Current version of the plot
> histogram(~Y|X, data=myDf, as.table=T, strip=function(...)
strip.default(..., style=1))

> # New version - create "newX" trellis variable
> xMeans <- round(tapply(myDf$Y, myDf$X, mean), 2)
> myDf$newX <- paste(myDf$X, " (Mean =", xMeans[myDf$X], ")", sep="")
> histogram(~Y|newX, data=myDf, as.table=T, strip=function(...)
strip.default(..., style=1))

Many thanks,
Rich.

mangosolutions
data analysis that delivers

-----Original Message-----
From: Dave Evens [mailto:devens8765@yahoo.com] 
Sent: 11 March 2007 15:28
To: s-news@lists.biostat.wustl.edu
Subject: [S] trellis plots question

Dear members, 

I would like to produce a multi-histogram plot of some
data such that each sub-plot has a mark in the heading
bar to represent the mean,

For example, I have the following data 

>x <- matrix(c(runif(15), sample(4, 15, T)), 15) 

plot the data 

>histogram(~x[,1]|as.factor(x[,2]), layout=c(2,2)) 

and the means are

>tapply(x[,1], x[,2], mean)

         1        2         3         4 
 0.6384744 0.677662 0.5630317 0.5473149


I want to put this mean data into each sub-plot such
that the top (heading) panel has a mark in the correct
position on the x-axis to represent the mean. Is this
possible?

Many thanks in advance for your help.

Regards,
Dave



 
____________________________________________________________________________
________
Bored stiff? Loosen up... 
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>