Creating a histogram from a frequency vector

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a histogram from a frequency vector

R help mailing list-2
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?

Thanks Nick Wray
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a histogram from a frequency vector

Ivan Krylov
On Wed, 9 Oct 2019 16:12:57 +0100 (BST)
Nick Wray via R-help <[hidden email]> wrote:

> 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

> I can't see an elegant way <...> of creating a histogram from this
> data set.  Is there one?

A histogram is a bar plot of frequencies of data values falling into
specific bins, so you can reconstruct one yourself using the barplot()
function.

--
Best regards,
Ivan

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a histogram from a frequency vector

S Ellison-2
In reply to this post by R help mailing list-2
> 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}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a histogram from a frequency vector

R help mailing list-2
In reply to this post by Ivan Krylov
Aargh of course - so obvious I'd completely overlooked that.>.  Thanks Nick

> On 09 October 2019 at 16:21 Ivan Krylov <[hidden email]> wrote:
>
>
> On Wed, 9 Oct 2019 16:12:57 +0100 (BST)
> Nick Wray via R-help <[hidden email]> wrote:
>
> > 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
>
> > I can't see an elegant way <...> of creating a histogram from this
> > data set.  Is there one?
>
> A histogram is a bar plot of frequencies of data values falling into
> specific bins, so you can reconstruct one yourself using the barplot()
> function.
>
> --
> Best regards,
> Ivan

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a histogram from a frequency vector

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

Here are 3 ways.

The first are almost the same, they use base graphics.

x <- 1:6
y <- c(73,53,42,67,41,50)

barplot(setNames(y, x))

Or

names(y) <- x
barplot(y)


And 3:

library(ggplot2)
ggplot(data.frame(x, y), aes(x, y)) +
   geom_col()


Hope this helps,

Rui Barradas


Às 16:12 de 09/10/19, Nick Wray via R-help escreveu:

> 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?
>
> Thanks Nick Wray
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a histogram from a frequency vector

Berry, Charles


> On Oct 9, 2019, at 9:58 AM, Rui Barradas <[hidden email]> wrote:
>
> Hello,
>
> Here are 3 ways.
>

For a large number of bars, sometimes this:

 plot( y, type='h') # maybe use lwd=5

Chuck


> The first are almost the same, they use base graphics.
>
> x <- 1:6
> y <- c(73,53,42,67,41,50)
>
> barplot(setNames(y, x))
>
> Or
>
> names(y) <- x
> barplot(y)
>
>
> And 3:
>
> library(ggplot2)
> ggplot(data.frame(x, y), aes(x, y)) +
>  geom_col()
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 16:12 de 09/10/19, Nick Wray via R-help escreveu:
>> 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?
>> Thanks Nick Wray
>> [[alternative HTML version deleted]]
>> ______________________________________________
>> [hidden email] 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.
>>
>

______________________________________________
[hidden email] 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.