将x = y行添加到散点图 [英] add x=y line to scatterplot

查看:274
本文介绍了将x = y行添加到散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用汽车包装中的散点图功能来生成散点图. 我希望能够在绘图中生成应该为x = y的参考线. 我尝试使用abline,它确实添加了一行,但它不是x = y行.有人可以帮忙吗?

I am using the scatterplot function from the car package to generate a scatterplot. I want to be able to generate a reference line in the plot that should be x=y. I tried using abline and it does add a line but it is not the x=y line. Can someone help?

我的代码如下:

scatterplot(phenos$P1~pheno$P0, data=pheno,spread=FALSE,ylab="6 month timepoint", xlab="Baseline Timepoint", jitter=list(x=1, y=1))
abline(0,1)

谢谢.

推荐答案

这实际上非常困难/棘手,因为scatterplot()在内部使用layout,这使得很难控制图形驱动程序当前正在使用的子图. . (更新:这比我想象的要难-设置par("mfg")一定是偶然地或多或少地起作用.)

This is actually fairly difficult/hackish, because scatterplot() internally uses layout, which makes it hard to control the subplot currently being used by the graphics driver. (Update: it's harder than I thought -- setting par("mfg") must have been working more or less by accident.)

组成数据(更新:使用均值x和y不等于0且彼此不相等的数据,因为它更清楚地说明了单纯使用abline()的困难)

Make up data (update: using data with mean x and y not equal zero and not equal to each other, as it illustrate the difficulties of naively using abline() more clearly)

set.seed(1)
d <- data.frame(x=rnorm(10,mean=10),y=rnorm(10,mean=12))
library(car)

尝试我的旧策略(该策略实际上不起作用,或者只能发挥不可预测的作用):

Try my old strategy (which doesn't actually work, or works only unpredictably):

scatterplot(y~x,data=d,reset.par=FALSE)
k <- 1              
for (i in 1:2) {
   for (j in 1:2) {
      par(mfg=c(i,j,2,2))
        abline(0,1,lwd=3,col=k)
        k <- k+1
  }

}

根据我的操作方式,我会收到警告和错误,也可能会得到虚假的答案.我是否在函数内进行scatterplot()调用似乎很重要... ??

Depending on how I do this, I either get warnings and errors or bogus answers. It seems to matter whether I do the scatterplot() call inside a function ... ??

第二次尝试,更加保守:从头开始重新构建布局.

Second try, more conservatively: reconstruct layout from scratch.

 scatterplot(y~x,data=d)
 uu <- par("usr")
 ## mimic layout frolm car:::scatterplot.default.  Would be different if we were drawing only one
 ## of x-boxes or y-boxes
 layout(matrix(c(1, 0, 3, 2), 2, 2), widths = c(5, 95), 
        heights = c(95, 5))
 oldmar <- par(mar=rep(0,4))  ## zero out margins so we can plot in sub-boxes without errors
 ## now skip through the first two sub-plots
 par(new=TRUE); plot.new(); par(new=TRUE); plot.new()
 par(oldmar)  ## reset margins
 ## blank plot with user limits set and 'interior' axis calculations
 plot(0:1,0:1,xlab="",ylab="",xlim=uu[1:2],ylim=uu[3:4],xaxs="i",yaxs="i")
 ## add annotation
 abline(a=0,b=1,col=4,lwd=3)              

鉴于此解决方案的工作量和脆弱性,实际上最好是破解scatterplot,以便有选择地允许另外指定abline(),或要求维护者提供该功能...

Given the amount of effort and fragility of this solution, it might actually be best to hack scatterplot to optionally allow abline() to be specified additionally, or to ask the maintainer for that capability ...

这篇关于将x = y行添加到散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