Posted to Tutorials  /  Tags: , ,

How to Visualize and Compare Distributions

Single data points from a large dataset can make it more relatable, but those individual numbers don’t mean much without something to compare to. That’s where distributions come in.

There are a lot of ways to show distributions, but for the purposes of this tutorial, I’m only going to cover the more traditional plot types like histograms and box plots. Otherwise, we could be here all night. Plus the basic distribution plots aren’t exactly well-used as it is.

Before you get into plotting in R though, you should know what I mean by distribution. It’s basically the spread of a dataset. For example, the median of a dataset is the half-way point. Half of the values are less than the median, and the other half are greater than. That’s only part of the picture.

What happens in between the maximum value and median? Do the values cluster towards the median and quickly increase? Are there are lot of values clustered towards the maximums and minimums with nothing in between? Sometimes the variation in a dataset is a lot more interesting than just mean or median. Distribution plots help you see what’s going on.

Want more? Google and Wikipedia are your friend.Anyways, that’s enough talking. Let’s make some charts.

If you don’t have R installed yet, do that now.

Box-and-Whisker Plot

This old standby was created by statistician John Tukey in the age of graphing with pencil and paper. I wrote a short guide on how to read them a while back, but you basically have the median in the middle, upper and lower quartiles, and upper and lower fences. If there are outliers more or less than 1.5 times the upper or lower quartiles, respectively, they are shown with dots.

The method might be old, but they still work for showing basic distribution. Obviously, because only a handful of values are shown to represent a dataset, you do lose the variation in between the points.

To get started, load the data in R. You’ll use state-level crime data from the Chernoff faces tutorial.

# Load crime data
crime <- read.csv("")

Remove the District of Columbia from the loaded data. Its city-like makeup tends to throw everything off.

# Remove Washington, D.C. <- crime[crime$state != "District of Columbia",]

Oh, and you don’t need the national averages for this tutorial either.

# Remove national averages <-[$state != "United States ",]

Now all you have to do to make a box plot for say, robbery rates, is plug the data into boxplot().

# Box plot
boxplot($robbery, horizontal=TRUE, main="Robbery Rates in US")

Want to make box plots for every column, excluding the first (since it’s non-numeric state names)? That’s easy, too. Same function, different argument.

# Box plots for all crime rates
boxplot([,-1], horizontal=TRUE, main="Crime Rates in US")

Multiple box plot for comparision.


Like I said though, the box plot hides variation in between the values that it does show. A histogram can provide more details. Histograms look like bar charts, but they are not the same. The horizontal axis on a histogram is continuous, whereas bar charts can have space in between categories.

Just like boxplot(), you can plug the data right into the hist() function. The breaks argument indicates how many breaks on the horizontal to use.

# Histogram
hist($robbery, breaks=10)

Look, ma! It’s not a a bar chart.

Using the hist() function, you have to do a tiny bit more if you want to make multiple histograms in one view. Iterate through each column of the dataframe with a for loop. Call hist() on each iteration.

