KANIKIG

KANIKIG

just for fun | 兴趣使然 Ph.D. in Engineering|❤️ #NFT $ETH| [Twitter](https://twitter.com/kanikig2)|[Github](https://github.com/KANIKIG)|[Telegram channel](https://t.me/kanikigtech)

Using Plotly for 3D Visualization in R

image

Introduction#

For simple multidimensional matrix data processing, R is quite convenient, and the syntax is also very concise.

During the process, I learned about representing the plane distribution of stress and strain through contour plots or heat maps. When I was looking for a way to implement this type of plot, I came across a package called Plotly in the Renmin University of China Economic Forum.

After learning about it, I was attracted by its expressive power. It turns out that visualization can be done so beautifully in such a simple way. I decided to use it!

This is a demo of the 3D Surface Plot from the official website: Volcano

library(plotly)
# volcano is a numeric matrix that ships with R
fig <- plot_ly(z = ~volcano)
fig <- fig %>% add_surface()

fig

pl2.png

Click here to visit

The 3D model is interactive, you can drag it with the mouse, and the position of the pointer will also display the data.

About Plotly#

Plotly is a very powerful open-source interactive visualization package that supports Python, R, and Matlab (apparently unofficially). After reading the official documentation, I felt that it has better support for Python in terms of functionality, while for R, there are still some difficulties in implementing certain features.

pl3.png

It has online and offline modes. In online mode, after registering an account on the official website, you can upload your visualization results to the official platform for online saving and viewing. I am using the offline mode.

Visualization Implementation#

Plotly requires a two-dimensional numerical matrix to draw a 3D surface plot. Therefore, after the team members processed the raw data, they provided me with a series of stress (strain) matrices on different depths z representing the horizontal planes (where i represents the x-axis and j represents the y-axis).

Since we divided the actual horizontal plane into 2cm * 2cm sections, all ij points in the matrix need to be multiplied by 2 to obtain the actual coordinates.

Implementing a single plot is achievable by referring to the demo provided on the official website. RStudio can not only export static images but also output the entire 3D model in the form of HTML, which can be interactively viewed using a web browser. You can experience it on the official demo page mentioned earlier.

pl1.png

What we need to do is to show the distribution of stress and strain as a parameter changes, which requires the use of GIF animations.

After reading the Plotly official documentation, it seems that the R package does not support outputting HTML through code, nor does it support generating 3D animations (I tried to output the results using a for loop, but it did not create an animation).

It may also be because I didn't study it in depth enough, so I ended up using a rather clumsy method.

Outputting JPEG images in a loop, and then using GIF editing software to combine them into an animation. (At least I don't have to think about how to implement animation in code)

Here is the code:

library(stringr)
library(dplyr)
library(plotly)
library(processx)

# Convert matrix ij to actual coordinates

x1 <- seq(0,by=2,length.out=100)
y1 <- seq(0,by=2,length.out=100)

# Get all file names in the folder for looping

strdir <- "./3D/data-z/ txy (kPa)"
dir <- dir(strdir)

# Loop through all CSV data files in the folder and output one plot for each file

for (i in 1:length(dir))
{
stress <- as.matrix(read.csv(file = str_c(strdir,"/",dir[i])))  # Read the file and convert it to a numerical matrix
stress <- stress[,-1]  # The first column of my CSV file is the index, which is not needed, so I delete it
fig <- plot_ly(z=~stress, x=x1, y=y1)%>%add_surface()
fig <- fig %>% layout(
  title <- dir[i], 
  scene <- list(
    yaxis <- list(autorange = "reversed"), # The y-axis of our coordinate system is reversed
    zaxis <- list(title = "Shear Stress")
  ))
orca(fig, str_c(dir[i],".jpeg")) # orca is the image output method given in the official documentation, it needs to be installed separately
}



After combining the images using GIF software, the final animation effect is as shown:

pl4.gif

That's all.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.