简体   繁体   中英

How do I add color to labels in a pca plot?

I am trying to plot pca. But there are overlap of the label names in the plot. How do I move those labels in the plot? 主成分分析输出

code used

library(factoextra)
library(FactoMineR)

# Compute PCA with ncp = 2
res.pca <- PCA(datm, ncp = 2, graph = FALSE)
# Compute hierarchical clustering on principal components
res.hcpc <- HCPC(res.pca, graph = FALSE)


plot(res.hcpc, choice = "3D.map", sub="PCA Clusters")

I want to add PPBC1a, PPBC1b, PPBC1c one color, similarly for PPBC2a,b,c and PPBC3a,b,c,d

the data used as follows:

dput(datm)

structure(c(12.79802058, 10.89742342, 13.00479536, 13.64688912, 
14.17038237, 13.90494906, 13.54635544, 14.25565698, 14.86503437, 
13.54133782, 11.1653212, 11.01250594, 10.9527396, 13.69093192, 
14.70875995, 11.38278472, 11.12961813, 13.29853222, 13.49095453, 
13.1619033, 9.966433138, 10.67426065, 10.78077335, 14.15939746, 
16.02465067, 11.73237694, 10.42060106, 9.453730095, 13.37238268, 
16.35668986, 10.70283822, 11.46108048, 4.667661637, 14.41355243, 
16.58813424, 12.21522223, 11.22616596, 12.32599886, 11.43089556, 
15.3907471, 10.7018847, 11.05914848, 10.01466231, 12.91440139, 
14.85643716, 11.17261824, 11.41834054, 10.39116957, 11.21272536, 
14.25124128, 12.22137936, 12.93868862, 11.56519352, 10.50344844, 
11.55031894, 11.38322914, 10.47556631, 11.15981881, 11.52102261, 
10.94487402, 11.64516694, 13.78851408, 11.42176044, 11.03451725, 
10.88189033, 11.53493091, 10.92014018, 11.74711381, 10.98622502, 
10.27737559, 10.04825453, 10.06856283, 9.473361641, 12.29532767, 
14.9837309, 10.2983783, 10.11032589, 10.19707019, 11.07196875, 
13.73176839, 11.28729374, 11.80497494, 13.56806793, 11.14757033, 
10.42813405, 10.83331515, 9.856914347, 11.21904373, 10.80822287, 
10.91284032, 10.20859053, 9.410343499, 9.948968741, 11.47888667, 
11.50839216, 11.47823205, 11.96327312, 11.17161266, 11.5667065, 
11.42616106, 9.493094329, 9.448753468, 9.288950806, 12.82071465, 
13.8216361, 9.618496101, 9.264784876, 9.158918708, 11.81330267, 
13.29542936, 11.64169084, 11.1446361, 11.78787136, 10.00885973, 
10.8443866, 9.620006131, 9.795650634, 10.95021727, 10.72375077, 
9.688961166, 11.56038993, 10.77185199, 11.56282223, 9.909142388, 
10.13332508, 10.64896999, 10.08616698, 10.79031452, 10.36053333, 
9.949119042, 9.667419497, 9.2998083, 9.270160036, 10.93375289, 
11.32732089, 10.43670451, 11.06311588, 11.21425016, 12.02255073, 
9.573431887, 9.214022307, 9.157054545, 8.618929314, 12.0888963, 
13.44540908, 9.497197032, 9.515249128, 8.623513357, 11.39360436, 
12.97238166, 10.36255544, 9.036073308, 8.975751645, 8.661048339, 
12.53967869, 10.38703923, 10.74839297, 10.0727921, 11.53731204, 
11.85151216, 8.963064616, 8.905819317, 8.961398581, 12.45768699, 
13.26878505, 9.13890204, 9.098268177, 8.641522307, 10.92563493, 
12.51834881, 9.031546274, 7.92737859, 9.332794101, 10.58509175, 
11.92010082, 10.18771142, 10.40310795, 10.26934483, 11.1051311, 
10.88559116, 11.69057173, 10.03446933, 11.23332211, 10.50344844, 
9.417949681, 9.443619859, 10.06159633, 9.274065761, 9.893683766, 
9.982434562, 8.347704963, 7.998531668, 9.203153275, 10.92113512, 
14.51497636, 9.087402337, 8.715407397, 8.14626007, 10.9365942, 
13.26424666, 9.022397644, 9.291377206, 7.802169848, 9.624119866, 
11.63210545, 10.91918468, 9.628354314, 9.715402094, 10.27459859, 
11.85488782, 9.085248988, 8.903975769, 7.842128194, 11.13963286, 
10.9426894, 10.1253052, 11.18126721, 10.88911888, 10.33176186, 
9.124823973, 10.23388164, 12.38883251, 9.768248709, 10.35329359, 
8.232032734, 9.206833364, 9.919787227, 9.063412539, 9.523736019, 
9.663506002, 10.91023525, 9.933398449, 11.09861509, 9.245256138, 
9.343531381, 9.578669343, 8.67702586, 10.39435208, 9.714191234, 
9.154382761, 11.37184936, 10.22273334, 10.33336875, 9.457908594, 
9.052084518, 9.458890947, 8.774155458, 9.081974159, 9.475674974, 
9.688961166, 10.02403938, 10.5024381, 11.03117054, 9.260761293, 
9.220378095, 9.613956493, 8.918174108, 8.510438946, 9.888047223, 
9.709006423, 11.71440478, 9.980458168, 10.51792787, 9.58525359, 
9.447318876, 9.02032214, 9.166742167, 9.243778354, 9.239244702, 
8.614926126, 10.83830018, 9.588324046, 10.60694522, 9.789244256, 
8.93708957, 8.976160958, 9.269086561, 9.074048482, 9.402415129, 
9.036295386, 6.701686821, 7.377218224, 8.98819414, 10.66235984, 
13.42158018, 8.692687637, 8.240953611, 8.057584229, 10.22851668, 
13.07653088, 10.91517999, 9.291377206, 10.93542887, 9.397483884, 
9.294090175, 8.821827081, 9.110308905, 10.19220187, 9.234807728, 
8.246792484, 8.731823059, 8.363957616, 7.470325568, 9.306299357, 
12.12012408, 9.38086296, 9.981342621, 7.759351889, 11.43862499, 
10.65328466, 9.067569876, 8.152966733, 8.668411667, 9.223264482, 
11.03793682, 8.896081541, 10.5311998, 9.783354462, 9.530989752, 
10.08376407, 10.32340931, 9.824703064, 10.88411494, 9.173338947, 
9.733479214, 8.544415804, 8.953370094, 8.322117986, 9.291448403, 
9.4176827, 10.60728078, 10.18305421, 10.1916577, 9.297306238, 
8.697110912, 9.410839171, 9.338269397, 8.556743415, 9.080141698, 
8.692623603, 8.811928095, 8.177667088, 7.307511459, 9.863185379, 
9.556158498, 10.02745688, 9.249626944, 10.67118117, 9.422058192, 
10.55015172, 10.07649154, 9.827619905, 10.2642534, 8.649299253, 
9.387969121, 9.013438829, 9.066482425, 8.391946397, 8.728792309, 
9.396201652, 10.60728078, 9.439882325, 10.29029261, 8.688859999, 
8.583939305, 9.281281423, 9.384594534, 9.161414002, 8.747621482, 
8.294169479, 8.125790217, 7.998531668, 8.361435176, 8.713889348, 
11.16604497, 8.746298376, 9.238701298, 9.453730095, 9.830435279, 
10.74347379, 11.34645, 9.245558055, 10.45345693, 9.025018286, 
8.454643693, 9.212852871, 6.184579775, 8.526040439, 9.527367451, 
8.863557236, 7.295645309, 7.193411473, 8.803364617, 8.233438374, 
10.69877804, 9.62151458, 9.676087343, 9.463869602, 10.25283839, 
9.53744679, 9.891447443, 9.275790795, 10.47820851, 9.226426791, 
8.841917928, 8.550777269, 8.856575833, 8.745075803, 8.297187146, 
8.415380547, 7.623943184, 8.311965668, 8.4897075, 8.608617442, 
10.88427676, 8.863268496, 7.883308702, 10.20071067, 9.570245299, 
9.9128568, 7.748462407, 8.552538573, 7.756876788, 8.531327176, 
10.45085452, 9.271702802, 8.456732294, 10.23188233, 9.119190827, 
10.1603572, 9.774738472, 13.41074356, 7.324670625, 10.34677543, 
7.077674568, 9.55202058, 9.444764619, 8.490695346, 7.635886226, 
7.195360015, 10.70474337, 8.968898113, 10.46683747, 9.40866043, 
7.268486595, 8.615923306, 8.028128113, 9.09247427, 8.61031829, 
8.327075009, 8.192784529, 7.84863112, 8.168216487, 7.944293433, 
9.910217283, 9.495552562, 8.249715019, 9.40193343, 10.15533647, 
9.396201652, 13.13663182, 6.623433691, 8.609746358, 7.586017239, 
9.504319631, 7.580262953, 8.953370094, 9.674085744, 9.726878458, 
6.608960227, 9.8947901, 9.382833733, 9.719025557, 8.991940494, 
8.542122463, 7.567669559, 7.540070977, 8.683843888, 8.630755359, 
8.677416831, 10.51148541, 9.26578359, 9.947151017, 8.083971149, 
8.422061056, 8.298080674, 8.258423094, 8.125885143, 8.086003207, 
8.521463999, 7.777954993, 7.605282271, 7.861693809, 10.37836558, 
10.97775208, 7.665407713, 7.734648946, 6.380465146, 9.198810841, 
11.82593974), .Dim = c(10L, 50L), .Dimnames = list(c("PPBC1a", 
"PPBC1b", "PPBC1c", "PPBC2a", "PPBC2b", "PPBC2c", "PPBC3a", "PPBC3b", 
"PPBC3c", "PPBC3d"), c("DSP", "ASPH", "ERBB2", "SCGB2A2", "CASC3", 
"GLUL", "ITPRID2", "MSL1", "PXDN", "TC2N", "PSMD3", "HLA-DRB1", 
"GRN", "MTUS1", "MED24", "CRACR2B", "ORMDL3", "ANK3", "CIITA", 
"GSDMB", "MAGI3", "SELENOP", "ABHD2", "VWA1", "IRF1", "SYNGR2", 
"TNFAIP3", "ITGB2", "GRB7", "SGK1", "ARHGEF38", "FAM118A", "C1QB", 
"EFHD2", "VAV3", "C1QC", "TNFRSF14", "MPHOSPH6", "RGS16", "RHPN2", 
"PGD", "SPTSSA", "SAMD12", "RGS5", "SOCS3", "C11orf80", "GABRE", 
"GBP4", "HLA-DQA1", "PGAP3")))

I would recommend you to not use a 3D visualization. Instead, you could simply use the fviz_dend which creates a cluster dendrogram that is actually the same and even better to read of. Here is a reproducible example:

library(factoextra)
library(FactoMineR)

# Compute PCA with ncp = 2
res.pca <- PCA(datm, ncp = 2, graph = FALSE)
# Compute hierarchical clustering on principal components
res.hcpc <- HCPC(res.pca, graph = FALSE)

fviz_dend(res.hcpc)

Created on 2023-01-04 with reprex v2.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM