Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com/

3D Hover Options in R

How to customize 3d hover options for plots in R with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

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)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, opacity = 0.8, color = ~am, colors = c('#BF382A', '#0C4B8E')) 
fig <- fig %>%
  add_markers() 
fig <- fig %>%
  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
      )
    )
  )

fig

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)

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


fig

Reference

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

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)