Analysis in R: PCA results in ggplot2 displayed in Biplot “ggbiplot” package

This is an introduction to the ggplot2 package for displaying the results of a principal component analysis in Biplot.

Package version is 0.55. Checked with R version 4.1.2.

Install Package

Run the following command.

#Install Package
install.packages("devtools")
devtools::install_github("vqv/ggbiplot")

Examples

See the command and package help for details.

#Loading the library
library("ggbiplot")

###Creating Data#####
set.seed(1234)
TestData <- matrix(rnorm(1000), 200)
TestData <- as.data.frame(TestData)
#Data Adjustment
TestData[1:67,] <- TestData[1:67,] + 1
TestData[68:135,] <- TestData[68:135,] + 2
TestData[136:200,] <- TestData[136:200,] + 3
#Group information is assigned
TestData <- cbind(c(rep("Group1", 67),
                    rep("Group2", 67),
                    rep("Group3", 66)),
                  TestData)
colnames(TestData) <- c("Group", paste0("ColName", seq(5)))
########

#stat package:Principal component analysis with prcomp command
TestPrc <- prcomp(TestData[, 2:6], scale. = FALSE)

#Create biplot:ggbiplot command
#Specifies the result of prcomp() or princomp():pcobj option
#The principal component to be plotted:choices option
#Group Information:groups option
#Probability ellipses for each group:ellipse option
#Describes a correlated circle:circle option
#The command ggplot2 is available
ggbiplot(pcobj = TestPrc, choices = 1:2, obs.scale = 1, var.scale = 1,
         groups = TestData[, 1], ellipse = TRUE, circle = TRUE) +
  scale_colour_manual(values = c("#FF0000", "black", "#00FF00")) +
  theme(legend.direction = "horizontal", legend.position = "top")

Output Examples

・ggbiplot command

ggbiplot

I hope this makes your analysis a little easier !!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
Copied title and URL