title: Plotly does not plot a non-quadratic surface
body: <p>I want to produce a 3D scatterplot and add a surface fitted with a linear regression, using plotly. My data:</p>
  <pre><code>structure(list(political_trust = c(1, 6, 7, 5, 0, 2, 1, 3, 5, 
  0, 2, 5, 5, 6, 6, 3, 3, 2, 5, 8, 3, 7, 3, 4, 5, 4, 5, 0, 0, 4, 
  6, 1, 0, 4, 0, 5, 5, 6, 7, 3, 5, 4, 5, 2, 4, 4, 7, 6, 7, 5, 4, 
  6, 7, 5, 7, 3, 3, 3, 2, 5, 2, 7, 3, 2, 7, 2, 3, 0, 7, 5, 7, 3, 
  0, 7, 2, 6, 3, 8, 7, 2, 2, 5, 0, 1, 6, 3, 6, 5, 1, 3, 4, 4, 5, 
  3, 3, 0, 2, 4, 9, 6, 3, 3, 2, 3, 4, 5, 8, 0, 4, 1, 5, 0, 4, 0, 
  5, 6, 3, 2, 7, 5, 4, 3, 8, 3, 4, 0, 3, 6, 7, 7, 2, 3, 5, 5, 5, 
  0, 3, 2, 1, 7, 5, 0, 4, 0, 2, 7, 3, 0, 8, 3, 2, 4, 5, 5, 3, 2, 
  3, 8, 6, 5, 6, 7, 0, NA, 7, 7, 2, 0, 3, 4, 7, 2, 1, 2, 0, 0, 
  4, 3, 3, 6, 6, 1, 4, 0, 4, 0, 0, 7, 6, 4, 4, 6, 5, 4, 3, 3, 0, 
  NA, 2, 5), political_interest = c(2, 0, 3, 3, 2, 1, 2, 2, 2, 
  2, 2, 2, 3, 3, 3, 3, 2, 2, 3, 2, 1, 2, 2, 2, 2, 0, 2, 1, 3, 1, 
  1, 1, 1, 1, 2, 3, 2, 2, 2, 1, 3, 3, 2, 3, 2, 1, 3, 2, 0, 3, 1, 
  1, 2, 1, 2, 2, 1, 3, 3, 2, 3, 2, 3, 2, 2, 1, 2, 0, 3, 1, 2, 2, 
  1, 3, 2, 2, 1, 2, 2, 0, 3, 2, 2, 1, 2, 1, 1, 3, 1, 1, 3, 2, 0, 
  2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 0, 1, 1, 2, 2, 2, 2, 
  2, 0, 0, 2, 3, 2, 2, 2, 3, 3, 0, 3, 3, 1, 2, 1, 1, 1, 2, 3, 2, 
  2, 2, 0, 2, 2, 2, 1, 2, 3, 3, 1, 2, 0, 1, 1, 0, 2, 2, 1, 2, 2, 
  2, 2, 3, 2, 1, 2, 2, 0, 0, 3, 2, 2, 2, 1, 2, 3, 0, 1, 2, 3, 2, 
  2, 2, 1, 3, 1, 1, 2, 2, 3, 3, 1, 2, 2, 2, 2, 2, 1, 0, 1, 1, 0, 
  3, 3), education_level = c(0, 2, 1, 5, 5, 0, 4, 4, 0, 0, 3, 2, 
  3, 4, 0, 4, 4, 4, 4, 3, 0, NA, 4, 0, 4, 3, 4, 1, 5, 2, NA, 0, 
  0, 4, 3, 3, 5, 3, 4, 0, 4, 4, 0, 4, 5, 4, 2, 2, 0, 5, 3, 0, 4, 
  1, 5, 4, 0, 4, 4, 5, 5, 4, 4, 4, 5, 2, 3, 2, 4, 0, 4, 0, 5, 4, 
  4, 4, 4, 4, 4, 2, 4, 5, 3, 4, 3, 0, 4, 4, 4, 3, 4, 4, 0, 3, 4, 
  2, 3, 3, 0, 4, 4, 4, 5, 4, 0, 4, 4, 4, 0, 3, 1, 4, NA, 4, 0, 
  1, 2, 4, 0, 2, 1, 4, 4, 4, 3, NA, 5, 2, 1, 0, 0, 4, 3, 3, 4, 
  3, 0, 3, NA, 4, 0, 0, 4, 5, 4, 5, 2, 2, 0, 3, 4, 3, 1, 3, 2, 
  3, 5, 0, 4, 5, 0, 5, 2, 0, 3, NA, NA, 2, 4, 3, 4, 3, 2, 2, 4, 
  4, 3, 0, 4, 0, 4, 4, 3, 0, 4, 4, 3, 5, 0, 3, 0, 4, 3, 0, 3, 3, 
  3, 4, 5, 1)), row.names = c(NA, -200L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>I start by defining a list of relevant variables - this is not necessary but basically a consequence of using the code in a Shiny up:</p>
  <pre><code>input &lt;- list()
  input$x &lt;- &quot;education_level&quot;
  input$y &lt;- &quot;political_trust&quot;
  input$z &lt;- &quot;political_interest&quot;
  </code></pre>
  <p>Next, creating the surface data:</p>
  <pre><code># Regressing &quot;political_interest&quot; on &quot;education_level&quot; and &quot;political_trust&quot;:
  lm &lt;- lm(as.formula(paste0(input$z, &quot; ~ &quot;, input$x, &quot; + &quot;, input$y)), data)
  
  # Defining range of values that outcome will be predicted for
  axis_x &lt;- seq(min(data[, input$x], na.rm = T),
                      max(data[, input$x], na.rm = T), by = 0.2)
  axis_y &lt;- seq(min(data[, input$y], na.rm = T),
                      max(data[, input$y], na.rm = T), by = 0.2)
  
  # Predicting outcome, and getting data into surface format
  lm_surface &lt;- expand.grid(x = axis_x, y = axis_y, KEEP.OUT.ATTRS = F)
  colnames(lm_surface) &lt;- c(input$x, input$y)
  lm_surface &lt;- acast(lm_surface, as.formula(paste0(input$x, &quot; ~ &quot;, input$y)), 
                                       value.var = input$z)
  </code></pre>
  <p>Last, plotting this with plotly:</p>
  <pre><code>data %&gt;%
          filter(!is.na(get(input$z))) %&gt;%
          filter(!is.na(get(input$x))) %&gt;%
          filter(!is.na(get(input$y))) %&gt;% 
          plot_ly(., x = ~jitter(get(input$x), factor = 2.5), 
                  y = ~jitter(get(input$y), factor = 2.5), 
                  z = ~jitter(get(input$z), factor = 2.5),
                         type = &quot;scatter3d&quot;, mode = &quot;markers&quot;,
                         marker = list(size = 2, color = &quot;#cccccc&quot;)) %&gt;%
          add_surface(., z = lm_surface,
                  x = axis_x,
                  y = axis_y,
                  type = &quot;surface&quot;)
  </code></pre>
  <p>This gives me the following. As you can see, the surface does not cover the full range of the y-dimension. Note also that the surface plotted is &quot;quadratic&quot; - i.e. same length in x and y - although it should have non-quadratic dimensions.</p>
  <p><a href="https://i.stack.imgur.com/2MWLX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2MWLX.png" alt="enter image description here" /></a></p>
  <p>I can bring plotly to draw larger surface area, e.g. by changing the range of values like below, but it always stays quadratic.</p>
  <pre><code>axis_x &lt;- seq(0, 10, by = 0.2)
  axis_y &lt;- seq(0, 10, by = 0.2)
  </code></pre>