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
# General Social Survey Data
gss <- droplevels(read.dta("https://tdmize.github.io/data/data/gss.dta"))
# 5.a - Overlay multiple densities for different groups
# Use both colors and line patterns so the five groups are easy to tell apart
degree_labels <- c("< High School", "HS Degree", "Junior College",
"College Degree", "Grad Degree")
ggplot(filter(gss, !is.na(degree)), aes(x = wages, color = degree, linetype = degree)) +
geom_density(key_glyph = "path") +
labs(x = "Hourly Wages", y = "Density", color = "", linetype = "",
title = "[5.a] Kernel-Density Plots of Hourly Wages Based on Educational Attainment",
subtitle = "Densities are group specific") +
scale_color_cleanplots(labels = degree_labels) +
scale_linetype_cleanplots(labels = degree_labels) +
scale_x_continuous(breaks = seq(0, 70, 10))![[5.a] Kernel density plots of hourly wages for five levels of education, each with its own color and line pattern: the less than high school curve peaks highest and earliest, and each higher degree peaks lower and farther to the right, with the graduate degree curve flattest.](5-groups_files/figure-html/unnamed-chunk-1-1.png)

![[5.a] Kernel density plots of hourly wages for five levels of education, each with its own color and line pattern: the less than high school curve peaks highest and earliest (near 7 dollars), and each higher degree peaks lower and farther to the right, with the graduate degree curve flattest and peaking near 20 dollars.](fig/dmv-05_a-kdensity_groups.png)
![[5.b] Mean cognitive test scores with 95% confidence intervals for men (red) and women (light blue) in each HRS wave from 1996 to 2020: scores decline over time for both, from about 16 to 13.5 for men and from about 17 to 14.5 for women, with women higher in every wave.](fig/dmv-05_b-dot_cog_gender.png)
![[5.b] Mean cognitive test scores with 95% confidence intervals for men and women in each HRS wave from 1996 to 2020: scores decline over time for both, with women higher in every wave.](5-groups_files/figure-html/unnamed-chunk-2-1.png)

![[5.c] Mean cognitive test scores with 95% confidence intervals for eight education groups in each wave from 1996 to 2020, each group with its own color and marker shape: scores decline over time in every group, and groups with more education score higher: the no degree group falls from about 13.5 to about 11, while the law/md/phd group stays highest, between about 17 and 19.5.](fig/dmv-05_c-dot_cog_educ.png)
![[5.c] Mean cognitive test scores with 95% confidence intervals for eight education groups in each wave from 1996 to 2020, each group with its own color and marker shape: scores decline over time in every group, and groups with more education score higher.](5-groups_files/figure-html/unnamed-chunk-3-1.png)
![[5.d] Scatterplot of mean warmth by mean competence stereotypes for 26 gender by racial-ethnic groups, each group a different color with a 26-entry legend: the colors are too similar to match the points to the groups.](fig/dmv-05_d-scatter_colors.png)
![[5.d] Scatterplot of mean warmth by mean competence stereotypes for 26 gender by racial-ethnic groups, each group a different color with a 26-entry legend: the colors are too similar to match the points to the groups.](5-groups_files/figure-html/unnamed-chunk-4-1.png)
![[5.e] The same scatterplot with each point labeled directly with its group name: White Men and Asian Men are rated most competent, Pakistani, Iranian, Saudi Arabian, and Middle Eastern Women least competent, and the Middle Eastern, Saudi Arabian, Pakistani, and Iranian Men least warm.](fig/dmv-05_e-scatter_labels.png)
![[5.e] The same scatterplot with each point labeled directly with its group name: White Men and Asian Men are rated most competent, Pakistani, Iranian, Saudi Arabian, and Middle Eastern Women least competent, and the Middle Eastern, Saudi Arabian, Pakistani, and Iranian Men least warm.](5-groups_files/figure-html/unnamed-chunk-5-1.png)



![[5.f] Bar chart of the average count of chronic diseases for eight education groups in ordinal viridis colors, from light yellow for no degree to dark purple for law/md/phd: the average falls from about 2.25 with no degree to about 1.6 for the highest degrees.](fig/dmv-05_f-color_viridis.png)
![[5.f] Bar chart of the average count of chronic diseases for eight education groups in ordinal viridis colors, from light yellow for no degree to dark purple for law/md/phd, with 95% confidence intervals: the average falls as education rises.](5-groups_files/figure-html/unnamed-chunk-6-1.png)