How do I plot 4 candlestick charts in one plot?

74 Views Asked by At

I have been trying to plot 4 candlestick charts in one plot. The grid.arrange, layout or par() function didn't work. I have tried the following code as well

m1 <- tail(merck.xts, n = 32)
head(merck.xts)



          Open    High    Low      Close   Adj.Close 
2019-09-04 "87.14" "87.17" "85.420" "86.05" "83.54009"
2019-09-05 "86.64" "86.66" "85.500" "86.10" "83.58862"
2019-09-06 "86.54" "86.93" "86.310" "86.57" "84.04491"
2019-09-09 "86.50" "86.52" "82.920" "83.47" "81.03533"
2019-09-10 "82.39" "82.39" "78.850" "81.69" "79.30725"
2019-09-11 "81.86" "83.11" "81.620" "82.92" "80.50137"

dy_graph <- list(
  dygraphs::dygraph(m1, group="Indicator", main="Merck"),
  dygraphs::dygraph(m2, group="indicator", main="Pfizer")
 )

dy_candlestick<-dyCandlestick(dy_graph)

htmltools::browsable(htmltools::tagList(dy_candlestick))

The problem with this code is that it brings the 2 charts in one single plot but shows no candlesticks

There are two more candlestick charts but as well m3 and m4. If someone could answer that would be very useful. Thanks in advance

1

There are 1 best solutions below

0
On

Your time series matrix looks odd for me. It seems to be all characters. You can get it to work by using the example dataset, note and check that you have numeric and not characters:

library(dygraphs)
library(htmltools)
data(sample_matrix)

m1 <- head(sample_matrix, n = 32)
m2 <- tail(sample_matrix, n = 32)

# you can see it's num
str(m1)
 num [1:32, 1:4] 50 50.2 50.4 50.4 50.2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:32] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
  ..$ : chr [1:4] "Open" "High" "Low" "Close"
str(m2)
 num [1:32, 1:4] 47.8 47.8 47.7 47.6 47.7 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:32] "2007-05-30" "2007-05-31" "2007-06-01" "2007-06-02" ...
  ..$ : chr [1:4] "Open" "High" "Low" "Close"


dy <-lapply(list(m1,m2),function(i)dygraph(i) %>%dyCandlestick)
browsable(tagList(dy))

enter image description here