Let's take some artificial plot created by ggplot
:
ggplot()+aes(x = 1:100, y = 1:100) + geom_line()
And also let's consider candlestick plot created by chartSeries
:
start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
# Apple stock
getSymbols("AAPL", src = "yahoo", from = start, to = end)
chartSeries(AAPL)
My question is : How can I plot them side-by-side - something what function plot_grid()
from cowplot
package does for ggplot
objects. I checked and plot_grid()
will not work to plot them side-by-side due to fact that chartSeries
is not a ggplot
object. So is there any way how can I plot both them next to each other ?
The result of chartSeries
is a chob. This can not be transformed by cowplot to a grob.
To get what you want you could use the functions from tidyquant. This returns the stockdata as a tibble
/ data.frame
and it has some functions to plot everything in ggplot2. See the vignette for more options. Then you can combine everything with the cowplot or patchwork package. I only show the result from patchwork. But the cowplot looks the same minus the title and captions.
library(tidyquant)
library(ggplot2)
aapl <- tq_get("AAPL", from = start, to = end)
aapl_plot <- aapl %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close))
g <- ggplot() +aes(x = 1:100, y = 1:100) + geom_line()
#combine with cowplot
cowplot::plot_grid(g, aapl_plot)
# combine with patchwork
library(patchwork)
g + aapl_plot + plot_annotation('This is a title', caption = 'made with patchwork')