Hi I've been struggling with R I did a PCA with some data, and now I need to color the PCA dotplot in function on what group the data belongs to.
Example of my data used for the PCA (the table is much longer)
A R N C
A 43 121 113 34
R 14 272 190 456
N 145 182 392 123
C 62 32 231 32
Exemple of my list of colors:
A blue
R red
N red
C yellow
So far I did this:
color=c('blue','red','yellow')
plot(result.acp$scores[,1], result.acp$scores[,2], col=color)
But the colors do not match what I want them do be...
Any suggestions?
EDIT:
> dput(head(m.acp$scores))
structure(c(0.485248382360341, 1.22814614717322, 1.41375370895067,
1.85498947175758, -0.375697094166481, 1.34378781510518, 1.51863186603447,
0.0572515048241142, -0.0568916310833516, 0.206543014267263, -0.343532082404636,
0.381649504516637, 1.97718761386959, -1.94694408760922, 0.280457723526986,
-0.265669310511105, 1.72386086473253, -1.68640891503048, 0.672268772844792,
2.49620220365566, -1.30569962068436, -2.96457420077961, 1.05873011149105,
0.249856229642777, 0.752740909620085, -0.3221475597597, -0.404971059387798,
-0.352104569288916, -1.59955880042156, 0.333186494105438, 1.01661633422811,
-0.524424365814158, -1.7920923623531, -0.118129421711271, -0.73363898593327,
0.773671927192673, 0.345338193690839, -0.147960273603448, -1.75910531541936,
0.470479642531196, 1.66771948007324, 0.858806139744018, 0.23677924891001,
0.414419626952358, 0.0739894762692486, 0.195277617408496, 0.665251426743185,
-0.0762532572287367, 1.16997815118309, -0.182032179249172, -0.631929337424788,
0.0308147786374188, 1.95340300164186, 0.375321240022787, 0.960836703335612,
0.183269601943323, -0.474379307207703, -0.366060693508836, -2.44695520772428,
-0.148494705705629, 0.98954553138859, -0.948420509262348, -1.43040720187175,
-0.813792180978826, -0.668336260091166, 1.44697150834988, 2.15084852373618,
0.660040624486677, 0.603497901938454, 0.71258499250433, -0.37074063114479,
-1.73475877465446, 0.545840674222426, -1.15093599362176, 2.25309487011121,
-1.20269177035014, 0.0873434143238955, 2.27247541012993, 0.562854625458883,
0.27543018066447, 0.133753878088941, 0.00449996616098378, -0.227465329112754,
-0.213097298055626, 1.28987242747272, -0.498171201060435, 0.672676976975279,
-0.703021260403972, -0.118017362131157, -0.563645828724812, 0.547975716027795,
1.88086974178728, 0.135685047918279, 0.714498879661545, -0.389199450099059,
0.974113605475622, 0.336700529414482, 0.669737969811285, 0.256286261107131,
-0.144265911846499, 0.129062364254905, 0.117900930006798, 0.462989706826572,
-0.730213304706471, -0.723150151495445, 1.82998913621228, -0.0485822435981942,
0.717875231696143, 0.335399340465998, -0.022113090137115, -0.0205652022391104,
-0.0412768969464503, 0.120177653804699, 0.0474613997209151, 7.25114412958305e-16,
-9.29811783123569e-16, -3.04617442381527e-15, -1.13103970633688e-15,
2.08166817117217e-16, -5.27355936696949e-16), .Dim = c(6L, 20L
), .Dimnames = list(c("A", "R", "N", "D", "C", "Q"), c("Comp.1",
"Comp.2", "Comp.3", "Comp.4", "Comp.5", "Comp.6", "Comp.7", "Comp.8",
"Comp.9", "Comp.10", "Comp.11", "Comp.12", "Comp.13", "Comp.14",
"Comp.15", "Comp.16", "Comp.17", "Comp.18", "Comp.19", "Comp.20"
)))
If the list
of colors is a list
, we can subset the list
elements based on the column name of the data used and that would go into the col
plot(result.acp$scores[,1], result.acp$scores[,2],
col = unlist(lst1[names(result.acp$scores)[1:2]]))
which can be wrapped into a function and pass only the position index
f1 <- function(ind) {
plot(result.acp$scores[,ind[1]], result.acp$scores[, ind[2]],
col = unlist(lst1[names(result.acp$scores)[ind]]))
}
f1(1:2)
f1(2:3)
f1(3:4)
Based on the new dataset, the dataset is a matrix
, so we use colnames
instead of names
scores1 <- scores[, 1:6]
colnames(scores1) <- row.names(scores1)
plot(scores1[,1], scores1[,2],
col = unlist(list1[colnames(scores1)[1:2]]))
result.acp <- list(scores = structure(list(A = c(43L, 14L, 145L, 62L), R = c(121L,
272L, 182L, 32L), N = c(113L, 190L, 392L, 231L), C = c(34L, 456L,
123L, 32L)), class = "data.frame", row.names = c("A", "R", "N",
"C")))
lst1 <- list(A = 'blue', R = 'red', N = 'red', C = 'yellow')