Categorical data
When working with categorical data we generally simply count the number of occurrences of each category and make a table. We can visualize such a table as a bar plot.
The information in any bar plot can always be conveyed to your readers in a table instead:
| Classical |
45 |
| Hip Hop |
294 |
| Jazz |
81 |
| Pop |
361 |
| Rock |
219 |
However, bar plots are more effective at conveying gist of the data. For example, is the count of Hip Hop closer to Pop, or Rock? It’s much easier to see (quickly) in the graph, compared to the table.
In a bar plot, the height of bars may indicate the actual counts of each level or the proportion of the total data in each level. In the graph above, I get the best of both worlds by showing proportions on the left and counts on the right.
The advantage of counts is that they convey the total size of the dataset, where as proportions (a more abstract, quantitative measure) hide this information. Proportions also make more assumptions, as they assume that you have counted all relevant categories (so the proportions must sum to 1). On the other hand, proportions are easier to compare across datasets. For example, if we found another data set of genre labels that looked this:
If we look at the counts (on the right), we can see that this dataset is much bigger than the last one. However, by looking at the proportions (on the left), it is easier to see that, for example, the proportion of classical music is about the same as before.
Bar plots are most useful when the total number of levels is fairly small (perhaps, 12 or fewer). In these (ideal) cases, each bar should be labeled with its own category. By default, the bars should be ordered in a meaningful way based on the levels of the variable, not the values of the barplot. We might organize labels alphabetically, as I do above, or perhaps group them based on some logic relevant to our research theories. For example, these categories might make more sense grouped musically like:
If there are a large number of categories (like more than 15–20), don’t bother labeling each bar. In these cases, bar plots are not really very useful; However, sometimes sorting a large numbers of bars by bar height can reveal useful patterns. For a classic example, let’s consider what happens if I count how often every unique “word” occurs in this text book. There are 196,016 in the book, but only 19,420 unique words. That is, we have sampled 196,016 values from a categorical variable with 19420 levels. A bar plot with this many levels, with the levels sorted alphabetically, will look like this:
Useless! However, if we sort the table by count, we see this:
We can now see that there are a small number of very common words, but that the frequency of words drops off exponentially.
- The most common word is “the,” occurring 8,164 times;
- The second most common word is “of,” occurring 4,930 times;
- The third most common word is “of,” occurring 4,930 times;.
- etc.
This is consistent with Zipf’s Law, which is often seen in this sort of data.
Another approach to graphing a single categorical variable is with a pie chart. Pie charts should be used sparingly, if at all, as they are looked down upon by many researchers. The issue is that humans have a hard time judging the relative size based on area:
How much more rock is there compared to jazz? Twice as much? Three times as much? It’s hard to judge. A bar plot makes this easier. However, a pie chart might be useful in certain cases, depending on what gist you want to point out to your reader. For example, it’s easy to see here that the dataset is about one third pop, one third hip hop, and one third other. This is not nearly as obvious in the barplots.
Numeric data
For numeric variables, the absolute best visualization is the quantile plot. Simply sort the data points, usually from lowest to highest, and plot them from left to right:
In such a plot, the X axis simply represents the quantiles of the data, with the median in the middle, and the quartiles at 25% and 75% of the screen, etc.
Quantile plots are extremely informative visualizations. From them, you can see useful “gists” like symmetry, the number of modes, and generally how concentrated data is at different Y values:
- The flatter the curve is, the more concentrated the data is at the flat level.
- The steeper the curve, the less concentrated.
- The closer you are to \(45^{\circ}\)-angle curve, the more uniform the distribution at that point.
For example, here is a totally uniform distribution:
Here is a distribution with two peaks (bimodal):
Can you see which of the two peaks is more concentrated?
The upper one! We can tell because the slope is flattest where \(Y > 40\). The steep part in the middle is a relatively empty range between the two peaks.
The really useful thing about quantile plots is that we are looking at all the data, so it’s also possible to notice small details about the sample (useful for descriptive analysis).
- You can see how the most extreme individual values fit in the curve (bottom left, and top right points). Outliers will be obvious.
- You can see where data values are bounded (example further below).
- You can also see things like gaps, where no values occur, which show up as a break or a kink in the curve:
It is often useful to compare your quantiles to known distributions, like the normal distribution, shown here as a dashed line:
Another approach to visualizing a numeric variables, is with a histogram. Values of the variable are “binned” into ranges and the number of values in each bin are counted and then represented by the height of bars:
Histograms are more commonly seen in papers than quantile plots. However, they have one important disadvantage: the result depends a lot on the size of the bins. There are various ways of determining the size of bins, but it is generally automated by graphing software. Lets see what happens if we increase the number of bins on the same data:
Ah! Only now can we see that weird gap in the data, which we immediately saw in our last quantile plot.
Visualizing data in one dimension is extremely important during the data preparation and cleaning process! Use quantile plots and histograms to look for problems in the data, like the gap in the data shown above. For another example, you might wonder if a distribution is hitting boundaries:
Or perhaps a “continuous,” real-number variable is really being rounded more than you think:
This is where the quantile plot shines, because the histogram won’t show this unless there are a lot of bins.