R one liner: histogram for integers

Here is a function for making better histograms for integers.

For example, you have

x = c(1,1,1,1,1,2,2,2,2,6,6,6,6,7,7,7,7,7,8,8)

If you call hist(x) you get the 1s and 2s in the same bin.

int.hist = function(x,ylab="Frequency",...) {
barplot(table(factor(x,levels=min(x):max(x))),space=0,xaxt="n",ylab=ylab,...);axis(1)
}

Then trying int.hist(x):

Advertisements

2 thoughts on “R one liner: histogram for integers”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s