A quick tutorial on how to visualize scalar data using ggplot2.
Author
Guilherme D. Garcia
Published
October 23, 2023
Ordinal data can be a bit tricky to visualize. The goal here is to share one type of figure I personally like to use when I analyze this type of data. Because scalar data is rarely normal, using ggplot2 functions such as stat_summary() isn’t particularly useful, since means aren’t that informative in such scenarios. Furthermore, since we tend to use ordinal models in these situations, one way to maximize the alignment between visualization and modeling is to use the actual categories from the scale in our figure.
Preparing the data
Let’s load some packages and our data (viz.RData), which you can download here. These are hypothetical data used in Garcia (2023).
We now create a summary where we calculate the percentage of responses in each certainty category by L1 and Proficiency, the two variables of interest in our example. In the code below, Dark is a dummy column that will be useful shortly.
Code
prop=viz|>summarize(n =n(), .by =c(L1, Proficiency, Certainty))|>mutate(Prop =n/sum(n), .by =c(L1, Proficiency), Dark =if_else(Certainty%in%c("3", "4"), "yes", "no"))prop|>slice_sample(n =3)#> # A tibble: 3 × 6#> L1 Proficiency Certainty n Prop Dark #> <fct> <fct> <ord> <int> <dbl> <chr>#> 1 Spanish Adv 2 12 0.12 no #> 2 English Adv 2 9 0.09 no #> 3 Spanish Beg 2 35 0.35 no
Next, lets’s create our figure, which will have several layers.
The figure uses bars and coord_flip() to emulate the original scale in the data. The proportions of each scale category are shown as text, and their size is proportional to the actual percentage. Finally, we use colors to match lower and upper bounds of the scale, so it’s easy to see that advanced learners are more certain in both language groups compared to beginners.
The dummy columns Dark plays a minor but visually important role here: it’s there to make sure that the color of the percentages and the color of the bars have enough contrast. This figure is adapted from Garcia (2021) (chapters 5 and 8).