plot - Nested layouts in R -
i want make plot consisting of multiple plots consisting of multiple plots, 5x2 grid 3 plots in each cell. more precise, need not 1 figure finding way of using plotting function multiple times in single plot.
i have written function uses layout stack plots, common axis in outer margin. need seqiplot , seqdplot functions traminer package, far understand problem not related those, here minimal working example barplot.
stackedplot <- function(data){ layout(matrix(c(1:3), nrow=3)) par(mar=c(0,0,0,0), oma=c(4,1,1,1), mgp=c(3,0.5,0), cex=1) barplot(data[[1]], axes=f, xlab="", ylab="", horiz=true) barplot(data[[2]], axes=f, xlab="", ylab="", horiz=true) barplot(data[[3]], axes=f, xlab="", ylab="", horiz=true) axis(1, at=c(0:10)/10, outer=true) mtext("label", line=2, side=1) } stackedplot(list(1:10, 10:1, rep(1,10)))
what use layout again , use stackedplot grids of layout, i.e. (which, of course, not work):
layout(matrix(c(1:2), nrow=1)) stackedplot(list(1:10, 10:1, rep(1,10))) stackedplot(list(rep(1,10), 1:10, 10:1))
i have tried split.screen, no success:
split.screen(c(1,2)) screen(1) stackedplot(list(1:10, 10:1, rep(1,10))) screen(2) stackedplot(list(rep(1,10), 1:10, 10:1)) close.screen(all = true)
i tried grid package, apparently not compatible base graphics.
grid.newpage() pushviewport(viewport(x=0, y=0, width=0.5, height=1, default.units="native")) print(stackedplot(list(1:10, 10:1, rep(1,10))), newpage=false) pushviewport(viewport(x=0.5, y=0, width=0.5, height=1, default.units="native")) print(stackedplot(list(rep(1,10), 1:10, 10:1)), newpage=false)
after more research , answering own question in case useful else.
nested layouts can created grid
package, can used base graphics using gridbase
package. function stacked plots written follows.
library(grid) library(gridbase) stackedplot <- function(data, main=""){ top.vp <- viewport(layout=grid.layout(nrow=5, ncol=1, heights=unit(c(3, 1, 1, 1, 5), c("lines", "null", "null", "null", "lines"))), width=unit(0.9, "npc")) title <- viewport(layout.pos.row=1, layout.pos.col=1, name="title") p1 <- viewport(layout.pos.row=2, layout.pos.col=1, name="plot1") p2 <- viewport(layout.pos.row=3, layout.pos.col=1, name="plot2") p3 <- viewport(layout.pos.row=4, layout.pos.col=1, name="plot3") xaxis <- viewport(layout.pos.row=5, layout.pos.col=1, name="xaxis") splot <- vptree(top.vp, vplist(title, p1, p2, p3, xaxis)) # defining hierarchy of viewports pushviewport(splot) # creating viewports plotting definitions of splot upviewport() # navigating in viewport tree downviewport("plot1") # navigating down in viewport tree, searching viewport "plot1" grid.rect() # plotting rectangle (borders viewport) par(plt=gridplt(), new=true) # taking dimensions of viewport base graphics plot # adding plot existing plot barplot(data[[1]], axes=false, xlab="", ylab="", horiz=true) upviewport() downviewport("plot2") grid.rect() par(plt=gridplt(), new=true) barplot(data[[2]], axes=false, xlab="", ylab="", horiz=true) upviewport() downviewport("plot3") grid.rect() par(plt=gridplt(), new=true) barplot(data[[3]], xlab="", ylab="", horiz=true) upviewport() downviewport("xaxis") grid.text("x label", y = unit(2, "lines")) upviewport() downviewport("title") grid.text(main, y = unit(1, "lines")) upviewport(2) }
the function first describes viewport (of 90% of window's width) divided 5x1 grid of viewports differing heights. each viewport in grid given name can later called. tree of viewports (splot) described vptree
defines hierarchical structure of viewports. after describing viewports prepared plotting pushviewport
.
now each named viewport first seeked , opened plotting upviewport
(which goes in viewport tree) , downviewport
(which seeks requested viewport down in viewport tree).
for plotting base graphics, gridplt
needed here (alternatively gridfig
or gridomi
can used, see manual of gridbase further info). after base graphics function can used plot current viewport.
after requested plots, upviewport(2)
used navigate root (2 viewports in hierarchy).
now stackedplot function can called multiple times in grid follows.
opar <- par(no.readonly=true) # saving graphical parameters plot.new() # needed par(new=true) in stackedplot() multitop.vp <- viewport(layout=grid.layout(1,2), width = unit(0.95, "npc")) pl1 <- viewport(layout.pos.col=1, layout.pos.row=1, name="a") pl2 <- viewport(layout.pos.col=2, layout.pos.row=1, name="b") vpall <- vptree(multitop.vp, vplist(pl1,pl2)) pushviewport(vpall) upviewport() downviewport("a") stackedplot(data=list(1:10,10:1,rep(10,10)),main="a") upviewport() downviewport("b") stackedplot(data=list(10:1,rep(10,10),1:10),main="b") upviewport(2) par(opar) # returning graphical parameters saved earlier
Comments
Post a Comment