TWiSt_kid
TWiSt_kid

Reputation: 107

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"?

Upvotes: 0

Views: 1539

Answers (2)

DorinPopescu
DorinPopescu

Reputation: 725

This problem is easy to solve if you structure your data into a nice data.frame. Lattice works best on data.frames. See the example bellow:

g.xy <- expand.grid(1:10, 1:10)
my.data <- data.frame(x=g.xy$Var1, y = g.xy$Var2, value1=1:100, value2 = 1:100, value3=1:100, value4=1:100)
levelplot(value1+value2+value3+value4~x+y, data=my.data, scales=list(x="free", y="free")) # free
levelplot(value1+value2+value3+value4~x+y, data=my.data, scales=list(x="same", y="same")) # same range, so panels are "touching"

You can control for the plots to appear very close together by setting the scale argument.

Upvotes: 1

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

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.

Upvotes: 0

Related Questions