Andy Bunn
2012-May-29 18:09 UTC
[R] lattice: add a marginal histogram on top of the colorkey of a levelplot?
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")))
ilai
2012-May-29 19:09 UTC
[R] lattice: add a marginal histogram on top of the colorkey of a levelplot?
?plot.trellis. In general something like mlp<- levelplot(... mhist<- histogram(... plot(mlp,split=c(1,2,1,2),more=T) plot(mhist,split=c(1,1,1,2),more=F) You will need to do some work on the padding and layout widths to get the distances right (I assume the key is to be the "x-axis" of the histogram). Also, might be worth checking out ?c.trellis in package latticeExtra because I find sometimes easier to draw the key as a separate grob and use x.same=T. Up to you. Cheers On Tue, May 29, 2012 at 12:09 PM, Andy Bunn <Andy.Bunn@wwu.edu> wrote:> 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"))) > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]