根据条件叠加对图

发布于 2024-10-18 09:39:37 字数 490 浏览 8 评论 0原文

**编辑:**很抱歉,但情况可能比我所展示的要复杂一些。但是,您的两个脚本都可以工作,尽管由于点重叠,第一个脚本对于大型数据集可能不太清楚!非常感谢萨莎!

我想首先显示几个变量对,然后叠加同一数据集的选定数据。通常,可以使用 par(new=T) 来实现叠加,如下所示:

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() 设置不适用于这种用法。

那么,格子库可能会有所帮助,例如。 splom(),但我不知道它是否真的有效,以及如何工作。有人可以提供一些建议吗?

**Edit:**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!

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

However, it seems that the par() setting does not work for such usage.

Then, probably lattice library could help, ex. splom(), but I do not know if it really works, and how. Could someone give some suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2024-10-25 09:39:37

我假设paris一定是pairspairs 函数没有 add 参数,所以它可能也不是那么简单,因为该图有 9 个面板(只需做 points 将在最后一个面板中绘制)。但使用 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中做的另一件事实际上是在每个面板中设置您想要执行的功能。默认情况下,这是点,但您可以扩展它以包含一些条件:

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)

这给出了与之前相同的图片(不同的点,因为我没有设置种子)。简单地制作颜色矢量会更容易实现这一点,但您可以将面板功能设置为您想要的大小。

此外,... 允许将其他参数传递给 points 函数:

pairs(m,panel=panelfun,pch=16)

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)

enter image description here

Edit:

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.

Also, the ... allow other arguments to be passed to the points function:

pairs(m,panel=panelfun,pch=16)
永不分离 2024-10-25 09:39:37

lattice::splom 工作正常。颜色索引需要增加 1,因为 R 索引是从 1 开始而不是从 0 开始,并且逻辑向量被强制为 0 和 1。

library(lttice)
nc=100; h<-rnorm(nc)  
x<-rnorm(nc)
y<-rnorm(nc)
z<-rnorm(nc)
m<-cbind(x,y,z)
splom(m, col=c("blue", "red")[1+(h>0.7)])

在此输入图像描述

lattice::splom works fine. The color indexing needs to be boosted by 1 since R indexing is 1 based rather than zero-based and the logical vectors get coerced as 0 and 1.

library(lttice)
nc=100; h<-rnorm(nc)  
x<-rnorm(nc)
y<-rnorm(nc)
z<-rnorm(nc)
m<-cbind(x,y,z)
splom(m, col=c("blue", "red")[1+(h>0.7)])

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文