如何在不预先调用 R 中的 plot() 的情况下使用 lines() 绘制线条

How to draw lines with lines() without preceding call to plot() in R

鉴于这些数据:

> x <- c(1, 2)
> y1 <- c(4, 3)
> y2 <- c(3, 6)

我想在同一个绘图框架上用不同颜色为 (x, y1) 和 (x, y2) 画线。这具有预期的效果:

> plot (x, y1, type='l', col='red')
> lines (x, y2, col='green')

有没有办法对两行都使用 lines 来做到这一点?这只会创建一个空图:

> plot.new ()
> lines (x, y1, col='red')
> lines (x, y2, col='green')

我猜 plot 在绘制第一行之前调用了一些函数来开始绘图;它似乎不是 plot.new。那是什么功能?我可以在调用之前自己调用吗lines?

编辑:我在 Ubuntu 14.04.

上使用 R 3.0.2

对于 plot 解决方案,您需要使用一些数据创建一个 plot 并使用 n 选项 mask 这些数据(我也使用 ann=F 来掩盖 xy 标签):

plot(x, y1, type='n', ann=F, xlim=c(1,2),ylim=c(1,6))
lines (x, y2, col='green')
lines (x, y1, col='red')

plot()命令中,使用type="n"设置剧情不可见。然后添加具有连续 lines() 的线段。

 x <- c(1, 2)
 y1 <- c(4, 3)
 y2 <- c(3, 6)
 plot.new(); plot.window(xlim=c(1,2),ylim=c(1,6) )
 lines (x, y1, col='red')
 lines (x, y2, col='green')

我收到一条关于解释我的代码的喋喋不休的消息,但我确实认为罗伯特足够聪明,可以解决这个问题。通过 ?plot.window 页面上的链接,您可以查看其他低级函数,例如 ?xy.coords。您可以通过在控制台提示符下键入其名称来查看 plot.default 的代码。在该代码中,您看到定义了一个新函数,但它实际上只是在调用 plot.window:

之前收集一些参数
localWindow <- function(..., col, bg, pch, cex, lty, lwd) plot.window(...)
# which gets called later in the code.