rquantmod

How to make the bottom graph bigger chart_Series quantmod


How to make the bottom graph bigger?

library(quantmod)
p1 <- rnorm(4000) |> cumsum() |> xts(Sys.time()+1:4000) |> to.minutes(name = NULL) |> round(0)+100

chart_Series(p1)
add_Series(p1, on = NA)

enter image description here


Solution

  • If it's not possible with quantmod directly, you could instead consider using ggplot and patchwork per below. Both give a high degree of control over the plot aesthetics, e.g. setting the height relative proportions with heights = c(1, 1).

    library(tidyquant)
    library(tidyverse)
    library(quantmod)
    library(patchwork)
    
    xt <- rnorm(4000) |>
      cumsum() |>
      xts(Sys.time() + 1:4000) |>
      to.minutes(name = NULL) |>
      round(0) + 100
    
    df <- xt |> 
      as_tibble(rownames = "date") |> 
      mutate(date = as_datetime(date))
    
    p1 <- df |> 
      ggplot(aes(x = date, y = Close)) +
      geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) +
      scale_x_datetime(date_breaks = "15 mins", date_labels = "%b %d\n%H:%M") +
      theme_bw() +
      labs(x = NULL)
    
    p2 <- df |> 
      ggplot(aes(x = date, y = Close)) +
      geom_candlestick(aes(open = Open, high = High, low = Low, close = Close)) +
      scale_x_datetime(date_breaks = "15 mins", date_labels = "%b %d\n%H:%M") +
      theme_bw() +
      labs(x = NULL)
    
    p1 / p2 + plot_layout(axes = "collect", heights = c(1, 1))
    

    Created on 2024-03-22 with reprex v2.1.0