I'm trying to create score plots of the first two principal components. I begin by splitting the data into three data frames based on class
. I then transform the data and perform PCA.
My data is as follows:
14 1 82.0 12.80 7.60 1070 105 400
14 1 82.0 11.00 9.00 830 145 402
14 1 223.6 17.90 10.35 2200 135 500
15 1 164.0 14.50 9.80 1946 138 500
15 1 119.0 12.90 7.90 1190 140 400
15 1 74.5 7.50 6.30 653 177 350
15 1 74.5 11.13 8.28 930 113 402
16 1 279.5 14.30 9.40 1575 230 700
16 1 82.0 7.80 6.70 676 175 525
16 1 67.0 11.00 8.30 920 106 300
16 2 112.0 11.70 8.00 1353 140 560
16 2 149.0 12.80 8.70 1550 170 550
16 2 119.0 8.50 7.40 888 175 250
16 2 119.0 13.30 9.60 1275 157 450
16 2 238.5 14.90 8.90 1537 183 700
16 2 205.0 12.00 7.90 1292 201 600
16 2 82.0 9.40 6.20 611 209 175
16 2 119.0 15.95 10.25 1350 145 450
16 2 194.0 16.74 10.77 1700 120 450
17 2 336.0 22.20 10.90 3312 135 450
17 3 558.9 23.40 12.60 4920 152 600
17 3 287.0 14.30 9.40 1510 176 800
17 3 388.0 23.72 11.86 3625 140 500
17 3 164.0 11.90 9.80 900 190 600
17 3 194.0 14.40 9.20 1665 175 600
17 3 194.0 14.40 8.90 1640 175 600
17 3 186.3 9.70 8.00 1081 205 600
17 3 119.0 8.00 6.50 625 196 400
17 3 119.0 9.40 6.95 932 165 250
17 3 89.4 14.55 9.83 1378 146 400
Column 1: type
, Column 2: class
, Column 3: v1
, Column 4: v2
, Column 5: v3
, Column 6: v4
, Column 7: v5
, Column 8: v6
My code is as follows:
data <- read.csv("data.csv")
result <- split(data, data$class);
data1 <- result[[1]][,3:8];
data1Logged <- log10(data1)
pca.data1Logged = prcomp( ~ v1 +
v2 +
v3 +
v4 +
v5 +
v6,
data = data1Logged, scale. = FALSE );
data2 <- result[[2]][,3:8];
data2Logged <- log10(data2)
pca.data2Logged = prcomp( ~ v1 +
v2 +
v3 +
v4 +
v5 +
v6,
data = data2Logged, scale. = FALSE );
data3 <- result[[3]][,3:8];
data3Logged <- log10(data3)
pca.data3Logged = prcomp( ~ v1 +
v2 +
v3 +
v4 +
v5 +
v6,
data = data3Logged, scale. = FALSE );
For each of the three class
, I want to have a score plot for PC1 and PC2:
pca.data1Logged$x[,1:2]
pca.data2Logged$x[,1:2]
pca.data3Logged$x[,1:2]
This is the best I could figure out:
opar <- par(mfrow = c(1,3))
plot(pca.data1Logged$x[,1:2])
plot(pca.data2Logged$x[,1:2])
plot(pca.data3Logged$x[,1:2])
par(opar)
But I would like this plot to be scaled, coloured, superimposed, etc. I have started reading about ggplot, but I don't have the experience to do this. I would like something like the following:
https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html
The problem with the above is that I have broken the data into 3 separate data frames, so there are no headings for "class1", "class2, "class3".