如何在R中绘制abline的值?

时间:2012-05-11 11:30:47

标签: r plot

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

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

我的情节如下: Plot

1。)如何在简单的情节中绘制下划线的值?

2.。)如何缩放我的情节以使两条线条出现在中间?

2 个答案:

答案 0 :(得分:6)

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

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)

by“value”我假设你的意思是线切割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))

答案 1 :(得分:0)

与@ 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