[R] Need help putting histograms on the diagonal of a splom plot
Deepayan Sarkar
deepayan.sarkar at gmail.com
Fri Aug 31 23:02:27 CEST 2007
On 8/30/07, Marc Paterno <paterno at fnal.gov> wrote:
> Hello,
>
> 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
More information about the R-help
mailing list