Sorry, please see reprex. Instead of the existing hard coded subtitle, I'm trying to work out how to make this dynamic with the character vector called my_subtitle:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
library(purrr)
library(tidyquant)
#> Loading required package: PerformanceAnalytics
#> Loading required package: xts
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#> Registered S3 method overwritten by 'xts':
#> method from
#> as.zoo.xts zoo
#>
#> Attaching package: 'xts'
#> The following objects are masked from 'package:dplyr':
#>
#> first, last
#>
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#>
#> legend
#> Loading required package: quantmod
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
#> Version 0.4-0 included new data defaults. See ?getSymbols.
options(scipen=999)
end <- Sys.Date()
ree <- c("GGG.AX", "ARU.AX", "ALK.AX", "NTU.AX", "LYC.AX", "PM8.AX",
"PEK.AX", "HAS.AX")
stock_prices <- tq_get(ree,
get = "stock.prices",
from = "2007-01-01",
to = end)
#> Warning: `cols` is now required.
#> Please use `cols = c(stock.prices)`
# Set correct time zone
stock_prices$date <- as_date(stock_prices$date, tz="Australia/Brisbane")
# Set colours for some plots
my_colours <- c("#E69F00", "#56B4E9", "#009E73", "#CC79A7", "#D55E00",
"#1482EE", "#EEC900", "#686868")
# Remove country code
stock_prices$symbol <- gsub("\\..*", "", stock_prices$symbol)
# Get unique names for subtitle
my_subtitle <- unique(stock_prices$symbol)
# Plot
wide_vol <- stock_prices %>%
select(symbol, volume, date) %>%
distinct(symbol, date, .keep_all = TRUE) %>%
spread(symbol, volume) %>%
mutate(total_vol = rowSums(.[2:9])) %>%
filter(date >= as.Date("2012-01-01") & date <= end)
# Play with wide format
wide_vol %>%
filter(date >= as_date("2018-01-01")) %>%
ggplot(aes(x = date, y = total_vol)) +
geom_line(aes(), alpha = 0.5, size = 1) +
scale_y_log10(labels = scales::comma) +
scale_x_date(date_breaks = "6 month", date_labels = "%b %Y") +
geom_smooth(method = 'auto', colour = my_colours[2], linetype = "dashed") +
labs(x = "Date", y = "Volume",
title = "Total volume per day (log scale)",
subtitle = "GGG + ARU + ALK + NTU + LYC + PM8 + PEK + HAS",
# subtitle = map_chr(my_subtitle, paste0, ""),
caption = "Source: Yahoo Finance") +
theme_light() +
theme(axis.text.x = element_text(angle = 0, hjust = 1), legend.position = "none")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> Warning: Removed 3 rows containing non-finite values (stat_smooth).

Created on 2020-03-07 by the reprex package (v0.3.0)