Question

Say I have 3 groups with 3 dots each on the plot and I need a black & white version where the 3 dots of the 3 groups are all displayed with different symbols. How should I specify the panel.superpose function?

http://www.r-bloggers.com/working-with-themes-in-lattice-graphics/ http://stat.ethz.ch/R-manual/R-devel/library/lattice/html/panel.superpose.html

Was it helpful?

Solution

I tend to use the same general strategy laid out in the blog post you linked to.

Starting with standard.theme(), you can make tweaks to settings until you have a customized theme that best fits your own needs. Once you've got something you like, you can just plug it in via the par.settings argument whenever you want to use it.

library(lattice)
# Start work on your own black-and-white theme
myTheme <- standard.theme(col = FALSE)
myTheme$superpose.symbol$pch <-1:7

# These are the kinds of commands you can use to explore the list of available
# settings as well as their current settings.
names(myTheme)
myTheme$superpose.symbol

# Compare the results of your own theme to those produce by lattice's
# default settings.
library(gridExtra)
p1 <- xyplot(Sepal.Length ~ Petal.Length, group= Species, data = iris,
             main = "lattice's default theme")
p2 <- xyplot(Sepal.Length ~ Petal.Length, group= Species, data = iris,
             par.settings = myTheme,
             main = "My customized theme")
grid.arrange(p1, p2, ncol=2)

enter image description here

OTHER TIPS

There may be a simpler way (I'm not terribly familiar with lattice) but:

library(lattice)
df <- data.frame(x = rnorm(9), y = rnorm(9), z= letters[1:3])

xyplot(x~y,data=df,groups=z,
       par.settings=list(superpose.symbol=list(pch=1:3,
                                               col='black')))

Here is another solution, based on panel.superpose, which you refer to in your question:

library(lattice)
xyplot(Sepal.Length ~ Petal.Length, groups = Species, data = iris,
panel = function(x,y,...){
    panel.superpose(x,y,..., pch=list("A","O","X"))
})

produces the following output: panel_superpose_example

lattice uses primary variables (define the primary display), conditioning variables (define subgroups juxtaposed in different panels), and grouping variables (define subgroups superposed within panels).

The formula Sepal.Length ~ Petal.Length and the grouping statement groups = Species specify the data to be plotted and pass it to panel which controls plotting. If groups != NULL panel.superpose passes the i-th element of the list assigned to pch to the i-th level of groups.

Using ... for panel and panel.superpose one can avoid defining all function arguments and state only those which are to be customized.

pars.settings attaches custom settings to a specific object, unlike lattice.options which affects settings globally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top