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")
# 10.a - Basic scatterplot doesn't work because of overlapping data points
ggplot(hrs, aes(x = age, y = dzcount)) +
geom_point() +
labs(x = "Age",
y = "Number of chronic diseases",
title = "[10.a] Relationship between number of chronic diseases and age") +
scale_x_continuous(breaks = seq(20, 100, 10))![[10.a] Scatterplot of the number of chronic diseases (0 to 8) by age: because both variables take whole-number values, the points overlap into solid horizontal rows, and the graph cannot show where most of the data are.](10-linearity_files/figure-html/unnamed-chunk-1-1.png)

![[10.a] Scatterplot of the number of chronic diseases (0 to 8) by age: because both variables take whole-number values, the points overlap into solid horizontal rows, and the graph cannot show where most of the data are.](fig/dmv-10_a-scatterplot.png)
![[10.b] The same scatterplot with random noise added to each point: the rows become thick bands, showing that most observations are between ages 50 and 95, but with so many points the bands are still solid.](fig/dmv-10_b-scatterplot-jitter.png)
![[10.b] The same scatterplot with random noise added to each point: the rows become thick bands, showing that most observations are between ages 50 and 95, but with so many points the bands are still solid.](10-linearity_files/figure-html/unnamed-chunk-2-1.png)
![[10.c] Binned scatterplot with one point for the mean number of chronic diseases in each of 20 equal-sized age groups: the mean rises steeply from about 1 at age 47 to about 2.3 at age 70 and then levels off near 2.6 after age 80.](fig/dmv-10_c-binscatter.png)
![[10.c] Binned scatterplot with one point for the mean number of chronic diseases in each of 20 equal-sized age groups: the mean rises steeply from about 1 near age 47 and then levels off after age 80.](10-linearity_files/figure-html/unnamed-chunk-3-1.png)
![[10.d] The jittered scatterplot in light gray with a red linear regression line that rises steadily with age across the whole range.](fig/dmv-10_d-scatterplot-linear_fit.png)
![[10.d] The jittered scatterplot in light gray with a linear regression line that rises steadily with age across the whole range.](10-linearity_files/figure-html/unnamed-chunk-4-1.png)
![[10.e] The jittered scatterplot with a red quadratic regression line that rises with age and flattens out at the oldest ages.](fig/dmv-10_e-scatterplot-quadratic_fit.png)
![[10.e] The jittered scatterplot with a quadratic regression line that rises with age and flattens out at the oldest ages.](10-linearity_files/figure-html/unnamed-chunk-5-1.png)
![[10.f] Jittered scatterplot of chronic diseases by age for the 2022 wave with a red lowess line: the line is nearly flat around 1 until about age 45, rises between 45 and 80, and levels off near 3 after age 80.](fig/dmv-10_f-scatterplot-lowess_fit.png)
![[10.f] Jittered scatterplot of chronic diseases by age for the 2022 wave with a lowess line: the line is nearly flat until about age 45, rises between 45 and 80, and levels off after age 80.](10-linearity_files/figure-html/unnamed-chunk-6-1.png)


