基于条件的叠加对图 [英] Superimpose pairs plot based on condition
问题描述
****很抱歉,但是情况可能比我所显示的要复杂一些.但是,您的两个脚本都可以工作,尽管由于点重叠,对于大型数据集,第一个脚本可能不太清楚!非常感谢Sacha!
****I am sorry, but the situation could be a little bit more complex than I have shown. However, both of your scripts work, although the first might be not so clear for large dataset due to point overlap! Thanks very much Sacha!
我想首先显示几个变量对,然后叠加同一数据集的选定数据.通常,可以使用 par(new = T)
这样实现叠加:
I would like to first show the pairs of several variables, and then superimpose the selected data of the same dataset. Usually, the superimpose can be achived using par(new=T)
like this:
h<-rnorm(nc) # this variable was used for conditioning
x<-rnorm(nc)
y<-rnorm(nc)
z<-rnorm(nc)
m<-cbind(x,y,z)
pairs(m)
par(new=T)
pairs(m[h>0.7,],col="red")
但是,看来 par()
设置不适用于这种用法.
However, it seems that the par()
setting does not work for such usage.
然后,也许格子库可以提供帮助. splom()
,但我不知道它是否真的有效以及如何起作用.有人可以给些建议吗?
Then, probably lattice library could help, ex. splom()
, but I do not know if it really works, and how. Could someone give some suggestions?
推荐答案
我假设 paris
必须是 pairs
? pairs
函数大约没有参数 add
,这可能也不是一件容易的事,因为该图有9个面板(简单地做 points 代码>将在最后一个面板中显示).但是使用
col
:
I assume paris
must be pairs
? The pairs
function doesn't have an add
argument or so, it would probably also not be that trivial since the plot has 9 panels (simply doing points
will plot in the last panel). but it is not that hard to do what you want in a single plot using col
:
nc <- 100
set.seed(1)
x<-rnorm(nc)
y<-rnorm(nc)
z<-rnorm(nc)
m<-cbind(x,y,z)
cols <- ifelse(x>0.7,"red","black")
pairs(m,col=cols)
您可以在 pairs
中完成的另一件事实际上是在每个面板中设置要执行的功能.默认情况下,这是 points
,但是您可以扩展它以包括一些条件:
Another thing you can do in pairs
is actually set the function you want to do in each panel. By default this is points
, but you can extend that to include some conditions:
nc <- 100
X<-rnorm(nc)
Y<-rnorm(nc)
Z<-rnorm(nc)
m<-cbind(X,Y,Z)
panelfun <- function(x,y,foo=X,...){
points(x[foo<0.7],y[foo<0.7],col="black",...)
points(x[foo>0.7],y[foo>0.7],col="red",...)
}
pairs(m,panel=panelfun)
这提供了与以前相同的图片(相差很多,因为我没有设置种子).简单地制作颜色矢量会更容易实现这一点,但您可以将面板功能设置为您想要的大小.
This gives the same picture as before (well different points because I didnt set a seed). Simply making the color vector would be easier to accomplish this, but you can make the panel function as big as you would like.
此外, ...
允许将其他参数传递给 points
函数:
Also, the ...
allow other arguments to be passed to the points
function:
pairs(m,panel=panelfun,pch=16)
这篇关于基于条件的叠加对图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!