Johann Horvat Johann Horvat - 1 month ago 16
R Question

Histogram of linear data

I'm doing some experiments on histograms of image data. At a first step I tried to make a linear vector with values from 0 to 255. When printing the histogram of this vector I'd assume that every value would have the same frequency. But the

hist
function returns histograms where 0 has a higher frequency and 255 a lower than other values. Even when choosing a different bin size...

What am I doing wrong?

rampImageData<-rep(rep(0:255, each=4), each=512)
hist(rampImageData)
# hist(rampImageData, breaks=256)
# hist(rampImageData, breaks=128)
# hist(rampImageData, breaks=64)
# hist(rampImageData, breaks=32)


enter image description here

Answer

It is related to "binning".

Look:

library(ggplot2)

"Unexpected" behaviour for a Uniform:

qplot(rampImageData, geom = 'histogram') # default bins = 30 

enter image description here

"Expected" behaviour for a Uniform:

qplot(rampImageData, geom = 'histogram', bins = 1)

enter image description here

Comments