This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
Sample Excel Data (Birth_Rate_USA.xlsx) ### R Markdown File: Final Project
title: Final Project: Birth Rate Analysis in the USA author: Otis Hong output: html_document —
{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(readxl) library(ggplot2) library(gganimate) library(plotly)
Introduction
This report presents an analysis of birth rates in the USA from 2000 to 2022. It includes a series of visualizations that explore trends, distributions, and comparisons across different years, GDP per capita, unemployment rate, and population.
Importing the Data
{r data_import} # Importing the birth rate data from the Excel file dat <- read_excel(“Birth_Rate_USA.xlsx”) dat <- tibble(dat)
Visualization 1: Line Plot of Birth Rate Over Years
{r line_plot, echo=FALSE, message=FALSE, warning=FALSE} ggplot(dat, aes(x = Year, y = Birth_Rate)) + geom_line(color = “blue”) + geom_point() + labs(title = “Birth Rate in the USA (2000-2022)”, x = “Year”, y = “Birth Rate (per 1000 people)”)
Visualization 2: Bar Plot of GDP per Capita Over Years
{r bar_plot, echo=FALSE, message=FALSE, warning=FALSE} ggplot(dat, aes(x = Year, y = GDP_per_Capita, fill = as.factor(Year))) + geom_bar(stat = “identity”) + labs(title = “GDP per Capita in the USA (2000-2022)”, x = “Year”, y = “GDP per Capita”) + scale_fill_discrete(name = “Year”)
Visualization 3: Histogram of Birth Rates
{r histogram, echo=FALSE, message=FALSE, warning=FALSE} ggplot(dat, aes(x = Birth_Rate)) + geom_histogram(binwidth = 0.5, fill = “green”, color = “black”) + labs(title = “Distribution of Birth Rates (2000-2022)”, x = “Birth Rate (per 1000 people)”, y = “Frequency”)
Visualization 4: Box Plot of Unemployment Rates by Decade
{r box_plot, echo=FALSE, message=FALSE, warning=FALSE} dat <- dat %>% mutate(Decade = ifelse(Year < 2010, “2000-2009”, “2010-2022”))
ggplot(dat, aes(x = Decade, y = Unemployment_Rate, fill = Decade)) + geom_boxplot() + labs(title = “Box Plot of Unemployment Rates by Decade”, x = “Decade”, y = “Unemployment Rate (%)”) + scale_fill_discrete(name = “Decade”)
Visualization 5: Scatter Plot with Trend Line of Birth Rate vs. GDP per Capita
{r scatter_plot, echo=FALSE, message=FALSE, warning=FALSE} ggplot(dat, aes(x = GDP_per_Capita, y = Birth_Rate)) + geom_point() + geom_smooth(method = “lm”, se = FALSE, color = “red”) + labs(title = “Scatter Plot of Birth Rate vs. GDP per Capita”, x = “GDP per Capita”, y = “Birth Rate (per 1000 people)”)
Visualization 6: Heatmap of Birth Rates Over Years
{r heatmap, echo=FALSE, message=FALSE, warning=FALSE} dat\(Year <- as.factor(dat\)Year)
ggplot(dat, aes(x = Year, y = “USA”, fill = Birth_Rate)) + geom_tile() + scale_fill_gradient(low = “white”, high = “red”) + labs(title = “Heatmap of Birth Rates (2000-2022)”, x = “Year”, y = ““, fill =”Birth Rate”)
Visualization 7: Interactive Plot using Plotly
{r interactive_plot, echo=FALSE, message=FALSE, warning=FALSE} p
<- ggplot(dat, aes(x = Year, y = Birth_Rate, text = paste(“Year:”,
Year, “
Birth Rate:”, Birth_Rate))) + geom_line(color = “blue”) +
geom_point() + labs(title = “Interactive Line Plot of Birth Rate”, x =
“Year”, y = “Birth Rate (per 1000 people)”)
ggplotly(p, tooltip = “text”)
Visualization 8: Animated Plot using gganimate
{r animated_plot, echo=FALSE, message=FALSE, warning=FALSE} p <- ggplot(dat, aes(x = Year, y = Birth_Rate)) + geom_line(color = “blue”) + geom_point() + labs(title = “Animated Line Plot of Birth Rate”, x = “Year”, y = “Birth Rate (per 1000 people)”)
p + transition_reveal(Year)
Conclusion
This report presented various visualizations to analyze the birth rate trends in the USA from 2000 to 2022. Each visualization provided a different perspective on the data, highlighting important patterns and insights.