R Tutorial: Distribution of one variable

Learn how to visualize a single categorical variable in R using table() and bar charts. The tutorial covers marginal distributions, removing fill aesthetics, and using facet_wrap to compare distributions across groups. Bar charts are shown to convey proportions more clearly than pie charts.
Chapters
You might not have noticed, but already you've been creating plots that illustrate the relationship between two variables in your data set. It's a bit unusual to lead out with this, but it gets you thinking early about the multivariate structure that's found in most real data sets. Now let's zoom in on working with just a single variable. To compute a table of counts for a single variable like id, just provide a vector into the table function as the sole argument. One way to think of what we've done is to take the original two way table and then sum the cells across each level of a line. Since we've summed over the margins of the other variables, this is sometimes known as a marginal distribution. The syntax to create the simple bar chart is straightforward as well. Just remove the fill align argument. Another useful way to form the distribution of a single variable is to condition on a particular value of another variable. We might be interested, for example, in the distribution of ID for all neutral characters. We could either filter the data set and build a bar chart using only cases where alignment was neutral, or we could use a technique called faceting. Faceting breaks the data into subsets based on levels of a categorical variable and then constructs a plot for each. To implement this in ggplot2, we just need to add a faceting layer, the facet wrap function, then a tilde which which can be read as broken down by and then our variable align. The result is three simple bar charts side by side, the first one corresponding to the distribution of ID within all cases that had a bad alignment, and so on for good and neutral alignments. If this plot feels familiar, it should. In essence, it's a rearrangement of the stacked bar charts that we considered at the beginning of the chapter. Each facet in the plot on the left corresponds to a single stacked bar in the plot on the right. They allow you to get a sense of the distribution of a single variable by looking at a single facet or a single stacked bar, or the association between the variables by looking across facets or across stacked bars. A discussion of plots for categorical data wouldn't be complete without some mention of the pie chart. The pie chart is a common way to display categorical data where the size of the slice corresponds to the proportion of cases that are in that level. Here's a pie chart for the identity variable and it looks pleasing enough. The problem with pie charts though, is that it can be difficult to assess the relative size of the slices. Here is the green public slice or the gray na slice bigger. If we represent this data using a bar chart, the answer is obvious. The proportion of public is greater. For that reason, it's generally a good idea to stick to bar charts. Okay, now it's your turn to practice with simple bar charts and faceting.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







