Site icon R-bloggers

Alternative Hover Text In Custom Function with plotly

[This article was first published on klogr::blog(posts), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="problem" class="level2">

Problem

How to add alternative text when hovering over a value using plotly for R.

A few considerations I had to navigate:

Let’s begin with creating test data.

# Load packages
library(plotly)
library(rlang)

# Create data
df <- data.frame(
 x = 1,
 y = 1,
 text = "text",
 alt_text = "alt_text"
)
< section id="solution" class="level2">

Solution

This create_plot() function demonstrates how referencing a column against an argument, allows easy access for the hovertemplate argument to be assigned one of those referenced columns. In this example – text.

create_plot <- function(df, text){
 
 text <- enquo(text)

  plot_ly(
  data = df, 
  x = ~x, 
  y = ~y, 
  width = 300, 
  height = 300,
  type = "scatter",
  mode = "text+marker",
  text = text,
  # not necessary but demonstrates code differences
  hovertemplate = text,
  text = list(size = 50, color = "#b44046"))}

This create_plot_alt() function demonstrates the difference in syntax. Inspired by Stack Overflows Djack but amended to suit a custom function, rlang::quo_get_expr() with [[]] allows for referencing additional columns.

create_plot_alt <- function(df, text, alt_text){
 
 text <- enquo(text)
 alt_text <- enquo(alt_text)
 
 plot_ly(
  data = df,
  x = ~x, 
  y = ~y, 
  width = 300, 
  height = 300,
  type = "scatter",
  mode = "text+marker",
  text = text,
  text = list(size = 50, color = "#57a2a4"),
  hovertemplate = df[[rlang::quo_get_expr(alt_text)]] 
  )
 
}

It works!. The first plot with red text – on hover will show “text”, whilst the second plot with blue text shows “alt_text”

# To remove elements not needed
void <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE
)

p1 <- create_plot(df = df, text = text) %>% 
 layout(xaxis = void, yaxis = void)

p_alt <- create_plot_alt(df = df, text = text, alt_text = alt_text) %>% 
 layout(xaxis = void, yaxis = void)

# same hover text as text plotted
 style(p1, showlegend = FALSE)
# alternative hover text
 style(p_alt, showlegend = FALSE) 
< section id="conclusion" class="level1">

Conclusion

Getting this to work took me too long to admit and later became more complicated by passing a SharedData R6 class object for use with the crosstalk package. I’ll leave that battle for another day. Taking wins where I can, I’ll bank this fix and look to explore rlang in more detail in the future.

< section id="acknowledgements" class="level2">

Acknowledgements

r-bloggers.com for the reach, platform, and content

Packages and package maintainer(s):

To leave a comment for the author, please follow the link and comment on their blog: klogr::blog(posts).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version