Show 3d axes in Plotly, R - r

Using plotly in R, how do I make a 3d plot that shows the axes, like this:
instead of a plot that shows a bounding box, like this:
?
R code for the second is
t <- seq(0,2*pi,by =1)
r <- seq(0,1,by = .1)
x <- as.vector(outer(t,r,function(t,r).5*r*cos(t)))
y <- as.vector(outer(t,r,function(t,r)r*sin(t)))
z <- 4*x^2+y^2
plot_ly(x = ~x, y = ~y, z = ~z, type = "mesh3d", opacity=.5)

Related

plotting 3D surface with plotly (matrix transformation with akima::interp)

In order to plot a surface in plotly with x, y, z, we can use the function interp to create the data (as input for the add_surface function in plotly)
This article give the solution.
I follow the different steps.
With the follow code, we can plotly a surface with markers.
library(akima)
library(plotly)
x=rep(seq(-1,5,0.2),time=31,each=1)
y=rep(seq(-1,5,0.2),time=1,each=31)
df=data.frame(x=x,y=y,
z=2*x+y-1.4)
fig <- plot_ly()%>% add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
We can see the following plot
Then I use interp to create the data for the surface, and I plot the surface together with the markers.
s = interp(x = df$x, y = df$y, z = df$z)
fig <- plot_ly()%>%
add_surface(x = s$x, y = s$y, z = s$z)%>%
add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
I have the following image.
We can see that the result is different. And I can't see why.
When I try to change the function to generate z, sometimes, the two surfaces are the same. For example, for this data.frame
df=data.frame(x=x,y=y,
z=x+y+1)
We have the following image. And we can see that this time, we get the same surfaces.
It appears the meanings of x and y are swapped in the add_surface versus the meanings in interp. The example that worked had x and y appear symmetrically. So swap them back by transposing the z matrix:
fig <- plot_ly()%>%
add_surface(x = s$x, y = s$y, z = t(s$z))%>%
add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
This is just a guess because the documentation in plotly is so weak. akima::interp clearly documents that rows of z correspond to different x values.

One slider to control graphs in single subplot in R using plotly

I'm looking for certain fix with range selector in plotly using R.
I have two plots visualized via a single subplot using Plotly in R. Now, I need to add a Range Slider/Selector to the complete plot, so that changing it modifies both my plots.
Is it possible via Plotly? (using R only)
This functionality is similar to Dygraphs synchronize feature(https://rstudio.github.io/dygraphs/gallery-synchronization.html).
I'd recommend using subplots option shareX = TRUE:
Please check the following example:
library(plotly)
DF1 <- data.frame(x=1:100, y=runif(100)+ seq(0, 1, length.out = 100))
DF2 <- data.frame(x=1:100, y=runif(100)+ seq(0, 2, length.out = 100))
p1 <- plot_ly(DF1, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p2 <- plot_ly(DF2, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p <- subplot(p1, p2, nrows = 2, shareX = TRUE)
p

How to set X axis range on plotly graphs?

I am using R version of plotly. Here is what I want:
x = 1:100
y = 1:100
plot_ly(x, y)
I want the graph only show where x>20 and x<40, ignoring the other part. How to do this ?
Set the layout of xaxis.
y = 1:100
plot_ly(x=~x, y=~y) %>%
layout(
xaxis = list(
range=c(20,40)
)
)

Defining outer edge of filled 2d Contour plot in plotly

Problem: I am trying to reproduce a round filled 2d contour plot in R using plotly (have tried ggplot2 also but plotly seemed to be easier).
Data: Sample data download link -
https://drive.google.com/file/d/10Mr5yWVReQckPI6TKLY_vzPT8zWiijKl/view?usp=sharing
The data to be plotted for contour is in a column format and typically called z variable, there is x and y data also available for all values of z. A simple dataframe would look like this:
Please ignore the repeat common x and y as I have truncated decimals. The data has about 25000 rows.
Approach: I first use akima package to interpolate z variable values for given x and y to map z in 2d. This makes the z column data fit in a xy grid for 2d plotting and show contours.
Expected outcome:
Code used:
dens <- akima::interp(x = dt$`Xvalue(mm)`,
y = dt$`Yvalue(mm)`,
z = dt$Values,
duplicate = "mean",
xo=seq(min(dt$`Xvalue(mm)`), max(dt$`Xvalue(mm)`), length = 10),
yo=seq(min(dt$`Yvalue(mm)`), max(dt$`Yvalue(mm)`), length = 10))
plot_ly(x = dens$x,
y = dens$y,
z = dens$z,
colors = c("blue","grey","red"),
type = "contour")
Actual outcome:
Help Needed:
To refine edges of the actual outcome plot to something of a close match to the expected outcome image.
Many thanks in advance for your comments and help.
I found that I could increase the grid output z matrix from akima::interp() from default 40x40 to custom using nx and ny input in function.
And then in plot_ly() add contours = list(coloring = 'fill', showlines = FALSE) to hide contour lines to get output close to my expected outcome.
So working code is like this:
dens <- akima::interp(x = dt$`Xvalue(mm)`,
y = dt$`Yvalue(mm)`,
z = dt$Values,
nx = 50,
ny = 50,
duplicate = "mean",
xo=seq(min(dt$`Xvalue(mm)`), max(dt$`Xvalue(mm)`), length = 50),
yo=seq(min(dt$`Yvalue(mm)`), max(dt$`Yvalue(mm)`), length = 50))
plot_ly(x = dens$x,
y = dens$y,
z = dens$z,
colors = c("blue","grey","red"),
type = "contour",
contours = list(coloring = 'fill', showlines = FALSE))
Plotly contour plot reference was very helpful in this case:
https://plot.ly/r/reference/#contour

Plot ellipse3d in R plotly?

Package rgl includes a very useful function ellipse3d, which can return an ellipsoid that cover like 95% percent of the points in 3D. Then this object can be used in rgl::plot3d to plot it out. My question is that is it possible to convert the output of ellipse3d to something that can be plotted through js plotting packages like plotly?
library(rgl)
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
plot3d(dt)
plot3d(ellipse, add = T, color = "red", alpha = 0.5)
Then what can I do to plot the ellipsoid through plotly?
You can extract the coordinates of the ellipse from the ellipse$vb. Then plot these. Something like:
p <- plot_ly() %>%
add_trace(type = 'scatter3d', size = 1,
x = ellipse$vb[1,], y = ellipse$vb[2,], z = ellipse$vb[3,],
opacity=0.01) %>%
add_trace(data=dt, type = 'scatter3d', x=~x, y=~y, z=~z)

Resources