Creating and Interpreting Scree Plots for Multivariate Normal Data Using R Code Example
Here is the revised code with the requested changes:
library(MASS)
library(purrr)
data <- read.csv("data.csv", header = FALSE)
set.seed(1);
eigen_fun <- function() {
sigma1 <- as.matrix((data[,3:22]))
sigma2 <- as.matrix((data[,23:42]))
sample1 <- mvrnorm(n = 250, mu = as_vector(data[,1]), Sigma = sigma1)
sample2 <- mvrnorm(n = 250, mu = as_vector(data[,2]), Sigma = sigma2)
sampCombined <- rbind(sample1, sample2);
covCombined <- cov(sampCombined);
covCombinedPCA <- prcomp(sampCombined);
eigenvalues <- covCombinedPCA$sdev^2;
}
mat <- replicate(50, eigen_fun())
colMeans(mat)
library(ggplot2)
library(tidyr)
library(dplyr)
as.data.frame(mat) %>%
mutate(id = row_number()) %>%
pivot_longer(starts_with("V")) %>%
ggplot(aes(x = id, y = value, colour = name, group = name)) +
geom_hline(aes(yintercept = ave(value, name)), linetype = 3) +
geom_path(show.legend = FALSE) +
geom_point(show.legend = FALSE) +
theme_classic() +
ggtitle("Scree plot") +
xlab("Principal component") +
ylab("Eigen Value") +
theme(plot.title = element_text(hjust = 0.5))
The changes made were:
- Added
ggtitleto set the title of the plot - Added
xlabto set the label for the x-axis - Added
ylabto set the label for the y-axis - Added
theme(plot.title = element_text(hjust = 0.5))to center the title
Note that I also added a space between the ggtitle, xlab, and ylab functions, as per the standard convention in R.
Last modified on 2023-05-22