Home > database >  Labelling x,y, and z axis on plot_ly surface plot (R)
Labelling x,y, and z axis on plot_ly surface plot (R)

Time:03-02

I have the following lines of code for plotting a surface

      fig<-plot_ly(z=~MatDurMat,y=~MatDurAxis[,1],x=~MatDurAxis[,2],type = "surface")
 %>% layout(xaxis=list(title='Discount rate'), yaxis=list(title='Initial carbon price (£)'))
        fig

It plots the surface just fine, but despite not getting any errors, the axis titles show up as MatDurMat, MatDurMat[,1], and MatDurMat[,2], as opposed to the ones I have specified in the layout section.

Thanks in advance.

CodePudding user response:

This should do it:

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
  MatDurAxis <- cbind(1:25, seq(.01, .25, by=.01))
  MatDurMat <- outer(MatDurAxis[,1], MatDurAxis[,2], "*")
  MatDurAxis = as.data.frame(MatDurAxis)
  plot_ly(z=~MatDurMat,y=~MatDurAxis$V1,x=~MatDurAxis$V2, type = "surface") %>% 
    layout(scene = list(
      xaxis=list(title='Discount rate'),
      yaxis=list(title='Initial carbon price (£)'),
      zaxis=list(title='Z AXIS TITLE')))

Created on 2022-03-01 by the reprex package (v2.0.1)

Note, you need to use scene for 3d axes.

  • Related