r - BoxPlot in ggplotly -
i'm trying draw time series boxplot in r plotly libraries, need able have total control of ymin, ymax, ylow etc.
it renders fine in ggplot2, althought ton of warnings. fails render in ggplotly
here have.
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")) %>% na.omit() msftf %>% ggplot(aes(x = factor(date), ymin = low, lower = open, middle = close, upper = close, ymax = high)) + geom_boxplot() + geom_boxplot(stat = "identity")
@david crook here's simple example.
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 line segment # add open close 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: release of plotly 4.0
doing lot easier:
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))
for more complete example see here: http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/
Comments
Post a Comment