Show Sidebar Hide Sidebar

3D Hover Options in R

How to customize 3d hover options for plots 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.8.0'

Customize Hover for Spikelines

By default, Plotly's 3D plots display lines called "spikelines" while hovering over a point. These lines project from the hover point to each of the three axes' normal planes and then extend from those projection data points to the planes' wall boundaries.

library(plotly)

mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)

p <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, opacity = 0.8, color = ~am, colors = c('#BF382A', '#0C4B8E')) %>%
  add_markers() %>%
  layout(
    scene = list(
      xaxis = list(
          spikecolor = '#a009b5',
          spikesides = FALSE,
          spikethickness = 6
        ),
      yaxis = list(
          spikecolor = '#a009b5',
          spikesides = FALSE,
          spikethickness = 6        
        ),
      zaxis = list(
          spikecolor = '#a009b5',
          spikethickness = 6
      )
    )
  )

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

Customize Hover for Surface Contours

In addition to spikelines, Plotly 3D Surface plots also display surface contours on hover by default. These are customized by styling the contours attribute in the surface trace.

library(plotly)

p <- plot_ly(z = ~volcano) %>% add_surface(
  contours = list(
    x = list( 
      highlight = TRUE,
      highlightcolor = "#41a7b3"
      ),
      y = list(highlight = FALSE),
      z = list(highlight = FALSE)
  )
) %>%
  layout(
    scene = list(
      xaxis = list(showspikes=FALSE),
      yaxis = list(showspikes=FALSE),
      zaxis = list(showspikes=FALSE)
    )
  )


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

Reference

See https://plot.ly/r/reference/#layout-scene-xaxis and https://plot.ly/r/reference/#surface-contours for more information and options!

Still need help?
Contact Us

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