This assignment builds on what you did in the previous assignment. It uses the dataframe you saved at the end.

It also requires you to submit your work by posting a document on RPubs. This will allow you to create interactive graphs.

Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)
load("by_state_0323.Rdata")
by_state_0323
## # A tibble: 5,400 × 6
##    State    Year Age     Fpop Births   Rate
##    <chr>   <dbl> <chr>  <dbl>  <dbl>  <dbl>
##  1 Alabama  2003 15-19 157477   8095 0.0514
##  2 Alabama  2004 15-19 159259   8126 0.0510
##  3 Alabama  2005 15-19 161701   7771 0.0481
##  4 Alabama  2006 15-19 164708   8537 0.0518
##  5 Alabama  2003 20-24 163919  18633 0.114 
##  6 Alabama  2004 20-24 163736  18581 0.113 
##  7 Alabama  2005 20-24 163672  19134 0.117 
##  8 Alabama  2006 20-24 162871  19766 0.121 
##  9 Alabama  2003 25-29 142041  15925 0.112 
## 10 Alabama  2004 25-29 143951  16212 0.113 
## # ℹ 5,390 more rows

Task 1

Make a graph showing the total number of births per year. Use plotly.

g = by_state_0323 %>%
  group_by(Year) %>%
  summarize(Births = sum(Births)) %>%
  ggplot(aes(x = Year, y = Births))+
  geom_point()+
  ggtitle("Births Per Year")

  
ggplotly(g)

Task 2

It is somewhat surprising that the total number of births in the US has declined. Has the number of women been declining in this time period?

Redo the previous dataframe and include the total number of women. Also compute the ratio of births to women. Print the head and tail of this dataframe so we can see all three variables.

# Place your code here

g = by_state_0323 %>%
  group_by(Year) %>%
  summarize(Births = sum(Births), Fpop = sum(Fpop)) %>%
  mutate(BPW = Births/Fpop)
head(g, n = 1)
## # A tibble: 1 × 4
##    Year  Births     Fpop    BPW
##   <dbl>   <dbl>    <dbl>  <dbl>
## 1  2003 4069873 61745355 0.0659
tail(g, n = 1)
## # A tibble: 1 × 4
##    Year  Births     Fpop    BPW
##   <dbl>   <dbl>    <dbl>  <dbl>
## 1  2020 3593850 64351519 0.0558

Task 3

The total fertility rate (TFR) is the number of births for a woman during her lifetime. The rate data we have is the number of births per woman per year while she is in one of the 5-year age groups.

How do we use the rate information to construct the TFR? We add up all of the rates for the individual age groups and multiply the sum by 5. Do this for the State of Washington. Plot the time series using geom_point() and plotly.

g = by_state_0323 %>%
  filter(State == 'Washington') %>%
  group_by(Year) %>%
  mutate(Rate = sum(Rate)) %>%
  mutate(TFR = Rate * 5) %>%
  ggplot(aes(x = Year, y = TFR))+
  geom_point()+
  scale_y_continuous(limits = c(0, 3.0))+
  ggtitle("Washington Total Fertility Rate by Year")

ggplotly(g)

Task 4

Repeat the exercise for all states. Again, use plotly so we will be able to identify states. In the aes(), add “group = State”. Also draw a red horizontal line at 2.1. Use the plotly framework I have set up.

Using the interactive graph, which states have the highest and lowest TFR values in 2003 and 2023?

g = by_state_0323 %>%
  group_by(Year, State) %>%
  mutate(Rate = sum(Rate)) %>%
  mutate(TFR = Rate * 5) %>%
  ggplot(aes(x = Year, y = TFR, group = State))+
  geom_point()+
  geom_point(aes(y = 2.1), color = "red")+
  scale_y_continuous(limits = c(0, 3.0))+
  ggtitle("By-State Fertility Rate Per Year")

ggplotly(g)

Task 5

Create a graph showing the TFR for the US as a whole. We can’t use the rate data directly. We need to compute the total numbers of births and total numbers of women for each age group and year. Then compute the TFR by adding the calculated rates and multiplying by 5. Plot this using plotly.

g = by_state_0323 %>%
  group_by(Year) %>%
  mutate(Births = sum(Births), Women = sum(women), Rate = sum(Rate)) %>%
  mutate(TFR = (Rate * 5)/50) %>%
  ggplot(aes(x = Year, y = TFR))+
  geom_point()+
  scale_y_continuous(limits = c(0, 3.0))+
  ggtitle("US Total Fertility Rate By Year")
  
ggplotly(g)