Liz L
Liz L

Reputation: 43

plot3D to create 3 dimensional scatter plot in RStudio

I am trying to create a 3D scatter plot. I don't know the etiquette behind changing code as it's edited... so have just replaced with latest version. I'm now getting a 3D plot - not sure how to change the colour scheme of the dots to match "N" value but working on it...

Two 3d graphic visualisations of RA / RWR and C40 #VERSION 1 – pretty but I’m not sure what the colours are yet! Still needs work

if(!require('tidyverse')) {
  install.packages('tidyverse')
  library('tidyverse')
}
library(readxl)

if(!require('tidyverse')) {
  install.packages('tidyverse')
  library('tidyverse')
}
library(readxl)
library(dplyr)
library(ggplot2)

# Install and load the plot3D package
if(!require('plot3D')) {
  install.packages('plot3D')
  library('plot3D')
}

# Example data frame
tdp <- data.frame(
  sample=c(1770,1771,1772,1773,1774,1775,1776,1777,1778,1779,1780,1781,1782,1783,1784,1785,1786,1787,1788,1789,1790,1791,1792,1793,1794,1795,1796,1797,1798,1799,1800,1801),
  RA=c(31.25,7.142857143,20,22.22222222,0,16.66666667,33.33333333,45,22.22222222,12,9.090909091,33.33333333,29.41176471,36.36363636,50,25,21.42857143,0,22.5,18.18181818,0,8.333333333,38.88888889,26.31578947,40,16,37.5,17.64705882,10,40,0,0),
  C40=c(12.5,7.142857143,30,11.11111111,30,16.66666667,0,5,33.33333333,32,18.18181818,16.66666667,17.64705882,18.18181818,5.555555556,0,14.28571429,20,10,4.545454545,0,33.33333333,22.22222222,21.05263158,13.33333333,8,25,29.41176471,15,13.33333333,0,0),
  N=c(16,14,10,9,10,6,15,20,9,25,11,12,17,11,18,8,14,20,40,22,3,12,18,19,15,25,8,17,20,15,3,2),
  RWR=c(18.75,7.142857143,0,22.22222222,10,33.33333333,20,15,44.44444444,44,18.18181818,0,17.64705882,18.18181818,16.66666667,25,14.28571429,25,20,18.18181818,0,33.33333333,11.11111111,21.05263158,40,24,25,23.52941176,30,20,0,0)
)

# Display the first few rows of the data frame
head(tdp)

# Create a 3D scatter plot
scatter3D(x = tdp$RA, y = tdp$RWR, z = tdp$C40, colour = "black", 
      pch = 16, cex = 1.5, xlab = "RA", ylab = "RWR", 
      zlab = "C40", 
      main = "title", ticktype = "detailed", 
      type = "h", theta = 10, d = 2, 
      colkey = list(length = 0.5, width = 0.5, cex.clab = 0.75))

#VERSION 2 – interactive 3D model


if(!require('tidyverse')) {
  install.packages('tidyverse')
  library('tidyverse')
}
library(readxl)

if(!require('tidyverse')) {
  install.packages('tidyverse')
  library('tidyverse')
}
library(readxl)
library(dplyr)
library(ggplot2)

# Install and load the plot3D package
if(!require('plot3D')) {
  install.packages('plot3D')
  library('plot3D')
}

# Example data frame
tdp <- data.frame(
  sample=c(1770,1771,1772,1773,1774,1775,1776,1777,1778,1779,1780,1781,1782,1783,1784,1785,1786,1787,1788,1789,1790,1791,1792,1793,1794,1795,1796,1797,1798,1799,1800,1801),
  RA=c(31.25,7.142857143,20,22.22222222,0,16.66666667,33.33333333,45,22.22222222,12,9.090909091,33.33333333,29.41176471,36.36363636,50,25,21.42857143,0,22.5,18.18181818,0,8.333333333,38.88888889,26.31578947,40,16,37.5,17.64705882,10,40,0,0),
  C40=c(12.5,7.142857143,30,11.11111111,30,16.66666667,0,5,33.33333333,32,18.18181818,16.66666667,17.64705882,18.18181818,5.555555556,0,14.28571429,20,10,4.545454545,0,33.33333333,22.22222222,21.05263158,13.33333333,8,25,29.41176471,15,13.33333333,0,0),
  N=c(16,14,10,9,10,6,15,20,9,25,11,12,17,11,18,8,14,20,40,22,3,12,18,19,15,25,8,17,20,15,3,2),
  RWR=c(18.75,7.142857143,0,22.22222222,10,33.33333333,20,15,44.44444444,44,18.18181818,0,17.64705882,18.18181818,16.66666667,25,14.28571429,25,20,18.18181818,0,33.33333333,11.11111111,21.05263158,40,24,25,23.52941176,30,20,0,0)
)

# Display the first few rows of the data frame
head(tdp)
# Create a 3D scatter plot with sample numbers as labels
scatter3D(x = tdp$RA, y = tdp$RWR, z = tdp$C40, 
          pch = 19, cex = 0.5, col = "blue",
          xlab = "RA", ylab = "RWR", zlab = "C40")

# Add sample numbers as text labels
text3D(x = tdp$RA, y = tdp$RWR, z = tdp$C40, 
       labels = tdp$sample, add = TRUE, 
       cex = 0.6, col = "red")

I'm still trying to wrangle this with your help - which is much appreciated :)

Upvotes: 0

Views: 70

Answers (0)

Related Questions