library(tidyverse)
library(RSQLite)
library(wesanderson)
<- dbConnect(
tidy_finance SQLite(),
"data/tidy_finance_r.sqlite",
extended_types = TRUE
)
<- tbl(
factors_ff3_daily
tidy_finance,"factors_ff3_daily"
|>
) collect()
<- factors_ff3_daily |>
data_plot select(date, mkt_excess) |>
group_by(year = floor_date(date, "year")) |>
mutate(group_id = cur_group_id())
<- data_plot |>
data_plot group_by(group_id) |>
mutate(
day = 2 * pi * (1:n()) / 252,
ymin = pmin(1 + mkt_excess, 1),
ymax = pmax(1 + mkt_excess, 1),
vola = sd(mkt_excess)
|>
) filter(year >= "1962-01-01" & year <= "2021-12-31")
<- data_plot |>
levels distinct(group_id, vola) |>
arrange(vola) |>
pull(vola)
<- coord_polar(
cp direction = -1,
clip = "on"
)
$is_free <- function() TRUE
cp<- wes_palette("Zissou1",
colors n_groups(data_plot),
type = "continuous"
)
<- data_plot |>
cover mutate(vola = factor(vola, levels = levels)) |>
ggplot(aes(
x = day,
y = mkt_excess,
group = group_id,
fill = vola
+
)) +
cp geom_ribbon(aes(
ymin = ymin,
ymax = ymax,
fill = vola
alpha = 0.90) +
), theme_void() +
facet_wrap(~group_id,
ncol = 10,
scales = "free"
+
) theme(
strip.text.x = element_blank(),
legend.position = "None",
panel.spacing = unit(-5, "lines")
+
) scale_fill_manual(values = colors)
ggsave(
plot = cover,
width = 10,
height = 6,
filename = "images/cover.png",
bg = "white"
)
Cover and Logo Design
The cover of the book is inspired by the fast growing generative art community in R. Generative art refers to art that in whole or in part has been created with the use of an autonomous system. Instead of creating random dynamics, we rely on what is core to the book: The evolution of financial markets. Each circle in the cover figure corresponds to daily market return within one year of our sample. Deviations from the circle line indicate positive or negative returns. The colors are determined by the standard deviation of market returns during the particular year. The few lines of code below replicate the entire figure. We use the Wes Andersen color palette (also throughout the entire book), provided by the package wesanderson
(Ram and Wickham 2018)
To generate our logo, we focus on year 2021 - the end of the sample period at the time we published tidy-finance.org for the first time.
<- data_plot |>
logo ungroup() |>
filter(year == "2021-01-01") |>
mutate(vola = factor(vola, levels = levels)) |>
ggplot(aes(
x = day,
y = mkt_excess,
fill = vola
+
)) +
cp geom_ribbon(aes(
ymin = ymin,
ymax = ymax,
fill = vola
alpha = 0.90) +
), theme_void() +
theme(
strip.text.x = element_blank(),
legend.position = "None",
plot.margin = unit(c(-0.15,-0.15,-0.15,-0.15), "null")
+
) scale_fill_manual(values = "white")
ggsave(
plot = logo,
width = 840,
height = 840,
units = "px",
filename = "images/logo-website-white.png",
)
ggsave(
plot = logo +
scale_fill_manual(values = wes_palette("Zissou1")[1]),
width = 840,
height = 840,
units = "px",
filename = "images/logo-website.png",
)
Here is the code to generate the vector graphics for our buttons.
<- data_plot |>
button_r ungroup() |>
filter(year == "2000-01-01") |>
mutate(vola = factor(vola, levels = levels)) |>
ggplot(aes(
x = day,
y = mkt_excess,
fill = vola
+
)) +
cp geom_ribbon(aes(
ymin = ymin,
ymax = ymax,
fill = vola
alpha = 0.90) +
), theme_void() +
theme(
strip.text.x = element_blank(),
legend.position = "None",
plot.margin = unit(c(-0.15,-0.15,-0.15,-0.15), "null")
)
ggsave(
plot = button_r +
scale_fill_manual(values = wes_palette("Zissou1")[1]),
width = 100,
height = 100,
units = "px",
filename = "images/button-r-blue.svg",
)
ggsave(
plot = button_r +
scale_fill_manual(values = wes_palette("Zissou1")[4]),
width = 100,
height = 100,
units = "px",
filename = "images/button-r-orange.svg",
)
<- data_plot |>
button_python ungroup() |>
filter(year == "1991-01-01") |>
mutate(vola = factor(vola, levels = levels)) |>
ggplot(aes(
x = day,
y = mkt_excess,
fill = vola
+
)) +
cp geom_ribbon(aes(
ymin = ymin,
ymax = ymax,
fill = vola
alpha = 0.90) +
), theme_void() +
theme(
strip.text.x = element_blank(),
legend.position = "None",
plot.margin = unit(c(-0.15,-0.15,-0.15,-0.15), "null")
)
ggsave(
plot = button_python +
scale_fill_manual(values = wes_palette("Zissou1")[1]),
width = 100,
height = 100,
units = "px",
filename = "images/button-python-blue.svg",
)
ggsave(
plot = button_python +
scale_fill_manual(values = wes_palette("Zissou1")[4]),
width = 100,
height = 100,
units = "px",
filename = "images/button-python-orange.svg",
)