[R] lattice: add a marginal histogram on top of the colorkey of a levelplot?

Andy Bunn Andy.Bunn at wwu.edu
Tue May 29 20:09:23 CEST 2012


Lattice experts:

Can you think of a way to produce a levelplot as below and then add a histogram of the z variable to the top margin of the plot that would sit on top of the color key?



x <- seq(pi/4, 5 * pi, length.out = 100)
y <- seq(pi/4, 5 * pi, length.out = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
my.levs <- seq(-1,1,by=0.1)
my.cols <- grey(0:length(my.levs)/length(my.levs))
levelplot(z~x*y, grid, at=my.levs, scales=list(log="e"), xlab="",
          ylab="",colorkey = list(space = 'top'),col.regions = my.cols)
# is there a way to add a marginal histogram above the colorkey?
histogram(~z, grid, breaks=my.levs,col=my.cols,xlab='',ylab='',
    scales = list(draw = FALSE),
    par.settings = list(axis.line = list(col = "transparent")))



More information about the R-help mailing list