Skip to content

Lattice Notes

June 6, 2013

My Personal Lattice Notes


Add curves to each panel & Mixed keys

def.col = trellis.par.get()$superpose.symbol$col  # Get default colors.

mykey = list(space = “top”, text = list(c(“Observations:”,”Fixed effect fit:”, “Mixed effect fit:”)), columns = 3,

    lines = list(col = c(def.col[1],”grey60″,”red”), type = c(“p”,”l”,”l”),lty = c(1,2,1), pch = c(1,1,1))) #Point & line in key

xyplot(surface ~ days | patient, data = wd.cur, layout = c(13,1),
main = “Pressure Ulcer Wound Surface Area: Mixed Effect Model vs Fixed Effect Model”,
sub = “surface ~ exp(init + days + (days | patient))”,
xlab = “Time post initial treatment (days)”, ylab = “Surface Area”,
panel = function(x,y,…){
        panel.xyplot(x,y,…) # Prints the real observations
# Define x2, and y2. Omitted here
        panel.grid()
        panel.lines(x2,exp(y2),col = “red”) # These two lines will be added to the graph
        panel.lines(x2,exp(y2.fix),col = “grey60”, lty = 2)
    },
key = mykey,
)


Easy way to change plotting preferences of plots and keys together.

with(wd.cur,
stripplot(surface ~ patient, subset(wd.cur, days %in% c(0,1)), group = obs[,drop = TRUE],
par.settings = simpleTheme(col=c(rep(“red”,3),rep(“blue”,5)),
        pch=c(1:3,1:5), cex=1.2, lwd=1),
auto.key = list(space = “right”),
main = “Initial measurements: Surface”,
xlab = “patients”
)
)

Get Lattice default parameters.

ptcol = trellis.par.get(“superpose.symbol”)$col


Multiple Plots on one page
Method 1

library(lattice)
m <- matrix(c(0.1,0.2,0.3,0.4), 2, 2)
col.l <- colorRampPalette(c(‘blue’, ‘green’))(30)

plot.new()
par(mfrow=c(2,2), oma=c(2,0,2,0))
print(levelplot(m, col.regions=col.l, main=”L1″), split=c(1, 1, 2, 2)) # Check ?print.trellis for “split” argument.
print(levelplot(m, col.regions=col.l, main=”L2″), split=c(1, 2, 2, 2), newpage=FALSE)
print(levelplot(m, col.regions=col.l, main=”L3″), split=c(2, 1, 2, 2), newpage=FALSE)
print(levelplot(m, col.regions=col.l, main=”L4″), split=c(2, 2, 2, 2), newpage=FALSE)
title(“Centered Overall Title”, outer=TRUE)
mtext(side=1, “Centered Subtitle”, outer=TRUE)


Multiple Plots on one page
Method 2

–source: http://stackoverflow.com/questions/13327326/r-image-function-in-r

The Lattice Package often (but not always) ignores the par command, so i just avoid using it when plotting w/ Lattice.

To place multiple lattice plots on a single page:

  • create (but don’t plot) the lattice/trellis plot objects, then
  • call print once for each plot
  • for each print call, pass in arguments for (i) the plot; (ii) more, set to TRUE, and which is only passed in for the initial call to print, and (iii) pos, which gives the position of each plot on the page specified as x-y coordinate pairs for the plot’s lower left-hand corner and upper right-hand corner, respectively–ie, a vector with four numbers.

much easier to show than to tell:

data(AirPassengers)     # a dataset supplied with base R
AP = AirPassengers      # re-bind to save some typing

# split the AP data set into two pieces 
# so that we have unique data for each of the two plots
w1 = window(AP, start=c(1949, 1), end=c(1952, 1))
w2 = window(AP, start=c(1952, 1), end=c(1960, 12))

px1 = xyplot(w1)
px2 = xyplot(w2)

# arrange the two plots vertically
print(px1, position=c(0, .6, 1, 1), more=TRUE)
print(px2, position=c(0, 0, 1, .4))

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: