Stephen Ellison
2019-Oct-09 16:31 UTC
[R] Creating a histogram from a frequency vector - correction!
Sorry; that was not the working version.
Should be
freqs <- c(11, 19, 5, 3, 2, 1, 0, 0, 2, 3, 2)
freqhist <- function(counts, xname=deparse(substitute(counts)),
breaks=0:length(counts),
mids=(breaks[-1]+breaks[-length(breaks)])/2 , ...){
binwidths <- diff(breaks)
dens <- counts/(binwidths*sum(counts))
retval <- structure(list(breaks=breaks, counts=counts, density=dens,
mids=mids, xname=xname,
equidist=all(diff(breaks)==diff(breaks[1:2]) ) ),
class="histogram")
}
plot(freqhist(freqs, breaks=c(4*0:5, 10*3:5, 70, 100, 140)) )
#Also works equidistant with default 0:length(counts) breaks:
f2 <- c(30, 39, 31, 29, 10, 6, 3, 1, 0, 1)
plot(freqhist(f2))
Steve E
> -----Original Message-----
> From: Stephen Ellison
> Sent: 09 October 2019 17:29
> To: 'Nick Wray'; r-help at r-project.org
> Subject: RE: [R] Creating a histogram from a frequency vector
>
> > I have a vector like say 73,53,42,67,41,50 where these numbers are the
> > number of occurrences of the data values 1,2,3,4,5,6 - so in essence I
have
> > the frequency bit from the hist() function. I can't see an
elegant way
> (there
> > are clearly messy workarounds like generating a vector of 73 1's,
53 2's etc)
> of
> > creating a histogram from this data set. Is there one?
>
> hist() generates a histogram object that it then plots.
>
> You can use your frequency vector to generate the same kind of object and
> then just plot it, though you'll have to provide breaks (possibly
defaulted, if
> they're just 0:length(frequencies) ) and you'd have to work on the
density
> component a bit.
>
> I'm sure this is out there somewhere already, but here's as an
example, using
> values pulled from a (nonequidistant) ?hist example and using a short off-
> the-cuff function to build the histogram object:
>
> freqs <- c(11, 19, 5, 3, 2, 1, 0, 0, 2, 3, 2) #islands
> brks <- c(4*0:5, 10*3:5, 70, 100, 140)
>
> freqhist <- function(counts, xname=deparse(substitute(frequencies)),
> breaks=0:length(frequencies),
> mids=(breaks[-1]+breaks[-length(breaks)])/2 , ...){
>
> binwidths <- diff(breaks) #This copes with unequal break intervals
> dens <- counts/(binwidths*sum(counts))
>
> retval <- structure(list(breaks=breaks, counts=counts,, density=dens,
> mids=mids, xname=xname, equidist=all(diff(breaks)==diff(breaks[1:2]) ),
> class="histogram")
> }
>
> plot(freqhist(freqs, breaks=brks))
>
> #Also works equidistant with default 0:length(counts) breaks:
>
> f2 <- c(30, 39, 31, 29, 10, 6, 3, 1, 0, 1)
> plot(freqhist(f2))
>
> Steve Ellison
>
>
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
David Carlson
2019-Oct-09 20:49 UTC
[R] Creating a histogram from a frequency vector - correction!
Does it have to be a histogram or does it just have to look like one? counts <- c(73,53,42,67,41,50) vals <- c(1,2,3,4,5,6) barplot(counts~vals, space=0) If you want a more histogram-like axis: barplot(counts~vals, space=0, names.arg="") axis(1, 1:6 - .5, 1:6) David L Carlson Anthropology Department Texas A&M University College Station, TX 77843 On Wed, Oct 9, 2019 at 11:32 AM Stephen Ellison <S.Ellison at lgcgroup.com> wrote:> > Sorry; that was not the working version. > > Should be > > freqs <- c(11, 19, 5, 3, 2, 1, 0, 0, 2, 3, 2) > > freqhist <- function(counts, xname=deparse(substitute(counts)), breaks=0:length(counts), > mids=(breaks[-1]+breaks[-length(breaks)])/2 , ...){ > > binwidths <- diff(breaks) > dens <- counts/(binwidths*sum(counts)) > > retval <- structure(list(breaks=breaks, counts=counts, density=dens, > mids=mids, xname=xname, > equidist=all(diff(breaks)==diff(breaks[1:2]) ) ), > class="histogram") > } > > plot(freqhist(freqs, breaks=c(4*0:5, 10*3:5, 70, 100, 140)) ) > > #Also works equidistant with default 0:length(counts) breaks: > > f2 <- c(30, 39, 31, 29, 10, 6, 3, 1, 0, 1) > plot(freqhist(f2)) > > Steve E > > > > -----Original Message----- > > From: Stephen Ellison > > Sent: 09 October 2019 17:29 > > To: 'Nick Wray'; r-help at r-project.org > > Subject: RE: [R] Creating a histogram from a frequency vector > > > > > I have a vector like say 73,53,42,67,41,50 where these numbers are the > > > number of occurrences of the data values 1,2,3,4,5,6 - so in essence I have > > > the frequency bit from the hist() function. I can't see an elegant way > > (there > > > are clearly messy workarounds like generating a vector of 73 1's, 53 2's etc) > > of > > > creating a histogram from this data set. Is there one? > > > > hist() generates a histogram object that it then plots. > > > > You can use your frequency vector to generate the same kind of object and > > then just plot it, though you'll have to provide breaks (possibly defaulted, if > > they're just 0:length(frequencies) ) and you'd have to work on the density > > component a bit. > > > > I'm sure this is out there somewhere already, but here's as an example, using > > values pulled from a (nonequidistant) ?hist example and using a short off- > > the-cuff function to build the histogram object: > > > > freqs <- c(11, 19, 5, 3, 2, 1, 0, 0, 2, 3, 2) #islands > > brks <- c(4*0:5, 10*3:5, 70, 100, 140) > > > > freqhist <- function(counts, xname=deparse(substitute(frequencies)), > > breaks=0:length(frequencies), > > mids=(breaks[-1]+breaks[-length(breaks)])/2 , ...){ > > > > binwidths <- diff(breaks) #This copes with unequal break intervals > > dens <- counts/(binwidths*sum(counts)) > > > > retval <- structure(list(breaks=breaks, counts=counts,, density=dens, > > mids=mids, xname=xname, equidist=all(diff(breaks)==diff(breaks[1:2]) ), > > class="histogram") > > } > > > > plot(freqhist(freqs, breaks=brks)) > > > > #Also works equidistant with default 0:length(counts) breaks: > > > > f2 <- c(30, 39, 31, 29, 10, 6, 3, 1, 0, 1) > > plot(freqhist(f2)) > > > > Steve Ellison > > > > > > > > ******************************************************************* > This email and any attachments are confidential. Any use...{{dropped:8}} > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=ZVbXKJ26_NjMEwyoLt414s947aHWk1Eui0kMAJ7TEC4&s=Kl_9bErQCYPBSlnEusw5vugVXiykEewhBBHPZ99OIAk&e> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=ZVbXKJ26_NjMEwyoLt414s947aHWk1Eui0kMAJ7TEC4&s=6HhBux7d56HS1UuFrtWbphPMjrwGQYdPxuSal_Mcvhw&e> and provide commented, minimal, self-contained, reproducible code.