【问题标题】:How to set default par.settings theme in lattice如何在 lattice 中设置默认 par.settings 主题
【发布时间】:2026-01-22 11:50:01
【问题描述】:

例如:

mytheme <- trellis.par.get()
mytheme$strip.border$col = 'grey80'
mytheme$strip.background$col = 'grey80'
mytheme$axis.line$col = 'grey80'
mytheme$axis.text$col = 'grey60'
mytheme$plot.symbol$pch = 20
mytheme$plot.symbol$cex = .5
mytheme$plot.symbol$col = '#7AC5CD'
mytheme$plot.symbol$alpha = .8

l.sc <- update(scatter.lattice, par.settings = mytheme,
               layout = c(3, 2),
               between = list(x = 0.3, y = 0.3))
print(l.sc)

如何将par.settings 的默认值设置为mytheme


Lattice Multivariate Data Visualization with R一书中,第131页,作者给出了这个例子:

lattice.options(lattice.theme = standard.theme("pdf"))

但我不知道如何使其适应当前情况。我试过了:

lattice.options(lattice.theme = mytheme)

它不起作用。

【问题讨论】:

  • 有一个 latticeOptions 函数,可能拼写不准确。
  • 是的,它是 lattice.options,但手册页根本不清楚。
  • 通过查看lattice.options() 的输出,主要与布局有关。

标签: r graphics lattice


【解决方案1】:

主题可以持续设置(即,它们将影响所有后续情节,直到新的 设置已指定)使用trellis.par.set()函数:

 trellis.par.set(mytheme) ## mythme is a named list with settings parameters

mytheme 是一个列表(无需调用 trellis.par.get()):

mytheme <- list()
mytheme$strip.border$col = 'grey80'
mytheme$strip.background$col = 'grey80'
mytheme$axis.line$col = 'grey80'
mytheme$axis.text$col = 'grey60'
mytheme$plot.symbol$pch = 20
mytheme$plot.symbol$cex = .5
mytheme$plot.symbol$col = '#7AC5CD'
mytheme$plot.symbol$alpha = .8

要临时设置主题,请设置格的“par.settings”参数 绘图功能。例如latticeExtra 设置 ggplot2 风格的主题是这样的:

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

【讨论】:

  • 酷。我正在尝试将其添加到 .Rprofile 中,但 trellis.par.get() 函数每次调用时都会启动一个窗口,有什么方法可以抑制它?
  • trellis.par.set(mytheme) 也弹出一个窗口。
  • @qed 是的。如果尚未打开,这将打开默认设备。您可以使用第二种方法来避免这种情况。
  • 是否有任何优雅的方法可以使绘图参数持久化(即在 R 会话之间)?例如,我想永久更改默认格子颜色。在.Rprofile 中使用trellis.par.set 有效,但会导致启动时弹出窗口。