[R] Add the Gauss curve on histogram

Rolf Turner r.turner at auckland.ac.nz
Tue Mar 17 05:02:52 CET 2015


On 17/03/15 13:34, varin sacha wrote:
> Dear R-Experts,
>
> I can easily plot an histogram and its density curve. But when I try
> to add the Gauss curve on the histogram with the following Rcode here
> below, I don't get it. What is wrong ? Or what is missing in my code
> ?

Coherency and appropriate checking of syntax.

* Your call to lines() is *inside* the call to seq(), whence it gets 
ignored.

* Your call to seq() should specify some reasonable values for "length" 
or "by", otherwise "x" has far too few values and you get a very jagged 
curve.

* You should not jumble a whole lot of commands together.  This leads to 
the sort of mistakes that you made.  Do one thing at a time.  Keep your 
commands brief.  You will then have far less tendency to make mistakes 
and a far easier time in detecting where mistakes have been made if you 
do make them.

* There is no such thing as "the" Gauss curve.  There is an uncountably 
infinite number of Gauss curves.

* As Jeff Newmiller told you, *do* provide a *reproducible* example when 
you ask a question.  (Nobody but *you* has the data set "newdata".)
Neither R nor the members of this list can mind-read.

cheers,

Rolf Turner

>
> ##Plot an histogram h=hist(newdata$math.test, prob=TRUE, col="blue",
> border="white", xlab="Note test math", ylab="Densité", main="Test
> math")
>
> ##Add the density curve on the histogram
> lines(density(newdata$math.test,na.rm=TRUE),lwd=2,col="orange")
>
> ##Add the Gauss curve on the histogram x <- seq(from =
> min(newdata$math.test, na.rm=TRUE), to = max(newdata$math.test,
> na.rm=TRUE), lines(x, dnorm(x, mean(newdata$math.test, na.rm = TRUE),
> sd(newdata$math.test, na.rm = TRUE)))
-- 
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619



More information about the R-help mailing list