Dear list,

I'm writing a long document (thesis) and as much as I would like to use only ggplot2 for the graphics, some features are still a bit undocumented so I often end up choosing either ggplot2, lattice, or base plots (which i know better) depending on the particular graph to produce. With the default settings, this does not make for a consistent look I would like to see throughout the document. I really like most default settings in ggplot's theme (grid, labels spacing, background, ...), and I would like to produce a similar theme for use with lattice. I've made such an attempt in base graphics – see beow, it's just silly – but I think i could get away with using only lattice and ggplot2.

I don't know lattice well (just started using it, really), so I'm sort of lost in the jungle of parameters. Can anyone help me set up a lattice theme for the following example?

Best wishes,

baptiste

library(ggplot2)
library(lattice)

# example data
x <- seq(0, 10, len = 100)
y1 <- jitter(sin(x), 1000)
y2 <- 0.5*jitter(cos(x), 1000)

# custom colors
greyDark <- grey(0.5)
greyLight <- grey(0.9)
myColors <- c( "#E41A1C", "#377EB8")
palette(myColors)

# here is the ggplot2 version
df <- melt(data.frame(x=x, one=y1, two=y2), id="x")
p <- qplot(x,value, data=df,colour=variable, linetype=variable, main="ggplot2 (almost) defaults")
p <- p + scale_colour_manual(values = myColors)
print(p)

# lattice version
 strip.background <- trellis.par.get("strip.background")
background <- trellis.par.get("background")
 plot.symbol <- trellis.par.get("plot.symbol")
 trellis.par.set(strip.background = list(col = grey(7:1/8)))
 trellis.par.set(plot.symbol = list(col = myColors, pch=16))
trellis.par.set(background = list(col = greyLight)) # this does not clip to the plot region ...

p2 <- xyplot(value ~ x, data=df)
print(p2)

# example using base graphics
old.par <- par()
par(cex=1, bty = "n",fg = greyDark, col.lab = "black",
xpd = FALSE, mar = old.par$mar + c(-2,-1,-1,3), mgp=c(1.8, 0.5, 0), col="black")

plot(x, y1, new=TRUE, t="n") # plots nothing, needed to find the dimensions

lims <- par("usr")
subGrid1 <- axTicks(1) + mean(diff(axTicks(1)))/2 # position of the grid sub-divisions
subGrid2 <- axTicks(2) + mean(diff(axTicks(2)))/2

plot(x, y1, col=1, xlab = "x", ylab = "value", xaxt = "n", yaxt = "n", pch=16, cex=0.8,
        panel.first = {
rect(lims[1], lims[3], lims[2], lims[4],bord = NA, col = greyLight); # grey background segments(axTicks(1),lims[3], axTicks(1), lims[4], col = "white" , lwd=1.2); # main grid segments(lims[2], axTicks(2),lims[3], axTicks(2), col = "white" , lwd=1.2); segments(subGrid1,lims[3], subGrid1, lims[4], col = "white" , lwd=0.5); # secondary grid segments(lims[2], subGrid2,lims[3], subGrid2, col = "white" , lwd=0.5); axis(1, lty = "solid", lwd = 1, col = greyDark, col.axis = greyDark, tcl=-0.4, cex.axis = 0.8); # axis axis(2, lty = "solid", lwd = 1, col = greyDark, col.axis = greyDark, las=1, tcl=-0.4, cex.axis = 0.8);})

par(bty="o")
box(col="white", lwd=3) # draws in white over the axes

points(x, y2, col = 2, cex=0.8, pch=16) # some more plotting as usual

par(xpd = TRUE) # legend is outside
legend(1.1*max(x), mean(y1), pch=16, col=1:2, c("one", "two"), bty="n", title="variable")
title(main = "ggplot theme with base graphics")

par(old.par)

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to