[R] Plot and area below a line graph

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Nov 25 12:29:41 CET 2009


On Wed, 25 Nov 2009, Jose Narillos de Santos wrote:

> Imagine I have a variable X
>
> I use plot (X, type="l").
>
> I want to "show" with colour the area between "the line of X" and the Zero
> Axis.
>
> So that if X have negative values the colored area will be below zero.
>
> I?m trying to make this with polygons() but I?m not able....
>
> Can you guide me to do it?

Maybe something like this?

## artificial random walk
set.seed(123)
x <- cumsum(rnorm(100))

## plot
plot(x, type = "l")

## add shaded polygon
polygon(c(1, 1:100, 100), c(0, x, 0), col = "lightgray")

## zero axis
abline(h = 0, lwd = 2, col = "blue")

hth,
Z

>
> The other question is how can I show "the zero axis" in a plot?
>
> 	[[alternative HTML version deleted]]
>
>




More information about the R-help mailing list