4. Show uncertainty

use "https://tdmize.github.io/data/data/hrs_si.dta", clear
(Health and Retirement Study 1992-2022 - Cleaned for MCAP SI)
// 4.a - Add 95% confidence intervals to the bar chart
mean cesd if year == 2022, over(dayweekdrink)
Mean estimation                                Number of obs = 15,114

---------------------------------------------------------------------
                    |       Mean   Std. err.     [95% conf. interval]
--------------------+------------------------------------------------
c.cesd@dayweekdrink |
                 0  |      1.715      0.022         1.671       1.759
                 1  |      1.308      0.038         1.235       1.382
                 2  |      1.233      0.051         1.133       1.333
                 3  |      1.327      0.063         1.204       1.451
                 4  |      1.264      0.094         1.080       1.448
                 5  |      1.199      0.093         1.017       1.381
                 6  |      0.971      0.114         0.748       1.194
                 7  |      1.301      0.072         1.160       1.442
---------------------------------------------------------------------
marginsplot, recast(bar) plotopts(barwidth(.5) col(*.7)) ciopts(col(gs8)) ///
    ytitle("Depressive symptoms") ylabel(0(0.5)2) ///
    xtitle("Days drinking alcohol per week") ///
    title("[4.a] Average number of depressive symptoms") ///
    subtitle("By number of days drinking per week (95% confidence intervals)")
Variables that uniquely identify means: dayweekdrink

[4.a] Bar chart of the average number of depressive symptoms by days drinking alcohol per week (0 to 7), with 95% confidence intervals: the average is highest at 0 days (about 1.7), lowest at 6 days (about 1.0), and between 1.2 and 1.3 for the other numbers of days; the intervals are widest for 4 to 6 days.

library(foreign)            # load Stata datasets
library(ggplot2)            # main graphics package
library(dplyr)              # data manipulation tools
library(cleanplots)         # Mize's graphics scheme

## Use cleanplots colors, markers, lines, and theme for all graphs ##
cleanplots_defaults()
theme_update(plot.title.position = "plot")   # titles start at left edge of graph

# Health and Retirement Study Data
hrs <- read.dta("https://tdmize.github.io/data/data/hrs_si.dta")

# 4.a - Add 95% confidence intervals to the bar chart (2022 wave)
ggplot(filter(hrs, year == 2022), aes(x = dayweekdrink, y = cesd)) +
  stat_summary(fun = mean, geom = "bar", fill = cleanplots_colors("ltblue", bars = TRUE),
               width = .7) +
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = .3, color = "gray50") +
  labs(x = "Days drinking alcohol per week",
       y = "Depressive symptoms",
       title = "[4.a] Average number of depressive symptoms",
       subtitle = "By number of days drinking per week (95% confidence intervals)") +
  scale_y_continuous(breaks = seq(0, 2, 0.5)) + coord_cartesian(ylim = c(0, 2))

[4.a] Bar chart of the average number of depressive symptoms by days drinking alcohol per week (0 to 7), with 95% confidence intervals: the average is highest at 0 days (about 1.7), lowest at 6 days (about 1.0), and between 1.2 and 1.3 for the other numbers of days; the intervals are widest for 4 to 6 days.

// 4.b - Recreate plot above but with a dot plot
marginsplot, recast(scatter) recastci(rspike) ciopt(col(*.5)) ///
    ytitle("Depressive symptoms") ylabel(0(0.5)2) ///
    xtitle("Days drinking alcohol per week") ///
    title("[4.b] Average number of depressive symptoms") ///
    subtitle("By number of days drinking per week (95% confidence intervals)")
Variables that uniquely identify means: dayweekdrink

[4.b] The same averages and 95% confidence intervals as a dot plot: a dot at each average with a vertical line for its confidence interval.

# 4.b - Recreate plot above but with a dot plot
ggplot(filter(hrs, year == 2022), aes(x = dayweekdrink, y = cesd)) +
  stat_summary(fun = mean, geom = "point", size = 3) +
  stat_summary(fun.data = mean_cl_normal, geom = "linerange", alpha = .5) +
  labs(x = "Days drinking alcohol per week",
       y = "Depressive symptoms",
       title = "[4.b] Average number of depressive symptoms",
       subtitle = "By number of days drinking per week (95% confidence intervals)") +
  scale_y_continuous(breaks = seq(0, 2, 0.5)) + coord_cartesian(ylim = c(0, 2))

[4.b] The same averages and 95% confidence intervals as a dot plot: a dot at each average with a vertical line for its confidence interval.

Bad graph levity

Tweet: Wait a second, these are not real error bars ... the author literally just put the letter T above the bar graphs, above a bar chart whose error bars are the letter T.

Moving forward: building blocks

“There is a strand of the data viz world that argues everything could be a bar chart. That’s possibly true but also possibly a world without joy.” (Amanda Cox, Editor, The Upshot, quoted in HBR 2013)

  • As we move into more advanced data and model visualizations the basic tools we learned in this section will often reappear
  • Aspects of both bar charts and dot plots are used very often in displaying complex model results
    • They both have good visualization properties and work nicely as the building blocks of more complicated graphs—where you can layer on additional aspects

Colorful wooden toy building blocks stacked into towers.

Bar chart that didn’t catch on

W.E.B. Du Bois's hand-drawn spiral bar chart of the assessed value of household and kitchen furniture owned by Georgia Negroes.

Read more about this spiral chart and other pioneering visualizations of W.E.B. DuBois

The Methodology Center illustration: small line drawings of graphs around a central MCAP circle.

The spiral bar chart drawing in the Methodology Center illustration.

*Read about The Methodology Center’s graphic

Back to top