newdev14 newdev14 - 4 months ago 22
R Question

Use hist() function in R to get percentages as opposed to raw frequencies

How can one plot the percentages as opposed to raw frequencies using the hist() function in R?

Answer

Simply using the freq=FALSE argument does not give a histogram with percentages, it normalizes the histogram so the total area equals 1.
To get a histogram of percentages of some data set, say x, do:

h = hist(x)
h$density = h$counts/sum(h$counts)*100
plot(h,freq=FALSE)

Basically what you are doing is creating a histogram object, changing the density property to be percentages, and then re-plotting.