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:
colnames(df) <- gsub("\\.", " ", colnames(df))
# Remove abbreviations, uppercase letters possibly followed by spaces
colnames(df) <- gsub(" [A-Z]{1,3} +", "", colnames(df))
# Trimming any leading or trailing spaces
colnames(df) <- trimws(colnames(df))
print(colnames(df))
## [1] "State FIPS code"
## [2] "Date"
## [3] "All merchant category codes spending"
## [4] "Accommodation and food servicespending"
## [5] "Arts entertainment and recreationspending"
## [6] "General merchandise storesand apparel and accessoriesspending"
## [7] "Grocery and food storespending"
## [8] "Health care and social assistancespending"
## [9] "Transportation and warehousingspending"
## [10] "Retail spending including grocery CECGRFETC"
## [11] "Retail spending excluding grocery CECHICSGH"
# Convert Date column to date format
data <- df %>%
mutate(Date = mdy(Date))
avg_data <- data %>%
group_by(Date) %>%
summarize(across(
contains("spending"),
mean,
na.rm = TRUE
))
## Warning: There was 1 warning in `summarize()`.
## ℹ In argument: `across(contains("spending"), mean, na.rm = TRUE)`.
## ℹ In group 1: `Date = 2020-01-13`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))
long_data <- avg_data %>%
pivot_longer(
cols = -Date,
names_to = "Category",
values_to = "Percent_Change"
)
long_data$Category <- str_replace_all(long_data$Category, "\\s+spending", "")
plot1 <- ggplot(long_data, aes(x = Date, y = Percent_Change, color = Category)) +
geom_line(size = 0.75) +
labs(
title = "Percent Change in Consumer Spending Over Time",
x = "Date",
y = "Percent Change",
) +
theme_minimal() +
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "top",
legend.justification = c(0, 0.1),
legend.box = "vertical",
legend.text = element_text(size = 7),
legend.title = element_text(size = 7)
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Make the line graph interactive
interactive_line_graph <- ggplotly(plot1) %>%
layout(width = 1000)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Display the interactive line graph
interactive_line_graph
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
colnames(df_ur) <- c("Date", "Unemployment_Rate")
plot2 <- ggplot(df_ur, aes(x = Date, y = Unemployment_Rate, fill = Unemployment_Rate)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Make the line graph interactive
interactive_line_graph2 <- ggplotly(plot2) %>%
layout(width = 1000)
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Display the interactive line graph
interactive_line_graph2