Search code examples
rplotlattice

Arrange and size multiple plots in Lattice R


I want to plot three plots vary close together, so they appear as one field. My data are arrays with different dimensions. Here is some example code to display my problem:

library(lattice)

theme.novpadding = list(layout.heights = list(top.padding = 0,
                                          main.key.padding = 0,
                                          key.axis.padding = 0,
                                          axis.xlab.padding = 0,
                                          xlab.key.padding = 0,
                                          key.sub.padding = 0,
                                          bottom.padding = 0),
                    layout.widths =  list(left.padding = 0,
                                          key.ylab.padding = 0,
                                          ylab.axis.padding = 0,
                                          axis.key.padding = 0,
                                          right.padding = 0),
                    axis.line = list(col = "transparent"))


p1 = levelplot(array(c(1:100), c(10,10)), colorkey=F, par.settings=theme.novpadding)
p2 = levelplot(array(c(1:100), c(9,9)), colorkey=F, ylab = NULL, par.settings=theme.novpadding)
p3 = levelplot(array(c(1:100), c(11,11)),  ylab=NULL, par.settings=theme.novpadding)

width = 0.33
height = 1

ph = list(5, "in")

print(p1, position = c(0, 0, width, height), panel.height=ph, more=T)
print(p2, position = c(width, 0, 2*width, height), panel.height=ph, more=T)
print(p3, position = c(2*width, 0, 3*width, height),panel.height=ph, more=F)

As you see, they are spread very wide. I want them as close as possible. I use theme.novpadding to set the margins to zero. Is the a way to say something like "distance between plots"?


Solution

  • A trick you could use is to tweak the position argument. In stead of not letting the areas overlap, you can do just that to make them close together. For example, you can change the position arguments to:

    ovr = 0.05
    print(p1, position = c(0, 0, width + ovr, height), panel.height=ph, more=T)
    print(p2, position = c(width - ovr, 0, 2*width+ovr, height), panel.height=ph, more=T)
    print(p3, position = c(2*width - ovr, 0, 3*width, height),panel.height=ph, more=F)
    

    You have to some tweaking with this, and I have not tested this code. But I think the general idea should work.