Q:

R segment function with intervals

I am having trouble understanding the segments function in base graphics in the context of my specific problem.
x <- 0:1000
y <- c(0, 40000, 80000) 

Now I want to draw a plot with a line from 0 to 200 at y=0. Another line from 200 to 500 at y=40000 and the last line from 500 to 1000 at y=80000.
plot(x,y,type="n")
segments(0,0,200,40000,200,40000,500,8000,1000)
points(0,0,200,40000,200,40000,500,8000,1000)
points(0,0,200,40000,200,40000,500,8000,1000) 

I believe it is wrong to define the exact segments here. If x where 0:3 I would knwo what to do. But what do I have to do in the case of intervals?

A:

You need to supply vectors of coordinates x0 and y0 and x1 and y1 which are the x and y coordinates to draw from and to respectively. Consider the following working example:
x <- seq(0, 1000, length = 200)
y <- seq(0, 80000, length = 200)
plot(x,y,type="n")

from.x <- c(0, 200, 500)
to.x   <- c(200, 500, 1000)
to.y   <- from.y <- c(0, 40000, 80000) # from and to y coords the same

segments(x0 = from.x, y0 = from.y, x1 = to.x, y1 = to.y)

This produces the following plot

