Installing packages

“rgl” package contain plot3D function to create interactive plots

install.packages("rgl", type = "binary", repos = "https://cloud.r-project.org")
## Installing package into '/Users/tharakaw/Library/R/4.0/library'
## (as 'lib' is unspecified)
## 
## The downloaded binary packages are in
##  /var/folders/9c/x6y8738917b2cswdp75thmsc0000gn/T//RtmpcdQwHt/downloaded_packages
library(rgl, lib.loc =  "/Library/Frameworks/R.framework/Versions/4.0/Resources/library") #XQuartz must be installed on your computer

Reading data into R

morph <- read.csv("/Users/tharakaw/Documents/Personal/TWData/TWData_old/sfmorphfor3d.csv")
head(morph)
##   Population   Head     Wing     Body
## 1          1 336.00 1726.560 2017.920
## 2          1 301.92 1728.000 2022.144
## 3          1 303.36 1734.144 2001.024
## 4          1 301.92 1767.744 1964.640
## 5          1 324.96 1755.360 2004.288
## 6          1 343.68 1752.384 1963.776
tail(morph)
##     Population   Head     Wing     Body
## 95           2 340.32 1828.320 2446.752
## 96           2 360.96 1789.920 2485.824
## 97           2 353.28 1800.384 2500.224
## 98           2 369.60 1783.104 2498.400
## 99           2 373.92 1804.224 2497.344
## 100          2 360.96 1807.200 2420.544

This is not a actual data set. This data set indicates morphological parameters of two populations of sand flies (1 and 2). 3D plot will indicate how these morphological features differ in two populations

Creating the plot

In the following codes, plot3d function of rgl package has been used to create a 3d interactive scatter plot.

colors = c("red", "green") #defining colors
colors = morph$Population #referencing colors
plot3d(morph[,2:4])
plotint <- with(morph, plot3d(morph[,2:4], type="s", col = morph$Population, xlab="Total body length", ylab="Wing length", zlab = "Head length"))

Visualisation

Above code create the plot in XQuartz rgl device. Following code enables it’s visualization in a HTML page

install.packages("rglwidget", repos = "https://cloud.r-project.org")
## Installing package into '/Users/tharakaw/Library/R/4.0/library'
## (as 'lib' is unspecified)
## 
## The downloaded binary packages are in
##  /var/folders/9c/x6y8738917b2cswdp75thmsc0000gn/T//RtmpcdQwHt/downloaded_packages
library(rglwidget)
## The functions in the rglwidget package have been moved to rgl.
rglwidget(x = scene3d(plotint)) #rglwidget is better than writewebogl