Search code examples
rlattice

How can I assign different symbols to different groups in a lattice plot?


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


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