Totally just a mashup of “tibbletime: What’s It Used For?” by Matt Dancho at Business Science, and Daniel P. Hadley’s “How to Add a Logo to ggplot by Magick”. I don’t know who bert is. I wish I did. Wherever you are, Bert, your MC Hammer gif is awesome!
library(tibbletime)
library(tidyquant)
# Stock Prices from Yahoo! Finance
stocks <- c("INTC", "AMD", "NVDA", "MSFT")
stock_tbl <- stocks %>%
tq_get(get = "stock.prices", from = "2017-01-01", to = "2018-01-04")
stock_tbl <- stock_tbl %>%
group_by(symbol)
stock_tbl
## # A tibble: 1,012 x 8
## # Groups: symbol [4]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 INTC 2017-01-03 36.6 36.9 36.3 36.6 20196500 35.6
## 2 INTC 2017-01-04 36.7 36.8 36.3 36.4 15915700 35.4
## 3 INTC 2017-01-05 36.5 36.7 36.3 36.3 13986000 35.3
## 4 INTC 2017-01-06 36.6 36.7 36.2 36.5 15114000 35.5
## 5 INTC 2017-01-09 36.5 36.9 36.5 36.6 19449400 35.6
## 6 INTC 2017-01-10 36.5 36.9 36.5 36.5 15918800 35.5
## 7 INTC 2017-01-11 36.5 37.0 36.5 37.0 22398400 35.9
## 8 INTC 2017-01-12 36.8 36.8 36.3 36.7 20391900 35.7
## 9 INTC 2017-01-13 36.7 36.9 36.6 36.8 15072200 35.8
## 10 INTC 2017-01-17 36.7 36.8 36.6 36.8 20163000 35.8
## # ... with 1,002 more rows
# Setup plotting function that can be reused later
ggplot_facet_by_symbol <- function(data, x, y, group = NULL) {
# Setup expressions
x_expr <- rlang::enquo(x)
y_expr <- rlang::enquo(y)
group_expr <- rlang::enquo(group)
if (group_expr == ~NULL) {
# No groups
g <- data %>%
ggplot(aes(x = rlang::eval_tidy(rlang::`!!`(x_expr)),
y = rlang::eval_tidy(rlang::`!!`(y_expr)),
color = symbol)) +
labs(x = quo_name(x_expr),
y = quo_name(y_expr))
} else {
# Deal with groups
g <- data %>%
ggplot(aes(x = rlang::eval_tidy(rlang::`!!`(x_expr)),
y = rlang::eval_tidy(rlang::`!!`(y_expr)),
color = symbol,
group = rlang::eval_tidy(rlang::`!!`(group_expr))
)
) +
labs(x = quo_name(x_expr),
y = quo_name(y_expr),
group = quo_name(group_expr))
}
# Add faceting and theme
g <- g +
geom_line() +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_color_tq() +
theme_tq()
return(g)
}
# Plot adjusted vs date
stock_tbl %>%
ggplot_facet_by_symbol(date, adjusted) +
labs(title = "Adjusted Prices 2017 through 2018")
library(here)
library(magick)
background <- image_read("https://i.imgur.com/cFRL127.png")
logo_raw <- image_read("https://i.imgur.com/oJDChqu.gif")
frames <- lapply(logo_raw, function(frame) {
image_composite(background, frame, offset = "+100+400")
})
animation <- image_animate(image_join(frames))
image_write(animation, here::here("hammertibbletime.gif"))