rhighchartsr-highcharter

Trying to add a graph series in a separate panel to highcharter


I am trying to add a MACD graph section to the highcharter graph similar to volume section. I have the code for creating same. Not sure as how they will be added as separate section similar to volume. An image as an example is attached.

v_lst_packages <- c("data.table",'stringr','dplyr','quantmod',
                    'evaluate','magrittr','ff','candlesticks',
                    'zeallot','future','purrr','DT', 'highcharter')
a = lapply(v_lst_packages, require, character.only = TRUE)

quantmod::getSymbols('AAPL')
p_Symbol = 'AAPL'
AAPL.DF<-data.frame(Date=index(AAPL),coredata(AAPL))

data = AAPL.DF%>%filter(Date > '2024-01-01')
names(data) <- c('Date','Open','High','Low','Close','Volume')

data_macd = data%>%select(Date,Close)
data_macd%<>%mutate(macd=TTR::MACD(Close,nFast=12,nSlow=26,nSig=9)[,1], 
                    signal =TTR::MACD(Close,nFast=12,nSlow=26,nSig=9)[,2])%>%
  mutate(macd=round(macd,3), signal=round(signal,3),diff12=macd-signal, 
         signdiff12=case_when(diff12>0~'pos',diff12<0~'neg',diff12==0~'zero'))%>%
  rename(macd12=macd, signal12=signal)
data_macd = merge(data%>%select(Date), data_macd, by=c("Date"),all.x = TRUE)

hc_data <- lapply(1:nrow(data), function(i) {
  list(as.numeric(as.POSIXct(data$Date[i])) *1000, 
       data$Open[i], data$High[i],data$Low[i],data$Close[i])})

hc_volume <- lapply(1:nrow(data), function(i) {
  list(x = as.numeric(as.POSIXct(data$Date[i])) * 1000,
       y = data$Volume[i], 
       color = ifelse(data$Close[i] > data$Open[i], 'green', 'red'))})

hc_macd <- lapply(1:nrow(data_macd), function(i) {
  list(x = as.numeric(as.POSIXct(data_macd$Date[i])) * 1000,
       y = data_macd$macd12[i])})

hc_signal12 <- lapply(1:nrow(data_macd), function(i) {
  list(x = as.numeric(as.POSIXct(data_macd$Date[i])) * 1000,
       y = data_macd$signal12[i])})

hchart <- highchart(type = "stock") %>%
  #hc_title(text = paste(p_Symbol,' -- ',v_type)) %>%
  hc_title(text = paste('AAPL')) %>%
  hc_yAxis_multiples(
    list(title = list(text = "OHLC"), height = "60%", lineWidth = 2, relative = 80),
    list(title = list(text = "Volume"), top = "65%", height = "35%",
         offset = 0, lineWidth = 2, relative = 20),
    list(title = list(text = "Macd"), top = "65%", height = "35%",
         offset = 0, lineWidth = 2, relative = 20)
  ) %>% 
  hc_xAxis(tickInterval = 0.25, gridLineWidth = 1) %>%
  hc_add_series(type = "candlestick", name = p_Symbol, data = hc_data) %>%
  hc_add_series(type = "column", name = "Volume", data = hc_volume, yAxis = 1)%>%
  hc_add_series(type = "line", name = "Macd", data = hc_macd, yAxis = 2)%>%
  hc_add_series(type = "line", name = "Macd", data = hc_signal12, yAxis = 2)%>%
  hc_plotOptions(candlestick = list(color = 'red', upColor = 'green', lineColor = 'red', 
                                    upLineColor = 'green')) 
hchart

My current plot

This is what I am trying to achieve:

Example of my desired output


Solution

  • I was able to add the third panel, by defining the right height and positions (i.e. top = ...).

    The first panel takes up 60%, then the next starts at 62% (2% gap) and takes 18%, and the third panel (i.e. MACD) starts at 82% and takes up the remaining 18%. By doing this, I also dropped the relative argument which would cause layout conflicts.

    I also explicitly differentiated between Macd and Signal and assigned different colors to them in MACD panel.

    highchart(type = "stock") %>%
      hc_title(text = paste('AAPL')) %>%
      hc_yAxis_multiples(
        # 60% of height
        list(title = list(text = "OHLC"), 
             height = "60%", lineWidth = 2),
        # starts at 62%, takes 18% height  
        list(title = list(text = "Volume"), 
             top = "62%", height = "18%", offset = 0, lineWidth = 2),
        # starts at 82%, takes remaining 18% height
        list(title = list(text = "MACD"), 
             top = "82%", height = "18%", offset = 0, lineWidth = 2)
      ) %>%
      hc_xAxis(tickInterval = 0.25, gridLineWidth = 1) %>%
      hc_add_series(data = hc_data, type = "candlestick", 
                    name = p_Symbol,  yAxis = 0) %>%
      hc_add_series(data = hc_volume, type = "column", 
                    name = "Volume",  yAxis = 1) %>%
      hc_add_series(data = hc_macd, type = "line", 
                    name = "MACD",  yAxis = 2, color = "blue") %>%
      hc_add_series(data = hc_signal12, type = "line", 
                    name = "Signal",  yAxis = 2, color = "red") %>%
      hc_plotOptions(candlestick = list(color = 'red', 
                                        upColor = 'green', lineColor = 'red',
                                        upLineColor = 'green'))