Show Sidebar Hide Sidebar

Hover Text and Formatting in R

How to use hover text and formatting in R with Plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.7.1.9000'

Add Hover Text

library(plotly)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = c(1:5), 
    y = rnorm(5, mean = 5),
    text = c("Text A", "Text B", "Text C", "Text D", "Text E"),
    hoverinfo = 'text',
    marker = list(color='green'),
    showlegend = F
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link <- api_create(p, filename = "hover-text")
chart_link

Format Hover Text

library(plotly)

p <- plot_ly(type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = c(1:100), 
    y = rnorm(100, mean = 5), 
    marker = list(color='green'),
    hoverinfo = 'y',
    showlegend = F
  ) %>%
  layout(
    title = "Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>",
    titlefont = list(
      size = 10
    ),
    xaxis = list(
      zeroline = F
    ),
    yaxis = list(
      hoverformat = '.2f'
    )
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link <- api_create(p, filename = "hover-formating")
chart_link

Reference

See https://plot.ly/r/reference/ for more information and options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.