-
Notifications
You must be signed in to change notification settings - Fork 0
/
barplots.Rmd
102 lines (75 loc) · 3.31 KB
/
barplots.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: "How to create barplots"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{How to create barplots}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
This section will demo how to create a barplot in _fedplot style_, using Figure 2.4 of the [November 2022 FSR](https://www.federalreserve.gov/publications/files/financial-stability-report-20221104.pdf) as a reference.
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## Example barplot
First, we load the required packages(`ggplot2` and `fedplot`, plus `dplyr` and `scales`). Note that the sample dataset `FSR_2_4` is part of the `fedplot` package.
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r, include= TRUE, echo = TRUE}
#devtools::load_all()
library(ggplot2)
library(dplyr, warn.conflict=FALSE)
library(fedplot)
library(scales)
packageVersion("fedplot")
head(FSR_2_4)
```
We can construct the barplot using standard `ggplot2` functions:
```{r, include = TRUE, echo = TRUE}
# Caption disabled for FSR; enabled otherwise through labs()
caption <- "Source: Mergent, Fixed Income Securities Database; PitchBook Data, Leveraged Commentary & Data."
# If we don't want the default alphabetical order, we can do use factor() and a) use relevel() afterwards or specify the levels option.
levels <- c("Institutional leveraged loans", "High-yield and unrated bonds")
FSR_2_4 |>
dplyr::mutate(type = factor(risky_debt_type, levels=levels)) |>
ggplot(aes(x = date, y = value, fill=type)) +
geom_col() +
labs(y="Billions of dollars") # , caption=caption)
```
Now we customize it:
```{r, include = TRUE, echo = TRUE}
linewidth <- getOption("fedplot.linewidth_adj") * 0.25
levels <- c("Institutional leveraged loans", "High-yield and unrated bonds")
FSR_2_4 |>
dplyr::mutate(type = factor(risky_debt_type, levels=levels)) |>
ggplot(aes(x = date, y = value, fill=type)) +
geom_col(color="black", linewidth=linewidth, width=60, key_glyph="square") + # Width is in days
labs(y="Billions of dollars") +
geom_hline_zero() +
scale_x_date(minor_breaks=seq(from=as.Date("2003-01-01"), to=as.Date("2023-01-01"), by="1 years"),
breaks=seq(from=as.Date("2004-06-30"), to=as.Date("2022-06-30"), by="3 years"),
date_labels="%Y",
expand=expansion(mult=.05)) +
scale_y_continuous(sec.axis = dup_axis(),
breaks = seq(-60, 120, by=20),
limits = c(-60, 120),
expand = expansion(mult=0),
labels = scales::label_number(style_negative = "minus")) +
annotate_last_date(nudge_y = 15) +
theme_fed(legend_position = c(.24, .95),
fill_palette=c("#236192", "#DDCBA4"),
size='wide')
```
Lastly, we want to export the chart so it matches the required image characteristics:
```{r, include = TRUE, echo = TRUE}
save_plot('barplot', extension='all')
```
After exporting through `save_plot`, the chart looks like this:
![](barplot.png)
## Pending tasks:
1. Is there a rule for the bar width? Note that it's set in terms of _days_ and not in an absolute size.
1. Add a `geom_col_fed` function that automatically sets the required color (black) and linewidth (0.25 times adjustment).
1. Automatically use the required color fill palette (which needs to be determined).