Black Lives Matter. Please consider donating to Black Girls Code today.

Hover text for plotly r candlestick chart

I am trying to make using of candlestick chart to represent a model output . So I am trying to plot the actual,modeled, and upper and lower values using a candelstick chart in plotly. As per the given example for plotly r the hover always show open,close,high,low. Could I changed this hover text to custom text. Thank you.

library(plotly)
library(quantmod)

getSymbols("AAPL",src='yahoo')

# basic example of ohlc charts
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)

p <- df %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~AAPL.Open, close = ~AAPL.Close,
          high = ~AAPL.High, low = ~AAPL.Low) %>%
  layout(title = "Basic Candlestick Chart")

See the Data Labels on Hover section in https://plot.ly/r/line-and-scatter/

plot_ly(
  d, x = ~carat, y = ~price,
  # Hover text:
  text = ~paste("Price: ", price, '$<br>Cut:', cut)
)

I did use that in the example above but I still get open ,high along with custom text I added.

    plot_ly(cig_prophet_ak,x = ~date, type="candlestick",
          open = ~actuals, close = ~predicted,
          high = ~upper, low = ~lower,
          text = ~paste("High:", upper,"<br>Actuals: ", actuals, '<br>Predicted:', predicted, '<br>Lower:', lower)) %>%
  add_trace(x = ~date, y = ~mape, name = 'Mape', type = 'scatter', mode = 'lines',yaxis = "y2",
            fillcolor = 'rgba(0,255,0, 0.5)') %>%
  layout(title = "Basic Candlestick Chart",yaxis2 = ay,
         yaxis = list(title = 'Values',showgrid=FALSE))
text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
hoverinfo = 'text',

Add hoverinfo = 'text' to your code!

When I do that I dont get hover working at all:

candlestick_plotly<-
  plot_ly(cig_prophet_ak,x = ~date, type="candlestick",
          open = ~actuals, close = ~predicted,
          high = ~upper, low = ~lower,
          text = ~paste("High:", round(upper,4),"<br>Actuals: ", round(actuals,4), '<br>Predicted:', round(predicted,4), '<br>Lower:', round(lower,4),'<br>MAPE:', round(mape,4)),
          hoverinfo = 'text') %>%
  add_trace(x = ~date, y = ~mape, name = 'Mape', type = 'bar', mode = 'lines',yaxis = "y2",
            fillcolor = 'rgba(0,255,0, 0.5)') %>%
  layout(title = "Basic Candlestick Chart",yaxis2 = ay, xaxis = list(rangeslider = list(visible = F)),
         yaxis = list(title = 'Values',showgrid=FALSE))

I dont get any error message as such. It just doesnt show hover text.
‘4.8.0’

You’re right, the hoverinfo=‘text’ seems to be not working with the candlestick. You need to do it like the code below and specify you customer text in the add_markers part.

p <- df %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~AAPL.Open, close = ~AAPL.Close,
          high = ~AAPL.High, low = ~AAPL.Low,
          hoverinfo = 'none'
          ) %>%
  add_markers(y = ~(AAPL.Low + AAPL.High)/2, hoverinfo = "text",
              text = ~paste("High: ", round(AAPL.High,4)), marker = list(color = "transparent")) %>% 
  layout(title = "Basic Candlestick Chart")

Thank you :slight_smile: . That worked