[R] Add the Gauss curve on histogram
Rolf Turner
r.turner at auckland.ac.nz
Tue Mar 17 12:40:00 CET 2015
Please keep your replies on-list. I am not your private consultant.
Not, at least, until you start paying me!!!
Again your example is not reproducible. WTF makes you think I have
access to the file
"/Users/Caro/Desktop/Mesures d'association using R/test.txt" ???
I have no idea what your problem is. The following simulated example
works just fine:
# ------------------------------------------------------------
set.seed(59631)
simdat <- rnorm(300,0,2)
hist(simdat, prob=TRUE, col="blue", border="white",
xlab="x", ylab="y", main="Simulated data")
x <- seq(min(simdat),max(simdat),length=100)
muhat <- mean(simdat)
sigmahat <- sd(simdat)
y <- dnorm(x,muhat,sigmahat)
lines(x,y)
# ------------------------------------------------------------
Imitate it.
cheers,
Rolf Turner
On 18/03/15 00:15, varin sacha wrote:
> Hi Rolf,
> Many thanks for all your advices.
>
> I get a straight line but not a gauss curve as i am expecting ! I have tried as well another Rcode to get a gauss curve. I don't get any error message but my other Rcode doesn't give anything. Strange !!
>
>
> Dataset <- read.table("/Users/Caro/Desktop/Mesures d'association using R/test.txt", header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
> newdata=na.omit(Dataset)
>
> h <-hist(newdata$math.test, prob=TRUE, col="blue", border="white", xlab="Note test math", ylab="Densité", main="Test math")
>
> x <- seq(from = min(newdata$math.test, na.rm=TRUE), to = max(newdata$math.test, na.rm=TRUE, length=100))
> lines(x, dnorm(x, mean(newdata$math.test, na.rm = TRUE), sd(newdata$math.test, na.rm = TRUE)))
> OR
>
>
> x <- seq(from = min(newdata$math.test, na.rm=TRUE), to = max(newdata$math.test, na.rm=TRUE, length=100))
> lines(x, dnorm(x, mean(newdata$math.test, na.rm = TRUE), sd(newdata$math.test, na.rm = TRUE)))
>
>
>
> ----- Mail original -----
> De : Rolf Turner <r.turner at auckland.ac.nz>
> À : varin sacha <varinsacha at yahoo.fr>; "r-help at r-project.org" <r-help at r-project.org>
> Cc :
> Envoyé le : Mardi 17 mars 2015 5h02
> Objet : Re: [R] Add the Gauss curve on histogram
>
> 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