A triangle has three points. Your x
and y
vectors only describe two points.
The polygon()
function can draw any triangle or higher-order polygon, but you have to fully define the polygon by passing all of its vertices in the x
and y
arguments.
We can solve the problem by supplementing the x
and y
vectors with an additional element that fills in the missing vertex when passing these vectors to the polygon()
function.
Since you've indicated you want to shade the area under the line, here's how I would do this:
- First I'll address the additional
y
coordinate, since it's easier. We simply need to use the minimum of the known y
coordinates, for which we can use min()
.
- For
x
, we need to copy the x
coordinate that corresponds to the vertex with the higher y-coordinate, for which we can use which.max()
on y
and then subscripting x
.
x <- c(0,1);
y <- c(1,2);
plot(x,y);
polygon(c(x,x[which.max(y)]),c(y,min(y)),col='red');
