2

I am trying to change the font and opacity of the hoverinfo box within my plotly graph (using R). I have used the following code but cannot work out how to change the font or the opacity of the hover box, if this is even possible?

plotC <- plot_ly(tg, x = ~FINPERCH, y = ~JourneyTime, type = 'scatter', mode = 'lines',color = ~PeakDirection, colors=pal, linetype = ~PeakDirection,height = 500, width = 1200,

hoverinfo= 'text', text = ~paste("<b>Period: </b>", FINPERCH,'<br><b>Direction / Peak :</b>', PeakDirection,'<br><b>Average Journey Time:</b>',JourneyTime,'mins'))%>%

I do not want to add any annotations to the graph itself. I only want to set the font and opacity of the hover.

Any advice would be greatly appreciated. Thanks

JMac123
  • 23
  • 3

1 Answers1

4

You can use htmltools::htmlDependency to add CSS to the plot, as shown in this answer:

library(htmltools)
library(htmlwidgets)

p <- plot_ly(mtcars, x=~cyl, y=~mpg) 


x <- as_widget(p)                                 # convert to htmlwidget object 
# add a the code directly into <head> using `htmltools::htmlDependency`
x$dependencies <- c(x$dependencies,
  list(
    htmlDependency(
      name = "custom",
      version="1",
      src="",
      head='
        <style type="text/css">
        .hovertext {
            opacity: 0.5
        }
        </style>
      '
    )
  )
)
alan ocallaghan
  • 3,116
  • 17
  • 37