# Insert 2D plane into a 3D Plotly scatter plot in R

I am plotting a 3D chart with three variables, which are date, diffusion, and avg_Entropy. I would like to insert a simple 2D plane horizontally into the scatter plot that will serve as a threshold for diffusion. I have served multiple forums and tutorials for such an implementation, and all I can find is how to insert a regression line which is not really what I want. I have included a picture of my current scatter plot. I would like to be able to, for example, insert a transparent plane through the 100 value for “diffusion” so that any points above this threshold meet the criteria for my experiment. Here is my current code.

plot_ly(color = ~factor(date), showlegend = T) %>%
add_markers(data = tweets, x = ~date, y = ~avg_Entropy, z = ~diffusion) %>%
add_paths(data = tweets, x = ~date, y = ~avg_Entropy, z = ~diffusion)

@jdude49 A horizontal plane can be defined a a surface parameterized by

`x =u, y =v, z =height, u in [a, b], v in [c,d]`
where [a,b], [c,d] are intervals (ranges) of your data.

You can define it as any Plotly surface.

Thank you for your reply, empet. The one follow-up question that I have is how do I define the x,y, and z variables before I parameterize them in the add_surface statement? For example, when I define “u,” what is the correct syntax in which to place my range of vaues? Is is u = c(value1, n)? I have not worked with 3D plots before with R. Thank you.

@jdude49 Unfortunately I’m working with Python Plotly. I thought you have experience with plotting a surface
in R plotly, such that to particularize its definition to this simple surface.

I can describe how it works:

• divide the interval [a,b] by n points to get a grid, analogously divide [c,d] by m points.

• in this case x is an array of n elements and y an array of m elements.

• z is then defined as a 2d array of m x n elements equal each to the height.

x, y, z and a colorscale define the surface.

Thank you for your follow-up. I have been working with only 2D plots in R for my PhD research. I have been plotting Twitter sentiment analysis and shannon entropy data over a time period. My dissertation chair was suggestion that I use a tuple and a 3D environment for my plots. I have been trying to learn as much as possible over the past few days. Thanks.

@jdude49
Below is the Python code for plotting such a plane. It seems that the 2d Kernel Density estimation example posted here https://plot.ly/r/3d-surface-plots/ plots a surface defined by x, y, z. The colorscale is Viridis, by defalut in that example.

``````import numpy as np
import plotly.graph_objects as go
height=2
x= np.linspace(-1, 1, 75)
y= np.linspace(0, 2, 100)
z= height*np.ones((100,75))
mycolorscale = [[0, '#aa9ce2'],
[1, '#aa9ce2']]

surf = go.Surface(x=x, y=y, z=z, colorscale=mycolorscale, showscale=False)
layout = go.Layout(width=600,
scene_camera_eye_z=0.75)
fig = go.Figure(data=[surf], layout=layout)
fig.show()
`````` Thank you for the example. I am now trying to reverse engineer your Python code and translate the variable definitions to R. I used the tutorial previously that you had mentioned. I will see if I can get this to work in my 3D scatterplot. Thanks.