假设我有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个组,每个都有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
我倾向于使用与链接到的博客文章中列出的相同的一般策略。
从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)
可能有一个更简单的方法(我不是非常熟悉格子)但:
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')))
它正在工作,它是一个单线,谢谢!我选择接受的其他答案的原因可以在评论中找到。 – 2012-02-17 19:59:20
@AtilaCsordas - 很高兴我们的答案帮助。如果你喜欢他们,你也可以(除了接受他们之外)给他们一个upvote,通过点击答案左边的向上三角形。 (我只是提到它,因为我猜你会想知道,你也可以在网站上对其他问题和答案进行投票。)另外,欢迎来到SO! – 2012-02-17 20:10:37
这里是另一种解决方案,基于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"))
})
lattice
使用主要变量 (定义主显示器),调节变量(定义并列在不同面板中的子组)和分组变量(定义面板内重叠的子组)。
公式Sepal.Length〜Petal.Length和分组语句基团=物种指定要绘制的数据,并把它传递给panel
其控制绘图。如果groups!= NULL panel.superpose
将分配给pch
的列表的第i个元素传递给groups
的第i级。
对于panel
和panel.superpose
使用...
可以避免定义所有函数参数,并且只声明那些要定制的函数参数。
pars.settings
将自定义设置附加到特定对象,而不像lattice.options
会影响全局设置。
完美的,教育性的,百科全书式的答案我从它学到的东西比我原先预想的要多,所以我将其标记为可接受的,只是我实际上会使用由Justin提供的单行解决方案,因为它是2行。 – 2012-02-17 19:58:15
@AtilaCsordas我没有解释,因为我不知道!乔希的回答是提供信息。在你学习的同时,我还会研究'ggplot2'包。 – Justin 2012-02-17 20:15:58