Adding alpha to hover in R - can it be done?

Hi folks.
Newb here.

I am learning how to create plotly figures using the ggplotly route. I was pleased to see how easily I could generate an interactive plot from my previously created ggplot figure. However, after that very expeditious experience I’ve been slowed to crawl as I try to tweak the resulting plotly figure.

As you can hopefully see in the attached image, the hover that comes up obscures the points behind it. is there a way to set the alpha of the hover box to either match that of the points it references, or to a constant value?

G = ggplot(T, aes(x=fct_reorder(Tea, median_rating, median), y=Rating, color=Brand, fill=Brand,
                  text = paste(
                        "Tea: ", Tea, "\n",
                        "Brand: ", Brand, "\n",
                        "Tea Median Score: ", median_rating, "\n",
                        "Score: ", Rating, "\n",
                        sep = "")
           ) +
    geom_point(size=2.5, shape=21, alpha=0.3) +
    scale_color_brewer(palette="Set1", direction = 1) +
    scale_fill_brewer(palette="Set1", direction = 1) +
    coord_flip() +
    labs(x="Tea", y="Rating") +
    ggtitle('Herbal Tea Ratings') +
    theme_minimal() +
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5)
#set up margins
m <- list(
  l = 50,
  r = 50,
  b = 50,
  t = 100,
  pad = 4

P = plotly::ggplotly(G, tooltip = "text")
config(P, displaylogo = FALSE) %>%
    layout(responsive=TRUE, margin = m)


Currently setting the opacity for a hoverlabel isn’t possible. hoverlabel.opacity is part of an open issue regarding plotly.js.

The only workaround I am aware of is setting hovermode = 'x unified'.

You’ll need to adapt your layout call:

[...] %>% layout(responsive=TRUE, margin = m, hovermode = 'x unified', hoverlabel = list(bgcolor = 'rgba(255,255,255,0.75)', font = list(color='black')))

1 Like