16

How do you use hist() to plot relative frequencies in R?

If I do the following, I will get a density plot, but I want a relative frequency plot:

a <- c(0,0,0,1,1,2)
hist(a, freq=FALSE)

I want to see a histogram with the following relative frequencies:

.5 for 0 to 1,

.33 for 1 to 2,

and .166 for 2 to 3.

Will
  • 812
  • 3
  • 11
  • 21
  • 1
    apparently you can't use freq=TRUE with plot=FALSE, so this is a good question with some good answers below, thanks. – PatrickT Nov 13 '13 at 06:47
  • 1
    Possible duplicate of [Use hist() function in R to get percentages as opposed to raw frequencies](http://stackoverflow.com/questions/7324683/use-hist-function-in-r-to-get-percentages-as-opposed-to-raw-frequencies) – majom Aug 05 '16 at 14:40
  • For anyone looking for the answer in ggplot : [Relative frequency histogram in R, ggplot](https://stackoverflow.com/questions/57430885/relative-frequency-histogram-in-r-ggplot) – ishigoya Dec 03 '20 at 22:38

5 Answers5

14

you can try using the histogram() function in lattice

a <- c(0,0,0,1,1,2)
library(lattice)
histogram(a)

defaults to percent.

Greg
  • 11,564
  • 5
  • 41
  • 27
9

I've added a new function to the HistogramTools package on CRAN, PlotRelativeFrequency() which takes a histogram object and generates a relative frequency histogram plot. This is available now from R-Forge and will be in HistogramTools 0.3 for the next CRAN release.

Basically, you just need to make two modifications to the default histograms in R. First, you need to divide each count by the sum of all of the counts, and you need to replace the y-axis label to note that it is now plotting Relative Frequencies.

x<-runif(100)
h<-hist(x, plot=F)
h$counts <- h$counts / sum(h$counts)
plot(h, freq=TRUE, ylab="Relative Frequency")

Or, simply

install.packages("HistogramTools", repos="http://R-Forge.R-project.org")
library(HistogramTools)
PlotRelativeFrequency(hist(x, plot=F))

enter image description here

MurrayStokely
  • 345
  • 2
  • 6
6
hist(a, breaks=c(0, 1, 2, 3), freq=FALSE, right=FALSE)
caracal
  • 2,690
  • 19
  • 22
2
histo<-hist(yourvariable)
barplot(histo$counts/n,col="white",space=0)->bp   # n=length(yourvariable)
axis(1,at=c(bp),labels=histo$mids)
title(ylab="Relative Frequency",xlab="Your Variable Name")
king2rook1
  • 21
  • 1
2

Not properly a traditional histogram...

h<-hist(yourdata)
plot(h$mids,100*h$counts/sum(h$counts),type="h")
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153