Passing N-attributes to Hovertemplate

I’m using the R implementation of Plotly and have been wondering if there is a way to pass any number of attributes to plotly::plot_ly() solely for more information / data points in hover templates.

Take the following example where I create a custom hover template (custom_hover_marker) and pass that to plot_ly()'s hovertemplate argument - In addition to the x and y attributes, I’m leveraging the text and customdata. Note: This example is psuedo-code, not reprex.

custom_hover_marker <- paste0("Merchant: %{y}<br>",
                              "Change: %{x:.1%}<br>",
                              "Current Week: %{text}<br>",
                              "Prior Week: %{customdata}<br>",
                              "<extra></extra>")
  
  data |> 
    plot_ly(x = ~value_change, 
            y = ~forcats::fct_reorder(merchant, value_change, .desc = TRUE), 
            text = ~data_current,
            customdata = ~data_prior,
            type = "scatter", 
            mode = "markers",
            marker = list(color = "#1d2d42",
                          size = 10,
                          line = list(color = "#1d2d42", 
                                      width = 1)),
            hovertemplate = custom_hover_marker,
            hoverlabel = list(font = list(size = 10)))

Perhaps there already exists a way to do this but I want to pass any number of attributes (in addition to the core ones such as x, y) for use in hovertemplate. For example, instead of being forced to use a pre-existing set of attributes, something like hoverinput_1, hoverinput_2, hoverinput_3, … hoverinput_n would be very useful.

Thanks for your help and glad to have finally joined this community. If this feature doesn’t exist, I can create a GitHub issue / feature request. Thank you!

1 Like