@大衛克魯克這裏有一個簡單的例子。
library(plotly)
library(quantmod)
prices <- getSymbols("MSFT", auto.assign = F)
prices <- prices[index(prices) >= "2016-01-01"]
# Make dataframe
prices <- data.frame(time = index(prices),
open = as.numeric(prices[,1]),
high = as.numeric(prices[,2]),
low = as.numeric(prices[,3]),
close = as.numeric(prices[,4]))
# Blank plot
p <- plot_ly()
# Add high/low as a line segment
# Add open close as a separate segment
for(i in 1:nrow(prices)){
p <- add_trace(p, data = prices[i,], x = c(time, time), y = c(high, low), mode = "lines", evaluate = T,
showlegend = F,
marker = list(color = "grey"),
line = list(width = 1))
p <- add_trace(p, data = prices[i,], x = c(time, time), y = c(open, close), mode = "lines", evaluate = T,
showlegend = F,
marker = list(color = "#ff5050"),
line = list(width = 5))
}
p
UPDATE: 與plotly 4.0
這樣的版本是輕鬆了許多:
library(plotly)
library(quantmod)
prices <- getSymbols("MSFT", auto.assign = F)
prices <- prices[index(prices) >= "2016-01-01"]
# Make dataframe
prices <- data.frame(time = index(prices),
open = as.numeric(prices[,1]),
high = as.numeric(prices[,2]),
low = as.numeric(prices[,3]),
close = as.numeric(prices[,4]))
plot_ly(prices, x = ~time, xend = ~time, showlegend = F) %>%
add_segments(y = ~low, yend = ~high, line = list(color = "gray")) %>%
add_segments(y = ~open, yend = ~close,
color = ~close > open,
colors = c("#00b386","#ff6666"),
line = list(width = 3))
更完整的例子在這裏看到:http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/
如果收盤低於開放,會發生什麼?然後,您將「較低」設置爲比「較高」更高的值。 – user1357015
我試圖過濾出這些案件無濟於事。 msft = read.csv(「http://ichart.finance.yahoo.com/table.csv?s=MSFT」, header = TRUE, sep =「,」) msft $ Date msftF = msft%> % tbl_df()%>% filter(as.Date(Date)> as.Date(「2016-01-01」))%>% filter(Close> Open)%>% na.omit() ()= ggplot(aes(x = factor(Date),ymin = Low,lower = Open,middle = Close,upper = Close,ymax = High))+ geom_boxplot()+ geom_boxplot =「身份」) ggplotly(x) –
我基本上正在試圖建立一個燭臺圖表,但財務包裝似乎不存在的R呢。 –