[R] Need help putting histograms on the diagonal of a splom plot
Benjamin Barnes
b.barnes at dkfz-heidelberg.de
Fri Sep 21 09:33:44 CEST 2007
Hello,
I think the histograms may have been unintentionally omitted from the
examples below. Borrowing from a couple of sources, here's a function to
get the histograms instead of the density plot:
panel.hist.splom<-function(x, ...)
{
yrng <- current.panel.limits()$ylim
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- yrng[1] + 0.95 * diff(yrng) * y / max(y)
panel.rect(breaks[-nB], yrng[1], breaks[-1], y, col="cyan", ...)
}
-Ben
From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com
<mailto:deepayan.sarkar_at_gmail.com?Subject=Re:%20%5BR%5D%20Need%20help%20putting%20histograms%20on%20the%20diagonal%20of%20a%20splom%20plot>>
Date: Fri, 31 Aug 2007 14:02:27 -0700
On 8/30/07, Marc Paterno <paterno_at_fnal.gov> wrote:
> Hello,
<http://tolstoy.newcastle.edu.au/R/e2/help/07/08/24539.html#24614qlink1>
/> /
/> I am in need of help in putting histograms on the diagonal of a plot /
/> produced with splom(). /
/> /
/> The plot matrix I am trying to produce is to have standard scatterplots /
/> in the upper-left triangle, contour plots in the lower-right triangle, /
/> and histograms on the diagonal. I have a function that does the first /
/> two, but the histograms on the diagonal has been beyond my ability. /
/> /
/> Here is my function: /
/> /
/> require(lattice) /
/> require(MASS) /
/> my.plot = function(data) /
/> { /
/> splom( ~data /
/> , lower.panel=function(x,y, ...) /
/> { /
/> xy=kde2d(x,y) /
/> xy.tr=con2tr(xy) /
/> panel.contourplot( xy.tr$x /
/> , xy.tr$y /
/> , xy.tr$z /
/> , subscripts=seq(nrow(xy.tr)) /
/> , contour=TRUE /
/> , region=TRUE /
/> , labels = FALSE /
/> , col.regions = terrain.colors /
/> ) /
/> } /
/> , upper.panel=function(x,y, ...) /
/> { /
/> panel.grid(-1,-1) /
/> panel.xyplot(x,y, cex=0.5) /
/> } /
/> #, diag.panel=function(x, ...) /
/> # { /
/> # panel.histogram(x, ...) /
/> # } /
/> ) /
/> } /
/> /
/> It can be called, for example, with: /
/> /
/> my.plot(subset(iris, select = Sepal.Length:Petal.Width)) /
/> /
/> (the subset is necessary to get rid of a variable that is a factor; my /
/> function can not deal with factors). /
/> /
/> I have commented out my best guess at the code needed to produce the /
/> histograms along the diagonal, which fails. /
Well, basically the y-axis range of the diagonal panels are not right.
What you want is simpler if you are happy with a density estimate:
my.plot = function(data)
{
splom( ~data
#, lower.panel=...
#, upper.panel=...
, diag.panel = function(x, ...)
{
yrng <- current.panel.limits()$ylim
d <- density(x)
d$y <- with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y) )
panel.lines(d)
})
}
my.plot(iris[1:4])
For a histogram, things are a bit more complicated, but still easy enough:
my.plot = function(data)
{
splom( ~data
#, lower.panel=...
#, upper.panel=...
, diag.panel = function(x, ...)
{
yrng <- current.panel.limits()$ylim
d <- density(x)
d$y <- with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y) )
panel.lines(d)
})
}
-Deepayan
--
Benjamin Barnes, MEM
Doctoral Student
Department of Environmental Epidemiology
German Cancer Research Center
Im Neuenheimer Feld 280
D-69120 Heidelberg
More information about the R-help
mailing list