[R] curves with shaded areas?

Henrik Bengtsson hb at maths.lth.se
Tue Jul 22 12:44:37 CEST 2003


polygon() together with par("usr") would be your friend here. As I do
not know if you want horisontal/vertical lines or any line, here is a
general example:

# Generate the data
x <- seq(from=1, to=3.5*pi, length=100)
y <- sin(x)

# Creates an empty plot of right size
plot(x,y, type="n")  

# Get plotting region
x0 <- par("usr")[1]; x1 <- par("usr")[2];
y0 <- par("usr")[3]; y1 <- par("usr")[4];

last <- length(y)

# Create the "upper" polygon
polygon(c(x0,x0,x,x1,x1), c(y1,y[1],y,y[last],y1), col="#ffcccc",
border=NA)
# Create the "lower" polygon
polygon(c(x0,x0,x,x1,x1), c(y0,y[1],y,y[last],y0), col="#ccccff",
border=NA)

# Plot the data on top
points(x,y)

As you see, as there are 4% margins by default, you have to specify what
you want to do at the margins. Depending on if you want regions defined
by abline(h)/abline(v) or by abline(a,b) you solution will be somewhat
different. 

Cheers

Henrik Bengtsson
Lund University

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Johannes Fuernkranz
> Sent: den 22 juli 2003 12:10
> To: r-help at stat.math.ethz.ch
> Subject: [R] curves with shaded areas?
> 
> 
> Hi,
> 
> I want to make a plot with abline where the area below or above the 
> curve is shaded. I can't find any documentation on that. Can anybody 
> help me with that?
> 
> thanks, Juffi
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
> 
> 
>




More information about the R-help mailing list