This seems like a simple problem. I want to use an existing character vector in a ggplot subtitle (rather than hard-coding values), preferably with a "+" in between.
Is this what you mean?
library(ggplot2) text <- "Any text you want" ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() + labs(title = "Some Title", subtitle = text)
Created on 2020-03-06 by the reprex package (v0.3.0.9001)
If this is not what you want, please provide a proper REPRoducible EXample (reprex) illustrating your issue.
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, 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)
You shouldn't include your entire script, a reprex has to be minimal, only including the relevant parts, I think this is what you are trying to do.
library(ggplot2) # This represents the ouput of your code my_subtitle <- c("GGG", "ARU", "ALK", "NTU", "LYC", "PM8", "PEK", "HAS") ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_line() + labs(title = "Total volume per day (log scale)", subtitle = paste(my_subtitle, collapse = " + ")) # This is the solution
Thanks, collapse is the one.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.