r - aligning patterns across panels with gridExtra and grid.pattern() -
the gridextra
package adds grob of class "pattern" lets 1 fill rectangles patterns. example,
library(gridextra) grid.pattern(pattern = 1)
creates box filled diagonal lines. want create stack of panels in each panel filled these diagonal lines. easy:
library(lattice); library(gridextra) exampleplot <- xyplot( 1 ~ 1 | 1:2, panel = function () grid.pattern(pattern = 1), layout = c(1, 2), # remove distracting visual detail scales = list(x=list(draw=false), y=list(draw=false)), strip = false, xlab = '', ylab = '' ) print(exampleplot)
the problem diagonal lines aren't aligned across panels. is, there visual "break" bottom of first panel meets top of second panel: @ point, lines don't line up. problem want fix.
i can eliminate of visual break adding argument pattern.offset = c(.2005, 0)
grid.pattern
call, , making sure applies bottom panel. solution doesn't generalize. example, if change pattern (e.g., using granularity
argument grid.pattern
), solution won't work. there more general fix?
to make work, you'll have take charge of setting panel.height
argument used print.trellis
. (to see why, try resizing plotting device after running example code: size of device , panels changes, matching/mismatching of lines):
## calculate vertical distance (in mm) between 45 degree diagonal lines ## spaced 5mm apart (the default distance grid.pattern). vdist <- 5 * sqrt(2) nlines <- 8l ## can integer panelheight <- list(x = nlines*vdist, units = "mm", data = null) ## plot print(exampleplot, panel.height=panelheight)
Comments
Post a Comment