1
votes

I'm trying to draw a time series boxplot in R with the plotly libraries, however I need to be able to have total control of the ymin, ymax, ylow etc.

It renders perfectly fine in ggplot2, althought with a ton of warnings. It fails to render in ggplotly

Here is what I 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") 
3
What happens if the close is below the open? Then you're setting "lower" to a higher value than the "upper".user1357015
I tried filtering out those cases to no avail. msft = read.csv("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() x = msftF %>% ggplot(aes(x = factor(Date), ymin = Low, lower = Open, middle = Close, upper = Close, ymax = High)) + geom_boxplot() + geom_boxplot(stat = "identity") ggplotly(x)David Crook
I'm basically trying to build a candlestick chart, but the financial wrapper appears to not exist for R yet.David Crook
I wrote something for the folks at plotly here -> moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod Uses plot_ly() instead of ggplotly(). Goes without saying that the function is inspired by charts in the quantmod package :) Hopefully helps...royr2
Thats exactly what I was looking for, can you push that into the answers?David Crook

3 Answers

1
votes

@David Crook here's a 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 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: with the release of plotly 4.0 doing this is a 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 a more complete example see here: http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/

1
votes

You can do this using the quantmod package.

Try the following:

library(quantmod)
getSymbols("MSFT")
candleChart(MSFT,multi.col=TRUE,theme='white') 

You can trim the MSFT object to a smaller date range if you don't need all of them.

If you need to do it with ggplot, let me know and I'll write up the code. But typically, I stick with the packages where I can as it's cleaner!

0
votes

Royr2 answered this, however he has not moved the answer from comments into answers in a few days, so to capture the answer as a marked answer, I am simply migrating his comment into answers. If he posts his answer, I will gladly mark his answer as the appropriate answer.

"

I wrote something for the folks at plotly here -> http://moderndata.plot.ly/candlestick-charts-using-plotly-and-quantmod/ Uses plot_ly() instead of ggplotly(). Goes without saying that the function is inspired by charts in the quantmod package :) Hopefully helps... –

"

royr2 Apr 25 at 5:02