library(tidyverse)
library(ggplot2)
library(dplyr)
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 6
challenge_6
hotel_bookings
air_bnb
fed_rate
debt
usa_households
abc_poll
Visualizing Time and Relationships
Challenge Overview
Today’s challenge is to:
- read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data (as needed, including sanity checks)
- mutate variables as needed (including sanity checks)
- create at least one graph including time (evolution)
- try to make them “publication” ready (optional)
- Explain why you choose the specific graph type
- Create at least one graph depicting part-whole or flow relationships
- try to make them “publication” ready (optional)
- Explain why you choose the specific graph type
R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code.
(be sure to only include the category tags for the data you use!)
Read in data
Read in one (or more) of the following datasets, using the correct R package and command.
- debt ⭐
- fed_rate ⭐⭐
- abc_poll ⭐⭐⭐
- usa_hh ⭐⭐⭐
- hotel_bookings ⭐⭐⭐⭐
- AB_NYC ⭐⭐⭐⭐⭐
<-read.csv("_data/FedFundsRate.csv") fed_rate
### Briefly describe the data
The data appears to show a series of rates and changes in the economy as evaluated by the federal reserve on different dates between 1/7/1954 and 3/16/2017. Not all dates between these two points are accounted for, and there are multiple entries for every year. It appears that no federal data was collected for multiple of the observed dates, and there are 3196 NA signifiers (checked using "sum(is.na(fed_rate))") in the dataset as a result.
## Tidy Data (as needed)
I think this dataset's year, month, and day columns could be consolidated into one, so I'll do that below.
::: {.cell}
```{.r .cell-code}
fed_rate$Date<-as.Date(with(fed_rate, paste(Year,Month,Day,sep="-")),"%Y-%m-%d")
fed_rate<-fed_rate[-c(1:3)]
view(fed_rate)
:::
Are there any variables that require mutation to be usable in your analysis stream? For example, do you need to calculate new values in order to graph them? Can string values be represented numerically? Do you need to turn any variables into factors and reorder for ease of graphics and visualization?
Document your work here.
<-fed_rate%>%
fed_ratepivot_longer(cols=-Date,
names_to = "variable",
values_to = "value")
colnames(fed_rate)[2] ="Variable"
colnames(fed_rate)[3] ="Value"
glimpse(fed_rate)
Rows: 6,328
Columns: 3
$ Date <date> 1954-07-01, 1954-07-01, 1954-07-01, 1954-07-01, 1954-07-01, …
$ Variable <chr> "Federal.Funds.Target.Rate", "Federal.Funds.Upper.Target", "F…
$ Value <dbl> NA, NA, NA, 0.80, 4.60, 5.80, NA, NA, NA, NA, 1.22, NA, 6.00,…
fed_rate
# A tibble: 6,328 × 3
Date Variable Value
<date> <chr> <dbl>
1 1954-07-01 Federal.Funds.Target.Rate NA
2 1954-07-01 Federal.Funds.Upper.Target NA
3 1954-07-01 Federal.Funds.Lower.Target NA
4 1954-07-01 Effective.Federal.Funds.Rate 0.8
5 1954-07-01 Real.GDP..Percent.Change. 4.6
6 1954-07-01 Unemployment.Rate 5.8
7 1954-07-01 Inflation.Rate NA
8 1954-08-01 Federal.Funds.Target.Rate NA
9 1954-08-01 Federal.Funds.Upper.Target NA
10 1954-08-01 Federal.Funds.Lower.Target NA
# ℹ 6,318 more rows
Time Dependent Visualization
The following is the federal rates displayed over time.
%>%
fed_ratefilter(str_detect(Variable, "Federal"))%>%
ggplot(., aes(x = Date, y = Value, color = Variable)) +
geom_line() +
scale_y_continuous(labels = scales::label_percent(scale = 0.5))+
labs(title = "Federal Reserve Rates", x = "Date", y = "Rate")
Visualizing Part-Whole Relationships
ggplot(fed_rate, aes(x = Date, y = Value, fill = Variable))+
geom_area()+
labs(title = "Federal Reserve Rates", x = "Year", y = "Rate")