ggplot2 and Hexagon Binning in R

The original charting of the NHANES data used basic frequency and density plotting using histograms and scatter plots. ggplot2 is a package for flexibly visualizing all kinds of data.

> install.packages("ggplot2")
The downloaded binary packages are in
> library("ggplot2", lib.loc="/Library/Frameworks/R.framework/Versions/3.0/Resources/library")
> library(ggplot2)

It the last post there were some examples of how to plot some basic histograms and scatterplots (including a density distribution of BMI).  Hexagon Binning is a form of bivariate histogram that excel’s when the size of the data set is >10^6 (Lewin-Koh, 2016).

> install.packages("hexbin")
> ggplot(DS0012, aes(x=DS0012$height, y=DS0012$weight)) 
+ geom_hex(bins=30) + xlab("Height [cm]") + ylab("Mass [kg]")

ggplot2 HexBin visualization


The presentation of the density plot changes dramatically when hexagons are used.  It seems to have a much more fluid dynamic versus the harsh vertical and horizontal lines if squares were to be used for the same end.

This visualization also starts to show us where there are areas of dense concentration (right around 165cm and 70kg, and at 90cm and 20kg; not to mention the fact that it is quite a beautiful presentation).


About dwmaasberg

Memories are physical connections between neurons. I think that is pretty cool!
This entry was posted in ggplot2, R, Statistics. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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