[R] background color in scatterplots

Mike Prager mike.prager at noaa.gov
Fri Mar 28 22:59:21 CET 2008


"Georg Ehret" <georgehret at gmail.com> wrote:

> Dear R community,    I have a scatterplot with multiple vertical ablines. I
> wish to color each interval between two ablines in a different color...
> Could you please indicate me how to do this efficiently?
> 
> Thank you!
> Georg.
> ********************

Dear Georg,

Try this example:

# Demonstrate scatterplot w/ different color bands
# M.H.P. - March 2008
graphics.off()
# Generate dummy data & make a blank plot:
a <- runif(100)
b <- 2 * runif(100)
plot(a, b, type = "n", main = "Demonstration")
# Set the values of the x-axis references:
vrefs <- c(0.2, 0.4, 0.6)
# Get coordinates of plot
pdims <- par()$usr
# Concatenate them to the vector of x_axis references:
vrefs2 <- c(pdims[1], vrefs, pdims[2])
# Generate some weak colors:
cols <- rainbow(n = length(vrefs2), s = 0.15)
# Add the colors to the plot:
for (i in 2:length(vrefs2)){
    polygon(c(vrefs2[(i-1):i], vrefs2[i:(i-1)]),
        c(rep(pdims[3],2), rep(pdims[4],2)), col = cols[i],
        border = NA)
    }
# Draw the reference lines, points, and box:
abline(v = vrefs)
points(a, b, lwd = 1.5)
box()

-- 
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.



More information about the R-help mailing list