Search code examples
rplotlattice

How we use ggplot2like if there is already a user defined par.settings function


The example from the ggplot2like help returns ggplot2like() to par.settings, as follows:

library(lattice)
library(latticeExtra)

xyplot(exp(1:10) ~ 1:10, type = "b", 
  par.settings = ggplot2like(), axis = axis.grid)

But how shall we use ggplot2like and axis = axis.grid if we have already a user-defined function for par.settings, as follows:

mysettings <- list(par.main.text = list(font = 1, cex = 1))

xyplot(exp(1:10) ~ 1:10, type = "b", main = "Title",
  par.settings = mysettings)

Solution

  • Function modifyList can be used to modify ggplot2like theme:

    mysettings <- list(par.main.text = list(font = 1, cex = 1))
    
    xyplot(exp(1:10) ~ 1:10, type = "b", main = "Title",
      par.settings = modifyList(ggplot2like(), mysettings), axis = axis.grid)
    

    Works because a lattice theme is list of parameters. Function modifyList use second list to modify (add or replace) elements in first list.

    modified plot

    As an alternative you could setup your settings at first:

    mysettings <- modifyList(
        ggplot2like()
        ,list(par.main.text = list(font = 1, cex = 1))
    )
    
    xyplot(exp(1:10) ~ 1:10, type = "b", main = "Title",
      par.settings = mysettings, axis = axis.grid)