如何在 R 中制作 chartSeries (quantmod) 的交互式图表?
How can I make an interactive graph of chartSeries (quantmod) in R?
我有来自 quantmod 包的图表,我在其中从 SPY 获取数据,我想制作一个交互式图表,类似于我们使用 plotly
包中的 ggploty
的方式.这是我的代码的基本形式。
library(quantmod)
library(plotly)
getSymbols("SPY", from="2016-01-01", to="2020-01-01")
chartSeries(SPY, subset = "2017-11-18::2017-12-16")
addSMA(n=50, on=1, col = "blue")
这就是我尝试做的
p <- chartSeries(SPY, subset = "2017-11-18::2017-12-16")
ggplotly(p)
此代码无效,我认为它有一个不同的 class 对象。有没有办法让 chartSeries
的图表成为交互式图表,这样我就可以 select 特定范围的数据?
您可以使用 dygraphs 包创建一个带有 xts 对象的交互式图表 class
试试下面的代码,
它将创建一个带有价格烛台的交互式图表
library(quantmod)
library(dygraphs)
library(tidyverse)
library(lubridate)
library(htmlwidgets)
getSymbols("SPY", from="2016-01-01", to="2020-01-01")
SPY <- SPY[,c(1:4)] ## remove the volume and adjusted columns
SPY$SMA50 <- SMA(Cl(SPY), n = 50) #create SMA50 line
p <- dygraph(SPY, xlab = "Date", ylab = "Price", main = "SPY Price") %>%
dySeries("SPY.Open", label = "Open", color = "black") %>%
dySeries("SPY.Low", label = "Low", color = "red") %>%
dySeries("SPY.High", label = "High", color = "green") %>%
dySeries("SPY.Close", label = "Close", color = "orange") %>%
dySeries("SMA50", label = "SMA50", color = "blue") %>%
dyRangeSelector() %>%
dyCandlestick()%>%
dyCrosshair(direction = "vertical") %>%
dyHighlight(highlightCircleSize = 3, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = T) %>%
dyRoller(rollPeriod = 1)
p
我有来自 quantmod 包的图表,我在其中从 SPY 获取数据,我想制作一个交互式图表,类似于我们使用 plotly
包中的 ggploty
的方式.这是我的代码的基本形式。
library(quantmod)
library(plotly)
getSymbols("SPY", from="2016-01-01", to="2020-01-01")
chartSeries(SPY, subset = "2017-11-18::2017-12-16")
addSMA(n=50, on=1, col = "blue")
这就是我尝试做的
p <- chartSeries(SPY, subset = "2017-11-18::2017-12-16")
ggplotly(p)
此代码无效,我认为它有一个不同的 class 对象。有没有办法让 chartSeries
的图表成为交互式图表,这样我就可以 select 特定范围的数据?
您可以使用 dygraphs 包创建一个带有 xts 对象的交互式图表 class
试试下面的代码, 它将创建一个带有价格烛台的交互式图表
library(quantmod)
library(dygraphs)
library(tidyverse)
library(lubridate)
library(htmlwidgets)
getSymbols("SPY", from="2016-01-01", to="2020-01-01")
SPY <- SPY[,c(1:4)] ## remove the volume and adjusted columns
SPY$SMA50 <- SMA(Cl(SPY), n = 50) #create SMA50 line
p <- dygraph(SPY, xlab = "Date", ylab = "Price", main = "SPY Price") %>%
dySeries("SPY.Open", label = "Open", color = "black") %>%
dySeries("SPY.Low", label = "Low", color = "red") %>%
dySeries("SPY.High", label = "High", color = "green") %>%
dySeries("SPY.Close", label = "Close", color = "orange") %>%
dySeries("SMA50", label = "SMA50", color = "blue") %>%
dyRangeSelector() %>%
dyCandlestick()%>%
dyCrosshair(direction = "vertical") %>%
dyHighlight(highlightCircleSize = 3, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = T) %>%
dyRoller(rollPeriod = 1)
p