如何在格子图中为不同的组分配不同的符号?

3

假设我在图表上有3组每组3个点,我需要一份黑白版的图表,在该图表中,这3组的3个点都用不同的符号表示。 我应该如何指定panel.superpose函数?

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

3个回答

11

我倾向于使用你提供的博客文章中概述的相同的一般策略。

standard.theme() 开始,您可以进行设置调整,直到获得最适合自己需求的定制主题。一旦您得到一个喜欢的东西,只需通过 par.settings 参数将其插入到想要使用它的任何地方即可。

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)

输入图像描述


完美、富有教育性和百科全书式的回答,我从中学到的比我最初预期的还要多,因此我将其标记为已接受。不过,实际上我会使用Justin下面给出的一行代码解决方案,因为它只有两行。 - Attila Csordas
@AttilaCsordas 我没有解释是因为我不知道!Josh的回答很有启发性。当你在学习时,我也建议你研究一下ggplot2包。 - Justin

4

可能有更简单的方法(我对格子不是很熟悉),但是:

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')))

它正在工作,而且只有一行代码,谢谢!我选择另一个答案作为采纳的原因可以在那里的评论中找到。 - Attila Csordas
1
@AttilaCsordas -- 很高兴我们的回答有所帮助。如果您喜欢它们,除了接受其中一个答案外,还可以通过点击答案左侧的上三角形来给它们点赞。(我之所以提到这一点,是因为我猜您想知道。您也可以对网站上的任何其他问题和答案进行投票。) 另外,欢迎来到SO! - Josh O'Brien

2

这里有另一个解决方案,基于你在问题中提到的panel.superpose

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

产生以下输出: panel_superpose_example lattice使用主要变量(定义主显示),调节变量(定义不同面板中并置的子组)和分组变量(定义在面板内叠加的子组)。
公式Sepal.Length〜Petal.Length和分组语句groups = Species指定要绘制的数据并将其传递给控制绘图的panel。如果groups!= NULL,则panel.superpose将分配给pch列表的第i个元素传递到groups的第i个级别。
使用...panelpanel.superpose可以避免定义所有函数参数,并仅声明要自定义的那些。 pars.settings附加自定义设置到特定对象,而lattice.options则全局影响设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接