# === STEP 1: Install and Load Required Libraries ===
# Uncomment these if not already installed
# install.packages("openxlsx")
# install.packages("plotly")
# install.packages("ggplot2")
library(openxlsx)
## Warning: package 'openxlsx' was built under R version 4.3.3
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(magrittr)
# Load Excel data
ftir <- read.xlsx("C:/Users/Administrator/Desktop/ANALYSIS with R/Final 1.xlsx",
sheet = 1,
colNames = TRUE,
rowNames = TRUE)
# Convert Group column to factor
ftir$Group <- as.factor(ftir$Group)
# Create ftir_data excluding the Group column
ftir_data <- ftir[, !names(ftir) %in% "Group"]
# Run PCA
pca_ftir <- prcomp(ftir_data, scale. = TRUE)
# Combine PCA results with group info
scores_pca <- cbind.data.frame(pca_ftir$x[, 1:3], Group = ftir$Group)
# Create 3D plot
plot_ly(
data = scores_pca,
x = ~PC1, y = ~PC2, z = ~PC3,
color = ~Group,
text = rownames(scores_pca),
marker = list(size = 6)
) %>%
layout(
scene = list(
xaxis = list(title = "PC1 (58.9%)"),
yaxis = list(title = "PC2 (15.1%)"),
zaxis = list(title = "PC3 (13.1%)")
)
)
## No trace type specified:
## Based on info supplied, a 'scatter3d' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter3d
## No scatter3d mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode