Reputation: 17585
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. 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? 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; it doesn't appear to be plot.new
. What is that function? Can I call it myself before calling lines
?
EDIT: I am working with R 3.0.2 on Ubuntu 14.04.
Upvotes: 3
Views: 3491
Reputation: 31181
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(x, y1, type='n', ann=F, xlim=c(1,2),ylim=c(1,6))
lines (x, y2, col='green')
lines (x, y1, col='red')
Upvotes: 3
Reputation: 263451
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
. You can see the code for plot.default
by typing its name at a console prompt. In that code you see a new function defined but it is really just gathering some parameters before calling plot.window
:
localWindow <- function(..., col, bg, pch, cex, lty, lwd) plot.window(...)
# which gets called later in the code.
Upvotes: 6
Reputation: 476
In the plot()
command, use type="n"
to set up the plot invisibly. Then add your line segments with successive lines()
.
Upvotes: 2