Skip to content

Adds interactive tooltip boxes to ggplots (standalone or rendered in Shiny)

License

Notifications You must be signed in to change notification settings

mjakubczak/ggtips

 
 

Repository files navigation

ggtips Logo

ggtips - interactive tooltips for ggplots

Build Status

Our package provides a set of functions to enhance your ggplots with eye-pleasing tooltips easily, and render them in your Shiny apps.

Preview

plot with tooltips

Demo shiny app

Installation

To install ggtips, use devtools:

devtools::install_github("Roche/ggtips")

Usage:

The main function - renderWithTooltips() - works similar to Shiny's renderUI(). To use it in your app, define an uiOutput element in the UI section (this will be the container for your plot), and substitute renderPlot() in your server logic with renderWithTooltips(), providing a list of variables you want to see in the tooltips:

output[["myPlot"]] <- renderWithTooltips(
  plot = ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point(aes(colour = Species)),
  varDict = list(Sepal.Width = "Width", Sepal.Length = "Length", Species = "Species")
)

renderWithTooltips function accepts also optional customGrob parameter. It allows to pass modified grob object and as a result output tooltips coordinates will fits its geometries. This is useful if you want to add any extra geometry to your plot, e.g. bar with information or warning.

If you want to save svg (e.g. you're using cache'ing) use longer code:

cache <- list() ## instead of list you can save data in data base like mongoDB
output[["myPlot"]] <- renderUI({
  ## generate hash for this plot
  ## https://stackoverflow.com/q/24192570/387194
  hash <- generateHash(plot)
  if (is.null(cache[[hash]]) {
    res <- ggtips::getSvgAndTooltipdata(
      plot = plot,
      varDict = varDict
    )
    cache[[hash]] <- res
  } else {
    res <- cache[[hash]]
  }
  ggtips::htmlWithGivenTooltips(
    svg = res$svg,
    data = res$data,
    varDict = varDict,
    height = height
  )
})

new API added in version 0.3.0

output[["myPlot"]] <- renderUI({
  plot = ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point(aes(colour = Species))
  varDict = list(Sepal.Width = "Width", Sepal.Length = "Length", Species = "Species")

  ggtips::plotWithTooltips(plot, varDict = varDict)
})

plotWithTooltips function accept same parameters as renderWithTooltips.

Options

  • plot - ggplot object
  • varDict - list of data to render on the tooltip that will be extracted from ggplot (keys are read data names and values are labels on tooltips)
  • customGrob - original grob object that can have grob manipulation
  • callback - function that can be used, to put additional data into tooltip, it should return string
  • width/height - number
  • point.size - size of the points

See our demo application (type ggtips::demo()) for example usage.

Docker demo

Optionally you can also build a Dockerized version of the demo:

sudo docker build -t ggtips -f inst/Dockerfile .

After building the docker image (which can take a while) use the command:

docker run -p 3838:3838 ggtips

The docker image starts in interactive mode (which means you can use ctrl+c to stop it) and exposes the web browser at port 3838.

Depending on your Docker setup, the exposed application may be available under:

http://localhost:3838

or may be exposed through the locally running docker-machine, which is the typical case with the Windows operating system that have no Hyper-V enabled. You can find more information about it on Hyper-V on Windows.

If your Docker setup is using docker-machine, you can find it's IP using command docker-machine ip, then use the http://{docker machine IP}:3838 address to connect to demo session.

See Docker setup tutorial to learn more about configuring Docker and Docker Machine Documentation to learn about docker-machine itself.

Contributors

About

Adds interactive tooltip boxes to ggplots (standalone or rendered in Shiny)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • R 72.5%
  • JavaScript 25.1%
  • CSS 1.9%
  • Dockerfile 0.5%