[R] How to shade part of a density plot

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun May 19 21:08:08 CEST 2002

Stuart Luppescu wrote:
> I'm trying to shade part of a density plot. The code I'm trying (using
> the Old Faithful data as an example) is something like this:
> # The Old Faithful geyser data
> data(faithful)
> d <- density(faithful$eruptions, bw = "sj")
> plot(d)
> polygon(d[d$x>4], col = "wheat")

Hmm. Have a look at
to see that d is a density object, which is a list of objects of
different lengths, so indexing as you did doesn't work ...

What about taking 
  index <- d$x > 4
and working with
  d$x[index] and d$y[index]
to generate the shading?

Uwe Ligges

> I expected that the part of the curve to the right of 4 on the x axis
> should be shaded, but nothing gets shaded at all. In fact, when I just
> try to print d[d$x>4] I get many lines of
> $"NA"
> I must be mistaken in my understanding of density objects. Could someone
> clear up my mistake? Thanks very much.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list