简体   繁体   English

如何使用lines()绘制线条而不事先调用R中的plot()

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

Given these data: 鉴于这些数据:

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

I'd like to draw lines for (x, y1) and (x, y2) in different colors on the same plotting frame. 我想在同一个绘图框架上以不同的颜色绘制(x,y1)和(x,y2)的线条。 This has the desired effect: 这具有预期的效果:

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

Is there a way to do that using lines for both lines? 有没有办法使用lines的线条? This creates just an empty plot: 这只会创建一个空图:

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

I guess plot is calling some function to get the plot started before drawing the first line; 我想plot是在绘制第一行之前调用一些函数来使绘图开始; it doesn't appear to be plot.new . 它似乎不是plot.new What is that function? 那是什么功能? Can I call it myself before calling lines ? 我可以把它自己之前调用lines

EDIT: I am working with R 3.0.2 on Ubuntu 14.04. 编辑:我在Ubuntu 14.04上使用R 3.0.2。

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

在此输入图像描述

I'm getting a nattering message about explaining my code but I do think Robert is smart enough to figure this out. 我收到了关于解释我的代码的喋喋不休的消息,但我确实认为罗伯特很聪明,可以解决这个问题。 Following the links on the ?plot.window page lets you see other low-level functions like ?xy.coords . 通过?plot.window页面上的链接,您可以看到其他低级函数,例如?xy.coords You can see the code for plot.default by typing its name at a console prompt. 您可以通过在控制台提示符下键入其名称来查看plot.default的代码。 In that code you see a new function defined but it is really just gathering some parameters before calling plot.window : 在该代码中,您看到定义了一个新函数,但它实际上只是在调用plot.window之前收集了一些参数:

localWindow <- function(..., col, bg, pch, cex, lty, lwd) plot.window(...)
# which gets called later in the code.

For plot solution, you need to create a plot with some data and mask those data with n option (I also employed ann=F to mask x and y labels): 对于plot解决方案,您需要使用一些数据创建plot并使用n选项屏蔽这些数据(我还使用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')

在此输入图像描述

In the plot() command, use type="n" to set up the plot invisibly. plot()命令中,使用type="n"无形地设置绘图。 Then add your line segments with successive lines() . 然后使用连续的lines()添加您的线段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM