Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet... Robert
Add function to histogram?
8 messages · Robert Lundqvist, Gabor Grothendieck, Peter Wolf +4 more
On 9/20/05, Robert Lundqvist <Robert.Lundqvist at ltu.se> wrote:
Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet...
Try this: set.seed(1) z <- rnorm(100) library(MASS) truehist(z) lines(density(z)) curve(dnorm, color = "red", add = TRUE)
Robert Lundqvist wrote:
Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet... Robert
______________________________________________ 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
??? dat<-rnorm(100) hist(dat,prob=TRUE) x<-seq(-3.5,3.5,length=100) y<-dnorm(x) lines(x,y) have a look at: http://cran.at.r-project.org/doc/manuals/R-intro.pdf Peter Wolf
Le 21.09.2005 10:00, Peter Wolf a ??crit :
Robert Lundqvist wrote:
Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet... Robert
??? dat<-rnorm(100) hist(dat,prob=TRUE) x<-seq(-3.5,3.5,length=100) y<-dnorm(x) lines(x,y) have a look at: http://cran.at.r-project.org/doc/manuals/R-intro.pdf Peter Wolf
For a frequency polygon, try to work around that piece of code, following Peter's notations : dat <- rnorm(100) h <- hist(dat,prob=TRUE, border="gray", col="gray90") diffBreaks <- diff(h$breaks)[1] xx <- c(h$mids[1]-diffBreaks, h$mids, tail(h$mids,1)+diffBreaks) yy <- c(0, h$density, 0) lines(xx, yy, lwd=2) However, you might prefer (and that's probably wise) the kernel density estimator : density() that other people suggested. Romain
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
Le 21.09.2005 10:37, Romain Francois a ??crit :
Le 21.09.2005 10:00, Peter Wolf a ??crit :
Robert Lundqvist wrote:
Is there any neat way to add a curve (frequency function or the like) to a
histogram or other plots? I haven't found one yet...
Robert
??? dat<-rnorm(100) hist(dat,prob=TRUE) x<-seq(-3.5,3.5,length=100) y<-dnorm(x) lines(x,y) have a look at: http://cran.at.r-project.org/doc/manuals/R-intro.pdf Peter Wolf
For a frequency polygon, try to work around that piece of code, following Peter's notations : dat <- rnorm(100) h <- hist(dat,prob=TRUE, border="gray", col="gray90") diffBreaks <- diff(h$breaks)[1] xx <- c(h$mids[1]-diffBreaks, h$mids, tail(h$mids,1)+diffBreaks) yy <- c(0, h$density, 0) lines(xx, yy, lwd=2) However, you might prefer (and that's probably wise) the kernel density estimator : density() that other people suggested. Romain
I just added frequency polygon to the R Graph Gallery, see : http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=101 Regards, Romain
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
Robert Lundqvist wrote:
Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet...
For a general solution, one usually has to scale the density function to
fit the plot. The simple, but convenient, function rescale() in the
plotrix package can be used like this:
add.density.curve<-function(y,density=NA) {
xyrange<-par("usr")
if(is.na(density)) y.density<-density(y)
lines(seq(xyrange[1],xyrange[2],length=length(y.density$y)),
rescale(y.density$y,c(0,xyrange[4])))
}
This only works for histograms, but I am working on a general function
that will outsmart the habit of some functions of running the plot range
into negative numbers when these are not possible (i.e. you can't have a
negative count in a histogram - or a negative density except in the very
latest physics).
Jim
Le 20 Septembre 2005 09:53, Robert Lundqvist a ??crit??:
Is there any neat way to add a curve (frequency function or the like) to a histogram or other plots? I haven't found one yet...
The one key thing here is not to forget "prob=TRUE" in the call to hist(), to ensure that the y-axis is scaled in proportions, not in frequencies.
Vincent Goulet, Professeur agr??g?? ??cole d'actuariat Universit?? Laval, Qu??bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
To be more precise, when using hist(prob=T) the y axis shows the densities. If you want relative frequencies (proportions) you can use the histogram(x, type=) function in the package lattice or write your own function. Cheers Francisco
From: Vincent Goulet <vincent.goulet at act.ulaval.ca> Reply-To: vincent.goulet at act.ulaval.ca To: r-help at stat.math.ethz.ch Subject: Re: [R] Add function to histogram? Date: Wed, 21 Sep 2005 17:10:55 -0400 Le 20 Septembre 2005 09:53, Robert Lundqvist a Âécrit :
Is there any neat way to add a curve (frequency function or the like) to
a
histogram or other plots? I haven't found one yet...
The one key thing here is not to forget "prob=TRUE" in the call to hist(), to ensure that the y-axis is scaled in proportions, not in frequencies. -- Vincent Goulet, Professeur agrÂégÂé ÂÉcole d'actuariat UniversitÂé Laval, QuÂébec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
______________________________________________ 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