Changing margins of a 3d scatter plot using plotly- R

Viewed 642

I'm using plotly to generate a 3D scatter plot, see example code below:

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, 
     color = ~am, 
     colors = c('#BF382A', '#0C4B8E')) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'Weight'),
                 yaxis = list(title = 'Gross horsepower'),
                 zaxis = list(title = '1/4 mile time')),
     title = "Example plot")

When I download a static image of the plot, there's no space at the top of the image and the title looks nearly cut off. Is there a way to adjust the margins on the 3D plot to fix this? Attempting to adjust the margins by specifying layout yields the error:

'scatter3d' objects don't have these attributes: 'margin'

1 Answers

Perhaps you tried to add margin at a wrong place. The following does work:

layout(scene = list(xaxis = list(title = 'Weight'),
                    yaxis = list(title = 'Gross horsepower'),
                    zaxis = list(title = '1/4 mile time')),
       title = "Example plot", margin = list(t = -1))
Related