(Health and Retirement Study 1992-2022 - Cleaned for MCAP SI)
// 2.a - Pie charts are generally not recommendedgraph pie, over(srhealth) ///title("[2.a] Pie chart for distribution of self-rated health among older adults")
library(foreign) # load Stata datasetslibrary(ggplot2) # main graphics packagelibrary(dplyr) # data manipulation toolslibrary(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 Datahrs <-read.dta("https://tdmize.github.io/data/data/hrs_si.dta")# 2.a - Pie charts (generally not recommended)ggplot(filter(hrs, !is.na(srhealth)), aes(x ="", fill = srhealth)) +geom_bar(width =1) +coord_polar(theta ="y") +labs(title ="[2.a] Pie chart for distribution of self-rated health among older adults",fill ="") +scale_fill_viridis_d() +theme_void(base_size =12)
*Bar charts are simple but very effective for comparing amounts across groups
Stacked bar charts
Stacked bar charts are a useful alternative to pie charts
Also, make it easy to compare across time/waves or groups (which is very difficult with a pie chart)
Stacked bar charts can be used when you want a bar to fill the entire space of an axis (compare to how much space on the y-axis is covered by the bars above)
Stacked bar charts are especially useful when you have categorical outcome (y) variables and/or when the total of multiple variables is of interest
Works well for binary dependent variables
Especially useful for nominal/ordinal dependent variables
# Quick work to flag only those included in first HRS waveids_1992 <- hrs %>%filter(year ==1992) %>%pull(hhidpn) %>%unique()hrs_filtered <- hrs %>%filter(hhidpn %in% ids_1992)# 2.b - Stacked bar chartggplot(filter(hrs_filtered, !is.na(srhealth)), aes(y ="", fill = srhealth)) +geom_bar(position =position_fill(reverse =TRUE), width = .5) +labs(x ="Self-rated health", y =NULL, fill ="") +scale_fill_viridis_d(option ="cividis", direction =-1, end = .95) +theme(legend.position ="bottom")
Historical example: W.E.B DuBois
*Read more about W.E.B. DuBois’s pioneering data visualizations here (and a whole book here)
// 2.c - Stacked bar chart with 1992 cohort: ordinal colors from cleanplots5 schemegraphbar (count) if in1992, ///over(srhealth) over(year) /// asyvars stack percentages ///ytitle("Self-rated health") ///title("[2.c] Stacked bar chart of self-rated health among older adults over time (HRS)") ///scheme(cleanplots5)
# 2.c - Stacked bar chart with 1992 cohortggplot(filter(hrs_filtered, !is.na(srhealth)), aes(fill = srhealth, x = year)) +geom_bar(position ="fill") +labs(title ="[2.c] Stacked bar chart of self-rated health among older adults over time (HRS)",y ="Self-rated health", fill ="") +scale_fill_viridis_d(option ="cividis", direction =-1, end = .95)
Published stacked bar chart example
Bar charts
The findings from the visual perception experiments pioneered by Cleveland and colleagues lead to some straightforward conclusions about effective graphics:
Angles are difficult to perceive accurately
Pie charts are not effective
Area is difficult to judge—especially when multiple aspects are varying at once (e.g., both length and width)
A common scale is necessary to compare across groups
Bar charts—where the width stays the same and only the length varies based on a quantitative metric—are among the most effective tools for comparing quantities
// 2.d - Bar chart for distribution of days drinkinggraphbar (count), ///over(dayweekdrink) ///title("[2.d] Bar chart for days usually drink each week (older adults, HRS)") ///ytitle("Count") b1title("Days usually drink each week")
# 2.d - Bar chart for distribution of days drinkingggplot(filter(hrs, !is.na(dayweekdrink)), aes(x = dayweekdrink)) +geom_bar() +labs(title ="[2.d] Bar chart for days usually drink each week (older adults, HRS)",x ="Days usually drink each week", y ="Count")