# Install packages
if (!requireNamespace("plot3D", quietly = TRUE)) {
install.packages("plot3D")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}
# Load packages
library(plot3D)
library(ggplotify)
3D-Scatter
Hiplot website
This page is the tutorial for source code version of the Hiplot 3D-Scatter
plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
3D scatter plot is to apply a number of quantitative variables to different coaxes in space and combine different variables into coordinates in space, so as to clearly explain the interaction between the three quantitative variables.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
plot3D
;ggplotify
Data Preparation
The loaded data are three variables and grouping.
# Load data
<- read.delim("files/Hiplot/159-scatter-3d-data.txt", header = T)
data
# Convert data structure
<- which(colnames(data) == "group")
col_idx <- as.factor(data[, col_idx])
data[, col_idx] <- 19
shapes <- ""
shape_idx
# View data
head(data)
temp pressure dtime group
1 41.11057 0.49351190 1 G1
2 35.23429 0.76636476 2 G1
3 26.58407 0.63885937 3 G1
4 34.76097 -0.08106332 4 G1
5 25.76521 -0.31731579 5 G1
6 20.30115 -1.91132873 6 G1
Visualization
# 3D-Scatter
<- as.ggplot(function() {
p <- scatter3D(data[, 1], data[, 2], data[, 3],
plot3d pch = shapes, cex = 1,
phi = 0, theta = 45, ticktype = "detailed",
bty = "b2", colkey = FALSE, alpha = 1,
xlab = colnames(data)[1], ylab = colnames(data)[2],
zlab = colnames(data)[3],
main = "3D-Scatter Plot",
colvar = as.numeric(as.factor(data[, 4])),
col = c("#e04d39","#5bbad6","#1e9f86")
)
legend("right", pch=19, legend = levels(data[, col_idx]),
cex = 1.1, bty = 'n', xjust = 0.5, horiz = F,
title = colnames(data)[col_idx],
col = c("#e04d39","#5bbad6","#1e9f86"))
})
p

In the figure, temperature, pressure and time are respectively placed on x (horizontal axis), Y (vertical axis) and Z (perspective axis) to generate a THREE-DIMENSIONAL scatter plot, and the correlation between variables and their correlation degree can be intuitively found.