Create a new chunk and include setup code from instructions here.
This is Chunk 1.
Code
```{r}#|label: setup# this line specifies options for default options for all R Chunksknitr::opts_chunk$set(echo=T, highlight=T)# suppress scientific notationoptions(scipen=100)# install helper package (pacman) if neededif (!require("pacman")) install.packages("pacman", repos = "http://lib.stat.cmu.edu/R/CRAN/")```
Loading required package: pacman
Code
```{r}# install and load required packages# pacman should be first package in parentheses and then list otherspacman::p_load(pacman, tidyverse, gridExtra, magrittr, ggthemes)# verify packages (comment out in finished documents)p_loaded()```
```{r}#|label: create and save plot(exp_imp_lineplot <- exp_imp_plt |> # replace blank with name of dataset ggplot() + geom_line(aes(x = date, y = value, color = type), linewidth = 1 ) +# can opt for different theme theme_classic() + # moves legend to bottom theme(legend.position="bottom") + # specifies colors (many other options vailable) scale_color_manual(values=c("lightblue","blue")) + # formats x axis to show each year as 4 digits scale_x_date(date_breaks = "year", date_labels = "%Y") + labs(x = "Date", y = "Index", color = "", title = "Import and Export Price Indices", subtitle = "January 2014 - June 2024" , caption = "Data Source - www.bls.gov" ))```
Source Code
---title: "HW 4"author: "Ava Graf"subtitle: "Due 10/11/2024"date: last-modifiedtoc: truetoc-depth: 3toc-location: lefttoc-title: "Table of Contents"toc-expand: 1format: html: code-line-numbers: true code-fold: true code-tools: trueexecute: echo: fenced ---## Setup- Create a new chunk and include setup code from instructions here.- This is Chunk 1.```{r}#|label: setup# this line specifies options for default options for all R Chunksknitr::opts_chunk$set(echo=T, highlight=T)# suppress scientific notationoptions(scipen=100)# install helper package (pacman) if neededif (!require("pacman")) install.packages("pacman", repos ="http://lib.stat.cmu.edu/R/CRAN/")# install and load required packages# pacman should be first package in parentheses and then list otherspacman::p_load(pacman, tidyverse, gridExtra, magrittr, ggthemes)# verify packages (comment out in finished documents)p_loaded()```## HW 4 - Part 1### Chunk 2: Create `bls_tidy` function from provided code.```{r}#|label: create bls_tidy function# import and clean bls dataunemp_orig <-read_csv("data/bls_unemp_rate.csv", skip =11, show_col_types = F) |>pivot_longer(cols = Jan:Dec, # reshapes datanames_to ="month", values_to ="value") |>filter(!is.na(value)) |># removes missing valuesrename("unemp_rate"="value") # renames quantitative var bls_tidy <-function(data_file, skip_num, var_name){read_csv(data_file, skip = skip_num, show_col_types = F) |>pivot_longer(cols = Jan:Dec, # reshapes datanames_to ="month", values_to ="value") |>filter(!is.na(value)) |># removes missing valuesrename({{var_name}} :="value")}unemp <-bls_tidy(data_file="data/bls_unemp_rate.csv", skip_num=11, var_name="unemp_rate")```## HW 4 - Part 2### Chunk 3: Use function to import two datasets.```{r}#|label: import export_index and import_index datasetsexport_index <-bls_tidy(data_file="data/bls_export_index.csv",skip_num =10,var_name ="exp_indx")import_index <-bls_tidy(data_file="data/bls_import_index.csv",skip_num =10, var_name ="imp_indx")summary(export_index$exp_indx)summary(import_index$imp_indx)```## HW 4 - Part 3### Chunk 4: Join datasets and create date variable.```{r}#|label: join datasets and create date variableexport_import <-full_join(export_index, import_index) |>mutate(date =ym(paste(Year, month)))```## HW 4 - Part 4### Chunk 5: Reshape, format and plot data```{r}#|label: reshape and format plot dataexp_imp_plt <- export_import |>select(date, exp_indx, imp_indx) |>pivot_longer(cols = exp_indx:imp_indx, names_to ="type", values_to ="value") |>mutate(type=factor(type,levels =c("exp_indx", "imp_indx"),labels =c("Export", "Import"))) |>glimpse()```## HW 4 - Part 5### Chunk 6: Create Formatted Plot```{r}#|label: create and save plot(exp_imp_lineplot <- exp_imp_plt |># replace blank with name of datasetggplot() +geom_line(aes(x = date, y = value, color = type), linewidth =1 ) +# can opt for different themetheme_classic() +# moves legend to bottomtheme(legend.position="bottom") +# specifies colors (many other options vailable)scale_color_manual(values=c("lightblue","blue")) +# formats x axis to show each year as 4 digits scale_x_date(date_breaks ="year", date_labels ="%Y") +labs(x ="Date", y ="Index", color ="",title ="Import and Export Price Indices", subtitle ="January 2014 - June 2024" ,caption ="Data Source - www.bls.gov" ))```