[R] Histogram and Density on the the same graph

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Nov 30 12:27:53 CET 2009


On 30-Nov-09 11:09:12, Trafim wrote:
> Dear all,
> I cannot find a function which would allow drawing hist and density
> on the same graph.
> 
> x <- seq(1,40,1)
> y <- 2*x+1+5*rnorm(length(x))
> 
> hist(y,freq = FALSE)
> plot(density(y))
> 
> thanks a lot for the help

plot() initiates a completely new plot, and therefore erases what
was there before. Use lines(), or points(), instead:

  x <- seq(1,40,1)
  y <- 2*x+1+5*rnorm(length(x))
  
  hist(y,freq = FALSE)
  lines(density(y))

lines() *adds* extra elements (line segments) to an *existing* plot.

See ?plot abd ?lines (and also ?points) for more explanation.
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 30-Nov-09                                       Time: 11:27:49
------------------------------ XFMail ------------------------------




More information about the R-help mailing list