histogram – How do you use hist to plot relative frequencies in R?

histogram – How do you use hist to plot relative frequencies in R?

you can try using the histogram() function in lattice

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

defaults to percent.

Ive 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

histogram – How do you use hist to plot relative frequencies in R?

hist(a, breaks=c(0, 1, 2, 3), freq=FALSE, right=FALSE)

Leave a Reply

Your email address will not be published.