【问题标题】:How to plot the value of abline in R?如何在 R 中绘制 abline 的值?
【发布时间】:2012-09-10 01:32:21
【问题描述】:

我用这段代码制作了这个情节:

plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16, 
     panel.last=abline(h=67,v=1.89, lty=1,lwd=3))

我的情节是这样的:

1.) 如何在简单的图中绘制 ablines 的值?

2.) 如何缩放我的绘图以使两条线都出现在中间?

【问题讨论】:

  • 你的意思是 abline 的值?有任何示例代码吗?
  • 不,我不知道你说的 abline 值是什么意思
  • 生成绘图的示例代码?一个你想要的例子(如果你需要,使用油漆!)?
  • 这是我的代码:plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16, panel.last=abline(h=67,v=1.89, lty=1,lwd=3)) 但我不知道将 abline 的值打印到轴上。
  • 应该是这样的:i.imgur.com/9Dd9d.png

标签: r plot


【解决方案1】:

改变绘图的比例,使线条位于中间,改变轴,即

x<-1:10
y<-1:10
plot(x,y)
abline(a=1,b=0,v=1)

changed to:

x<-1:10
y<-1:10
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)

“值”我假设你的意思是线在哪里切割 x 轴?像text 这样的东西?即:

text((0), min(y), "number", pos=2) 

如果您想要 x 轴上的标签,请尝试:

abline(a=1,b=0,v=1)
axis(1, at=1,labels=1)

为了防止标签之间的重叠,您可以删除零,即:

plot(x,y,xlim=c(-30,30),yaxt="n")
axis(2, at=c(1.77,5,10,15,20,25))

或在您绘制之前扩展边距并添加远离轴的标签

par(mar = c(6.5, 6.5, 6.5, 6.5))
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
axis(2, at=1.77,labels=1.77,mgp = c(10, 2, 0))

【讨论】:

  • 这是我的方法: > plot(x, y, col=rgb(0,100,0,50,maxColorValue=255),pch=16, panel.last=abline(h=0.78,v =1.77, lty=1,lwd=3), yaxt = "n") > > 轴(2, las = 1) > 轴(2, 0.78, 0.78, las = 1) > 轴(1, 1.77, 1.77)这导致以下图像: !Graphic 您可以看到 0.49 和 0 重叠...如何解决它们不重叠的问题?
【解决方案2】:

与@user1317221 提出的答案在精神上相似,这是我的建议

# generate some fake points
x <- rnorm(100)
y <- rnorm(100)

# positions of the lines
vert = 0.5
horiz = 1.3

要在图的中心显示线条,首先计算数据点与线条之间的水平和垂直距离,然后适当调整限制。

# compute the limits, in order for the lines to be centered
# REM we add a small fraction (here 10%) to leave some empty space, 
# available to plot the values inside the frame (useful for one the solutions, see below)
xlim = vert + c(-1.1, 1.1) * max(abs(x-vert))
ylim = horiz + c(-1.1, 1.1) * max(abs(y-horiz))

# do the main plotting
plot(x, y, xlim=xlim, ylim=ylim)
abline(h=horiz, v=vert)

现在,您可以在轴上绘制“线的值”(line参数允许您控制可能的重叠):

mtext(c(vert, horiz), side=c(1,2))

或者在绘图框内:

text(x=vert, y=ylim[1], labels=vert, adj=c(1.1,1), col='blue')
text(x=xlim[1], y=horiz, labels=horiz, adj=c(0.9,-0.1), col='blue')

HTH

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 2021-07-09
    • 2019-11-14
    • 2021-04-04
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多