Healy Chapter 4 Part 3

Harold Nelson

2022-09-26

Setup

library(socviz)
library(gapminder)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Look at the graphics of Age, Sex, Race, and Childs

p <- ggplot(data = gss_sm,
            mapping = aes(x = age, y = childs))
p + geom_point(alpha = 0.2) +
    geom_smooth() +
    facet_grid(sex ~ race)
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 18 rows containing non-finite values (stat_smooth).
## Warning: Removed 18 rows containing missing values (geom_point).

Agegrp

Look at this variable in the gss_sm dataframe.

summary(gss_sm$agegrp)
## Age 18-35 Age 35-45 Age 45-55 Age 55-65   Age 65+      NA's 
##       786       452       510       529       573        17

We can use this as a categorical variable to investigate these relationships.

Make use of this variable (or not) to get a different visualization. See what you can do.

One solution

p = gss_sm %>% 
    filter(agegrp == "Age 35-45") %>% 
    na.omit() %>% 
  ggplot(aes(x = childs)) + 
  geom_density(aes(color = race),adjust = .3)
p

g = gss_sm %>% 
  na.omit() %>% 
  ggplot((aes(x= childs))) + geom_density(adjust = .2) 
g

Another Solution

g + facet_grid(race~agegrp)

## Free the y scale.

g + facet_grid(race~agegrp, scales = "free_y")

## Filter to 35-45

g = gss_sm %>% 
  filter(age >= 35 & age < 45) %>% 
  na.omit() %>% 
  ggplot((aes(x= childs))) + geom_density(adjust = .2) 
g

## Facet by Race

g + facet_wrap(~race,ncol = 1)

## Filter for 65 +

g = gss_sm %>% 
  filter(age >= 65 ) %>% 
  na.omit() %>% 
  ggplot((aes(x= childs))) + geom_density(adjust = .2) 
g