绘制垂直线以满足水平线

时间:2017-10-26 10:40:33

标签: r

我想在某个/任意点绘制一条垂直线以满足某条水平线:

xyplot(Amplification ~ Voltage | Serial_number,
       data = APD[APD$Serial_number==912009897,],
       panel = function(x, ...){
                                panel.xyplot(x, ...);
                                panel.abline(h = 150)
                                panel.abline(v = 350)},
       ylim = c(100,200),
       grid = TRUE 
)

完美地,两条线将在十字路口结束,以使其更具描述性。 我怎样才能做到这一点?谢谢!

Plot

1 个答案:

答案 0 :(得分:2)

所以,如果你想要x = 350和y = 150,作为行的结尾,我将通过命令'points'并制作两行,只是猜测两行的一些安全起点:

points(c(350,350),c(100, 150), type='l') # the vertical one

points(c(100, 350),c(150, 150), type='l') # the horizontal one

以更通用的方式,首先定义您的点,在绘图之后,您获得轴尺寸并使用实际值绘制线条:

point <- c(350,150) # point of line crossing
plot(.... ) # your plot
mrs <- par('usr') # axis limits
## Now the lines
points(rep(point[1],2),c(mrs[3], point[2]), type='l')
points(c(mrs[1], point[1]),rep(point[2],2), type='l')

应该做的工作。

末尾的行会在两点之间形成一条直线,就像连接(x1,y1)到(x2,y2)。因此,使用命令par('usr'),您将获得两个轴的起点和终点,在长度为4的向量中, 像(min(x),max(x),min(y),max(y))这样的东西。所以用点数你只需要(c(x1,x2),c(y1,y2))。对于垂直方向,那么两个点的x坐标是相同的,这就是为什么有rep(point [1],2),而在y中,它是y的值和y轴的起点(即这就是为什么它是msr [3],是向量的第三个数字,等等......

相关问题