# Multiple histograms
par(mfrow=c(3, 3))
colnames <- dimnames([[2]]
for (i in 2:8) {
	hist(crime[,i], xlim=c(0, 3500), breaks=seq(0, 3500, 100), main=colnames[i], probability=TRUE, col="gray", border="white")

Using the same scale for each makes it easy to compare distributions.

Density Plot

For smoother distributions, you can use the density plot. You should have a healthy amount of data to use these or you could end up with a lot of unwanted noise.

To use them in R, it’s basically the same as using the hist() function. Iterate through each column, but instead of a histogram, calculate density, create a blank plot, and then draw the shape.

# Density plot
par(mfrow=c(3, 3))
colnames <- dimnames([[2]]
for (i in 2:8) {
	d <- density(crime[,i])
	plot(d, type="n", main=colnames[i])
	polygon(d, col="red", border="gray")

Multiple filled density plots.

You can also use histograms and density lines together. Instead of plot(), use hist(), and instead of drawing a filled polygon(), just draw a line.

# Histograms and density lines
par(mfrow=c(3, 3))
colnames <- dimnames([[2]]
for (i in 2:8) {
	hist(crime[,i], xlim=c(0, 3500), breaks=seq(0, 3500, 100), main=colnames[i], probability=TRUE, col="gray", border="white")
	d <- density(crime[,i])
	lines(d, col="red")

Histogram and density, reunited, and it feels so good.


The rug, which simply draws ticks for each value, is another way to show distributions. It usually accompanies another plot though, rather than serve as a standalone. Simply make a plot like you usually would, and then use rug() to draw said rug.

# Density and rug
d <- density(crime$robbery)
plot(d, type="n", main="robbery")
polygon(d, col="lightgray", border="gray")
rug(crime$robbery, col="red")

Using a rug under a density plot.

Violin Plot

The violin plot is like the lovechild between a density plot and a box-and-whisker plot. There’s a box-and-whisker in the center, and it’s surrounded by a centered density, which lets you see some of the variation.

# Violin plot
vioplot($robbery, horizontal=TRUE, col="gray")

I bet this violin sounds horrible.

Bean Plot

The bean plot takes it a bit further than the violin plot. It’s something of a combination of a box plot, density plot, and a rug in the middle. I’ve never actually used this one, and I probably never will, but there you go.

# Bean plot

A little too busy for me, but here you go.

Wrapping Up

If you take away anything from this, it should be that variance within a dataset is worth investigating. Picking out single datapoints or only using medians is the easy thing to do, but it’s usually not the most interesting.

About the Author

Nathan Yau is a statistician who works primarily with visualization. He earned his PhD in statistics from UCLA, is the author of two best-selling books — Data Points and Visualize This — and runs FlowingData. Introvert. Likes food. Likes beer. Follow him @flowingdata.

Become a member. Learn to visualize your data. Support FlowingData.

Join Today


This is for people who want to learn to make and design data graphics. Your support goes directly to FlowingData, an independently run site.

Benefits of Membership

  • Instant access to tutorials on how to make and design data graphics
  • Source code and files to use with your own data
  • Four-week course on visualization in R
  • Hand-picked links and resources from around the web


  • Sven Mensing May 16, 2012 at 1:04 am

    Hi, does anybody know if there is a package that combines the violin plot with a scatter plot? This would help people see the actual data used.
    I coded a small example:

    axis(1,c(1,2),c('GNTP a','GNTP b'))
    for (r in 1:ncol(x))
    for(i in 1:N) y[i]=runif(1,-jitt[i],jitt[i])/2



    • Nathan — with the multiple box plot, it might be nice to force horizontal axis labels so you can see all the categories. It worked for me if I run this right before calling boxplot():
      par(mar=par()$mar+c(0,5,0,0), las=1)

      Sven — that’s pretty cool. Jittered scatterplots are a quick-and-dirty approximation to that (not as nice as yours, but less code):
      GroupNr <- rep(c(1,2),length(x))
      plot(jitter(GroupNr), c(x,y))

  • Brian Abelson May 16, 2012 at 7:58 am

    Here’s a simple example of adding transparency to colors in order to visualize the relationships between multiple distributions:


    #generate a bunch of normal distributions around different means







    #assign colors, paste on a number between 10 to 99 to add transparency
    col <- brewer.pal(7, "RdBu")
    alpha <- 50
    col <- paste(col, alpha, sep="")

    plot(x1,y1,type="n",lwd=2, xlim=c(-4,4))
    polygon(x1,y1, col=col[7])
    polygon(x2,y2, col=col[6])
    polygon(x3,y3, col=col[5])
    polygon(x4,y4, col=col[4])
    polygon(x5,y5, col=col[3])
    polygon(x6,y6, col=col[2])
    polygon(x7,y7, col=col[1])

  • All of these examples could be improved by comprehensive titles and labelling. Intelligible wording on a chart or graph makes the difference between confusion and coherence.
    I know you’re just trying to find a design that works, but if the readers don’t understand your message, then your design, regardless of originality and creativity, has failed. For example, the Multiple box plot shows 7 indicates but only 3 labels?!? The Bean plot shows 7 indicators are only 5 labels?!? It would only take a few seconds to ensure that each indicate was labeled.
    BTW, histograms are distinguished from bar charts because they show the distribution of data – often the values within ranges or class intervals. There are no spaces between the columns on a histogram but that’s just a convention, not the essential difference.

  • Hey friends, pay no attention to that last paragraph of my previous comment. Obviously having a demented morning to be followed by a demented afternoon.

  • I second Sally’s comment – this whole post is really hard to grasp due to lack of proper legend, labels and titles on the graphs. I would really like to understand this better, but can’t figure what exactly is being plotted on either the x or y axes of any of these graphs. At the risk of appearing stupid, can someone please explain.

    • Hey Mark,

      I think he explained the boxplot’s notable points on the x-axis. There is no significance to the y-axis in this example (although I have seen graphs before where the thickness of the box plot is proportional to the size of the sample; it makes the multiple box plot chart more informative.) Also, most of the time I see box plots drawn vertically.

      The histogram is pretty simple, and can also be done by hand pretty easily. The data points are “binned” – that is, put into groups of the same length. It looks like R chose to create 13 bins of length 20 (e.g. [0-20), [20-40), etc.) Then the y-axis is the number of data points in each bin. That’s what they mean by “frequency”.

      The density plot uses some kind of estimation of frequency, although it’s similar to the histogram. Not sure what the heck that violin plot is, though…

  • Hi Nathan,

    Great tutorial. I’ve been thinking about learning R for a while and this post is giving me the inspiration to finally take a crack at it. One related question for you – I have both a PC and Mac at my disposal – would you recommend one over the other for using R?

    Thanks so much!

  • Hey Nathan

    Thanks for this. I quite like strip plots where each dot is hollow. This is good for limited space, where you’re only trying to show broad spread and outliers. Obviously spikes in the tail are not observed this way, but it’s a quick snap shot.

    What would be good is a tutorial on box plots, where you can over-ride the 1.5 * IQR defaults, which determin the default whisker length. I often need to show simulated output from a stochastic monte carlo model, so I’d like whiskers at the 10th and 90th percentile, with dots at the 1 and 99th percentile.

    The advantge of strip and box over historgram, is that you avoid discussions about the height of histograms.

  • Eye Reward June 15, 2012 at 3:52 pm

    How are outliers handled?

  • What do you intend showing when you plot histogram?

  • Joana Maria October 30, 2016 at 6:43 am

    In the for loop for multiple histograms I believe it should be[,i] and not crime[,i]

Add Comment

You must be logged in to post a comment.

More Tutorials See All →

Moving Past Default R Charts

Customizing your charts doesn’t have to be a time-intensive process. With just a teeny bit more effort, you can get something that fits your needs.

How to visualize data with cartoonish faces ala Chernoff

The goal of Chernoff faces is to show a bunch of variables at once via facial features like lips, eyes, and nose size. Most of the time there are better solutions, but the faces can be interesting to work with.

An Easy Way to Make a Treemap

If your data is a hierarchy, a treemap is a good way to show all the values at once and keep the structure in the visual. This is a quick way to make a treemap in R.

R Cheat Sheet and Guide for Graphical Parameters

You can customize graphics in R with par(), but the docs are mostly text and just organized alphabetically. Here is a more visual reference, categorized by what you can change. Plus, a one-page printout.