[英]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
来掩盖x
和y
标签):
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.