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 ? ##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))) Best, thanks for your time.
Not reproducible (sample data missing). http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On March 16, 2015 5:34:18 PM PDT, varin sacha <varinsacha at yahoo.fr> 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 ? > >##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))) > > >Best, thanks for your time. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
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
Hi Jeff, Here is I hope a reproducible code. My R code gives something but not what I am expecting. Indeed, I am expecting a gauss curve and I get a straight line. I have followed the advices of Rolf as well but I still don't get a gauss curve... Thanks for your time 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))) ----- Mail original ----- De : Jeff Newmiller <jdnewmil at dcn.davis.CA.us> ? : 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 2h30 Objet : Re: [R] Add the Gauss curve on histogram Not reproducible (sample data missing). http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity.>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 ? > >##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))) > > >Best, thanks for your time. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
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
Sorry for that Rolf, Yes, it perfectly works. Many thanks. Cheers, ----- Mail original ----- De : Rolf Turner <r.turner at auckland.ac.nz> ? : varin sacha <varinsacha at yahoo.fr> Cc : R help <R-help at r-project.org> Envoy? le : Mardi 17 mars 2015 12h40 Objet : Re: [R] Add the Gauss curve on histogram 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