Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Disable interactions in Plotly for R and ggplot2

Hi plot.ly,

I want to achieve the following two things using plot.ly R API and the ggplotly function:

  • Remove components from the action bar and/or the whole action bar
  • Disable zoom/pan/stretch

Is there a way to do this without writing JS?

Pass a FALSE to the config value displayModeBar to disable the little action bar. To stop the zooming pass TRUE to the fixedrange layout option for each respective axis.

Example:

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
mode = “markers”, color = carat, size = carat) %>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE))

1 Like

What about with ggplotly()?

It is handled the same way; a ggplotly object is a plotly object.

p <- ggplot(data = d, aes(x = carat, y = price)) +
  geom_point(aes(text = paste("Clarity:", clarity)), size = 4) +
  geom_smooth(aes(colour = cut, fill = cut))

ggplotly(p) %>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE))

1 Like