Lothar Botelho-Machado
2006-Sep-25 17:56 UTC
[R] Can't mix high level and low level plot functions.
Hey R-Comunity, I'd like to print out an histogram of some experimental data and add a smooth curve of a normal distribution with an ideally generated population having the same mean and standard deviation like the experimental data. The experimental data is set as vector x and its name is set to group.name. I paint the histogram as follows: hist(data, freq=FALSE, col="lightgrey", ylab="Density", xlab=group.name) First I did the normal distribution curve this way: lines(x, dnorm(x, mean=mean(x), sd=sd(x)), type="l", lwd=2) This curve just uses as many values as there are in x. When using small amounts of sample populations the curve looks really shaky. I tried this one using a high level plot function as well: curve(dnorm, n=10000, add=TRUE, xlim=range(x)) The advantage is, now I can set an ideal population of 10000 to get the ideal curve really smooth. But the big disadvantage is, I don't know how to add "mean=mean(x), sd=sd(x)" arguments to it? It says that it can't mix high level with low level plot functions when I try to set some kind of parameter like "n=10000" to the low level function, it says that there ain't enough x values. So my question is, how to get a smooth curve placed of dnorm over an histogram of sample data, ideally by using the curve method? TIA, Lothar Rubusch
Duncan Murdoch
2006-Sep-25 18:03 UTC
[R] Can't mix high level and low level plot functions.
On 9/25/2006 1:56 PM, Lothar Botelho-Machado wrote:> Hey R-Comunity, > > > I'd like to print out an histogram of some experimental data and add a > smooth curve of a normal distribution with an ideally generated > population having the same mean and standard deviation like the > experimental data. > > > The experimental data is set as vector x and its name is set to > group.name. I paint the histogram as follows: > > hist(data, freq=FALSE, col="lightgrey", ylab="Density", xlab=group.name) > > > > First I did the normal distribution curve this way: > > lines(x, dnorm(x, mean=mean(x), sd=sd(x)), type="l", lwd=2) > > This curve just uses as many values as there are in x. When using small > amounts of sample populations the curve looks really shaky.This is generally the right way to do it, but you likely want to use a different variable for the first two occurrences of x, e.g. x0 <- seq(from=min(x), to=max(x), len=200) lines(x0, dnorm(x0, mean=mean(x), sd=sd(x)), type="l", lwd=2) Duncan Murdoch> > > I tried this one using a high level plot function as well: > > curve(dnorm, n=10000, add=TRUE, xlim=range(x)) > > The advantage is, now I can set an ideal population of 10000 to get the > ideal curve really smooth. But the big disadvantage is, I don't know how > to add "mean=mean(x), sd=sd(x)" arguments to it? It says that it can't > mix high level with low level plot functions when I try to set some kind > of parameter like "n=10000" to the low level function, it says that > there ain't enough x values. > > So my question is, how to get a smooth curve placed of dnorm over an > histogram of sample data, ideally by using the curve method? > > > TIA, > Lothar Rubusch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > 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.
Marc Schwartz (via MN)
2006-Sep-25 18:25 UTC
[R] Can't mix high level and low level plot functions.
On Mon, 2006-09-25 at 19:56 +0200, Lothar Botelho-Machado wrote:> Hey R-Comunity, > > > I'd like to print out an histogram of some experimental data and add a > smooth curve of a normal distribution with an ideally generated > population having the same mean and standard deviation like the > experimental data. > > > The experimental data is set as vector x and its name is set to > group.name. I paint the histogram as follows: > > hist(data, freq=FALSE, col="lightgrey", ylab="Density", xlab=group.name) > > > > First I did the normal distribution curve this way: > > lines(x, dnorm(x, mean=mean(x), sd=sd(x)), type="l", lwd=2) > > This curve just uses as many values as there are in x. When using small > amounts of sample populations the curve looks really shaky. > > > > I tried this one using a high level plot function as well: > > curve(dnorm, n=10000, add=TRUE, xlim=range(x)) > > The advantage is, now I can set an ideal population of 10000 to get the > ideal curve really smooth. But the big disadvantage is, I don't know how > to add "mean=mean(x), sd=sd(x)" arguments to it? It says that it can't > mix high level with low level plot functions when I try to set some kind > of parameter like "n=10000" to the low level function, it says that > there ain't enough x values. > > So my question is, how to get a smooth curve placed of dnorm over an > histogram of sample data, ideally by using the curve method? > > > TIA, > Lothar RubuschThis almost seems like it should be a FAQ. I also checked the R Graphics Gallery (http://addictedtor.free.fr/graphiques/index.php) and didn't see an example there either, unless I missed it. In either case: x <- rnorm(50) hist(x, freq = FALSE) # Create a sequence of x axis values with small # increments over the range of 'x' to smooth the lines x.hypo <- seq(min(x), max(x), length = 1000) # Now use lines() lines(x.hypo, dnorm(x.hypo, mean=mean(x), sd=sd(x)), type="l", lwd=2) HTH, Marc Schwartz