Show Sidebar Hide Sidebar

interactive tooltip in ggplot2

How to add custom tooltip effects to R and ggplot2 charts with JavaScript.

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.5.6.9000'

Maps

library(plotly)

data(canada.cities, package="maps")
p <- ggplot(canada.cities, aes(long, lat)) +
        borders(regions="canada", name="borders") +
        coord_equal() +
        geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")

p <- ggplotly(p)

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

Custom Tooltip

library(plotly)
#install.packages("gapminder")
library(gapminder)

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, text =      paste("country:", country))) +
     geom_point(alpha = (1/3)) + scale_x_log10()  

p <- ggplotly(p)

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

Inspired by Gapminder Tutorial

Control Events

library(plotly)
#install.packages("gapminder")
library(gapminder)

p <- ggplot(gapminder, aes(x = year, y = lifeExp, text = paste("country:",country))) + 
     geom_point() +
     facet_wrap(~ continent) 

p <- ggplotly(p)

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

Inspired by Gapminder Tutorial

Still need help?
Contact Us

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