# 3D Plotly scatter plot with 2D plane

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 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)
``````

Hi @jdude48. You can use `add_mesh` with `opacity` argument.

``````library(plotly)

df <- data.frame(x = rnorm(10), y = rnorm(10), z = rnorm(10))
planDf <- data.frame(x = rep(range(df\$x), 2), y = rep(range(df\$y), each = 2), z = mean(df\$z))

plot_ly(df) %>%
add_markers(x = ~x, y = ~y, z = ~z) %>%
add_mesh(x = ~x, y = ~y, z = ~z, data = planDf, opacity = 0.3)
``````

Thank you very much for the reply raytong. Do I just substitute my "tweets" data frame for the "df" data frame from your example? If that is the case, what values do I insert into the rnorm() functions? Is the "10" just refer to the number of desired rows from the data set?

@jdude48. Because you didn't provide sample data, I generate the sample data by random `rnorm` function. In your case, the code should be somethings like below.

``````library(plotly)

planDf <- data.frame(x = rep(range(tweets\$date), 2), y = rep(range(tweets\$avg_Entropy), each = 2), z = 100)

plot_ly(tweets) %>%
add_markers(x = ~date, y = ~avg_Entropy, z = ~diffusion, color = ~factor(date), showlegend = T) %>%
add_mesh(x = ~x, y = ~y, z = ~z, data = planDf, opacity = 0.3)
``````

Thank you, raytong. That worked perfectly.

raytong, I was altering some of the parameters to tweak the appearance of the plane a little bit. I tried "color" and I tried "colorscale" but neither of those would change the color of the plane. Also, how can I expand the size of the plane a little bit? There are some of my points in the plot that do not intersect with the threshold plane. Thank you.

@jdude48. I still cannot figure out how to accurately set color for `plotly`. For expand the size, you can multiple the coordinate by factor.

``````mulFactor = 1.2
planDf <- data.frame(x = rep(range(tweets\$date) * mulFactor, 2), y = rep(range(tweets\$avg_Entropy) * mulFactor, each = 2), z = 100)
``````

raytong, I used the mulFactor and played around with the factor numbers and the plane size kept getting smaller and smaller. Here was the code that I used.

``````mulFactor = 6
planDf <- data.frame(x = rep(range(tweets\$date)*mulFactor, 2), y = rep(range(tweets\$avg_Entropy)*mulFactor, each = 2), z = 70)
``````

The 2D mesh was tiny when the graph rendered. Should I be creating the factor parameters differently?

@jdude48. I can't think of what cause the problem. Do you have any sample data for me to test for?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.