John Romansic
2014-Aug-18 23:31 UTC
[R] principle component values on PCA plots do not match
Hi all, I am using prcomp to do Principle Components Analysis and have run into a problem regarding the scale of the axes on my plots. I am using prcomp to analyze a set of 25 morphological measurements taken on each of 161 individual frogs. I used the biplot function to produce a figure of PC1 vs PC2 for each of the individual frogs and arrows that represent the loadings of the different morphological measurements on PC1 and PC2. Then I constructed a separate, similar plot, using my own coding, that provides a number for each individual frogs corresponding to its species as determined by genetic analysis, without the arrows. The y-axis on the first plot ranges shows PC2 values ranging from about -11 to 11, but the y-axis on the second plot shows PC2 values ranging from about -4 to 4, although all the data points seem to show up on the figure. I would like to show the same PCA results on both plots and I do not understand why the y-axes on these two plots do not match. By the way, the x-axes on these two plots seem to match, but I think that is just a coincidence. I suspect that my coding for the second plot is missing a command regarding scaling of the principle components, but it's not obvious to me why those data would have to be re-scaled. I thought the scaling is dealt with by prcomp. Within prcomp, I used scale=TRUE, which I understand re-scales the original data so that all the variables have equal variance. Does anyone have any suggestions on what might be wrong with my coding? Below is an example, using a truncated data set, which produces different numbers than the example I described above, since this time only 5 measurements are included for each frog. Nevertheless, the second plot (the one with my coding) has the same problem.> frogsGenetic.species SVL HL HW BED FED 1 27 47.75 16.46 14.63 13.27 8.29 2 25 44.87 16.35 14.67 13.50 8.69 3 24 52.57 19.82 16.62 15.12 9.35 4 24 56.60 21.18 19.55 17.73 11.09 5 20 47.66 18.48 16.33 14.98 9.37 6 19 65.96 23.33 20.00 18.88 12.19 7 19 58.67 21.12 18.12 17.37 10.47 8 19 64.33 23.16 19.99 19.20 11.88 9 19 61.03 21.98 19.42 18.22 1.16 10 19 62.88 21.90 19.71 17.92 11.65 11 19 62.77 21.31 19.30 18.47 11.85 12 19 58.64 21.30 19.55 18.17 11.14 13 19 63.40 20.74 19.59 18.25 11.03 14 19 58.22 19.87 18.82 16.85 10.73 15 18 55.43 19.80 18.27 16.01 9.52 16 17 60.46 21.40 19.37 16.67 10.44 17 16 58.32 19.48 17.55 16.42 9.96 18 14 44.64 16.35 14.41 12.77 8.42 19 14 48.32 17.88 16.23 14.34 9.01 20 13 42.32 15.43 14.40 12.47 7.43 21 13 41.87 15.97 14.97 12.46 7.42 22 13 45.71 16.87 15.98 13.08 7.77 23 12 47.38 17.48 15.77 13.99 9.08 24 12 50.28 18.48 17.28 14.87 9.46 25 12 48.00 18.52 17.60 15.40 9.63 26 12 50.98 18.61 17.87 16.00 9.59 27 12 50.76 18.24 17.61 17.72 9.54 28 12 50.83 18.90 18.28 15.02 9.53 29 11 46.80 19.13 17.10 15.22 9.01 30 10 37.55 14.21 12.42 10.79 6.15 31 10 40.39 15.32 13.81 11.89 6.80 32 10 42.39 14.77 14.72 11.87 6.78 33 9 44.08 16.37 15.99 13.09 8.50 34 9 47.36 16.16 16.14 13.50 7.72 35 8 41.43 15.53 12.73 11.01 6.37 36 8 41.45 15.63 13.68 11.32 7.20 37 8 38.86 13.82 12.13 10.39 6.28 38 8 40.51 14.25 12.68 10.40 6.41 39 8 44.64 15.37 13.75 11.37 6.82 40 8 45.08 15.64 14.25 11.17 6.68 41 8 45.10 16.43 15.03 13.16 7.89 42 8 48.94 17.35 16.28 12.31 8.24 43 6 44.05 16.60 12.96 12.51 8.19 44 6 44.56 16.24 13.86 13.02 8.17 45 6 48.01 17.64 15.26 14.59 9.02 46 6 48.67 17.84 15.59 14.44 9.36 47 6 46.87 18.37 16.18 14.59 9.21 48 6 44.32 16.81 14.87 13.55 8.50 49 6 44.79 16.14 14.38 13.40 8.91 50 6 45.30 16.31 14.62 12.89 8.41 51 6 46.36 17.26 15.48 13.30 8.83 52 6 46.80 17.55 15.77 13.87 9.01 53 6 43.14 15.61 14.23 12.78 8.63 54 6 47.99 16.63 15.54 14.00 9.06 55 4 63.12 22.52 20.40 17.77 11.60 56 4 57.22 20.02 18.44 16.07 10.77 57 4 65.55 22.32 20.56 17.53 11.57 58 4 60.61 20.74 19.33 16.61 10.20 59 4 64.82 22.17 21.27 17.99 10.95 60 4 63.03 21.97 21.11 18.25 11.37 61 4 64.96 22.93 22.16 19.07 12.30 62 4 61.89 21.47 20.78 17.97 10.83 63 4 63.34 21.74 21.38 17.60 10.40 64 4 65.01 21.74 22.18 19.11 11.47 65 4 64.52 21.76 22.49 18.35 10.90 66 3 41.34 15.17 12.84 11.87 7.26 67 3 48.11 18.45 15.83 14.28 8.65 68 3 47.59 17.37 15.01 13.34 8.67 69 3 49.25 17.87 15.64 13.74 8.82 70 3 44.82 16.47 14.42 13.05 8.35 71 3 46.21 16.71 14.70 12.88 8.57 72 3 56.24 20.34 18.08 14.72 9.22 73 3 53.38 19.64 17.51 14.82 9.12 74 3 52.59 19.16 17.23 14.44 9.46 75 3 46.27 16.19 14.57 12.39 8.20 76 3 49.18 17.68 15.96 14.03 8.98 77 3 53.90 20.01 18.20 14.89 9.46 78 2 47.62 18.08 15.88 15.02 9.64 79 2 48.20 18.04 15.89 14.89 9.23 80 2 46.27 16.55 15.19 14.04 8.64 81 2 54.04 18.73 17.68 15.79 3.97 82 1 53.34 19.76 16.52 15.04 9.36 83 1 50.41 17.85 14.96 14.24 8.80 84 1 51.71 19.12 16.22 15.16 9.37 85 1 49.73 16.91 14.44 13.49 8.64 86 1 51.73 18.79 16.07 14.58 8.51 87 1 54.50 20.21 17.33 15.45 9.68 88 1 54.08 19.96 17.15 15.76 9.78 89 1 53.26 18.25 15.70 14.76 9.35 90 1 53.20 18.63 16.07 14.79 9.57 91 1 47.54 16.61 14.33 13.56 8.33 92 1 51.20 18.22 15.73 14.46 9.07 93 1 53.56 19.31 16.68 15.33 9.19 94 1 48.11 17.11 14.80 13.98 8.15 95 1 48.10 17.08 14.78 14.14 8.52 96 1 55.97 20.96 18.15 16.06 10.06 97 1 56.18 20.43 17.81 16.18 10.27 98 1 51.21 18.67 16.34 15.03 9.55 99 1 52.25 18.67 16.36 14.26 8.88 100 1 51.78 18.58 16.30 14.91 9.21 101 1 51.39 18.26 16.02 14.57 9.54 102 1 48.45 17.55 15.41 14.16 8.56 103 1 54.30 19.08 16.80 15.22 9.69 104 1 49.33 16.67 14.73 13.84 8.04 105 1 53.41 19.05 16.84 14.86 9.70 106 1 48.80 18.56 16.44 14.88 9.31 107 1 49.70 18.08 16.03 14.69 9.60 108 1 48.28 16.74 14.87 13.72 8.45 109 1 51.35 17.86 15.92 14.22 9.07 110 1 51.01 17.26 15.39 14.63 9.47 111 1 49.89 17.17 15.31 14.21 9.04 112 1 54.15 19.69 17.58 15.38 10.17 113 1 56.28 20.37 18.20 15.83 10.49 114 1 55.30 19.96 17.86 15.66 10.93 115 1 44.53 15.56 13.96 12.69 7.81 116 1 54.15 18.86 16.93 15.08 9.82 117 1 50.59 17.75 15.95 14.66 9.25 118 1 52.55 19.63 17.71 15.21 9.98 119 1 53.86 19.56 17.66 15.18 9.52 120 1 49.91 17.75 16.04 14.21 8.98 121 1 52.78 19.21 17.37 15.49 9.92 122 1 51.94 18.79 17.00 14.79 10.14 123 1 56.60 20.19 18.29 15.86 10.03 124 1 51.23 17.66 16.02 14.37 9.30 125 1 51.80 19.05 17.30 15.12 10.23 126 1 52.78 18.52 16.85 14.80 9.58 127 1 51.41 18.13 16.53 14.36 9.02 128 1 50.41 18.10 16.52 14.87 9.18 129 1 53.48 18.69 17.07 15.19 9.52 130 1 53.87 18.60 17.00 15.28 9.92 131 1 51.98 18.51 16.93 15.12 9.52 132 1 50.56 18.13 16.61 15.12 8.94 133 1 50.64 18.16 16.68 14.38 9.04 134 1 53.06 18.56 17.06 14.74 9.68 135 1 54.64 18.96 17.44 15.61 9.81 136 1 48.13 16.91 15.56 14.30 8.96 137 1 57.97 21.17 19.48 17.01 10.99 138 1 49.93 18.24 16.80 15.20 9.14 139 1 47.38 17.02 15.72 14.18 9.14 140 1 54.24 19.01 17.60 15.18 9.86 141 1 50.17 17.59 16.31 14.53 9.23 142 1 49.78 16.77 15.55 13.83 9.12 143 1 51.81 18.41 17.09 14.87 9.82 144 1 50.84 18.71 17.38 14.79 9.42 145 1 49.57 16.41 15.26 13.35 8.80 146 1 55.81 19.27 17.92 16.06 9.97 147 1 58.03 20.13 18.73 16.28 10.50 148 1 48.72 16.62 15.50 14.06 8.60 149 1 48.39 17.12 15.97 14.01 9.13 150 1 49.01 18.24 17.10 14.76 9.54 151 1 50.31 18.23 17.10 15.15 9.76 152 1 49.10 17.15 16.11 14.47 8.83 153 1 60.04 21.20 19.95 17.53 11.53 154 1 46.99 16.87 15.97 13.93 9.10 155 1 53.63 19.00 18.08 15.89 10.03 156 1 57.88 20.21 19.35 17.34 10.18 157 1 54.89 20.06 19.27 16.76 10.78 158 1 53.56 19.18 18.52 16.39 10.64 159 1 51.50 18.23 17.62 15.21 9.62 160 1 63.47 22.12 21.40 17.79 11.65 161 1 51.46 17.21 16.90 15.37 10.00> frogspca<-prcomp(frogs[2:6],scale=TRUE) > summary(frogspca)Importance of components: PC1 PC2 PC3 PC4 PC5 Standard deviation 2.0950 0.65315 0.27219 0.25087 0.21737 Proportion of Variance 0.8778 0.08532 0.01482 0.01259 0.00945 Cumulative Proportion 0.8778 0.96315 0.97796 0.99055 1.00000> plot(frogspca) > biplot(frogspca) > plot( frogspca$x[,1], frogspca$x[,2] , type="n", xlab="PC1", ylab="PC2") > text( frogspca$x[,1], frogspca$x[,2], labels=c(Genetic.species)) >Thank you very much for any suggestions you might have, John Romansic [[alternative HTML version deleted]]
Jinsong Zhao
2014-Aug-19 00:41 UTC
[R] principle component values on PCA plots do not match
Hi, There is a scale factor associated with biplot when plotting the PCA result. Please read the help page of biplot.princomp or/and the source code of this function. HIH, Jinsong On 2014/8/18 16:31, John Romansic wrote:> Hi all, > > I am using prcomp to do Principle Components Analysis and have run into a > problem regarding the scale of the axes on my plots. I am using prcomp to > analyze a set of 25 morphological measurements taken on each of 161 > individual frogs. I used the biplot function to produce a figure of PC1 vs > PC2 for each of the individual frogs and arrows that represent the loadings > of the different morphological measurements on PC1 and PC2. Then I > constructed a separate, similar plot, using my own coding, that provides a > number for each individual frogs corresponding to its species as determined > by genetic analysis, without the arrows. The y-axis on the first plot > ranges shows PC2 values ranging from about -11 to 11, but the y-axis on the > second plot shows PC2 values ranging from about -4 to 4, although all the > data points seem to show up on the figure. I would like to show the same > PCA results on both plots and I do not understand why the y-axes on these > two plots do not match. By the way, the x-axes on these two plots seem to > match, but I think that is just a coincidence. I suspect that my coding for > the second plot is missing a command regarding scaling of the principle > components, but it's not obvious to me why those data would have to be > re-scaled. I thought the scaling is dealt with by prcomp. Within prcomp, I > used scale=TRUE, which I understand re-scales the original data so that all > the variables have equal variance. > > Does anyone have any suggestions on what might be wrong with my coding? > > Below is an example, using a truncated data set, which produces different > numbers than the example I described above, since this time only 5 > measurements are included for each frog. Nevertheless, the second plot (the > one with my coding) has the same problem. > >> frogs > Genetic.species SVL HL HW BED FED > 1 27 47.75 16.46 14.63 13.27 8.29 > 2 25 44.87 16.35 14.67 13.50 8.69 > 3 24 52.57 19.82 16.62 15.12 9.35 > 4 24 56.60 21.18 19.55 17.73 11.09 > 5 20 47.66 18.48 16.33 14.98 9.37 > 6 19 65.96 23.33 20.00 18.88 12.19 > 7 19 58.67 21.12 18.12 17.37 10.47 > 8 19 64.33 23.16 19.99 19.20 11.88 > 9 19 61.03 21.98 19.42 18.22 1.16 > 10 19 62.88 21.90 19.71 17.92 11.65 > 11 19 62.77 21.31 19.30 18.47 11.85 > 12 19 58.64 21.30 19.55 18.17 11.14 > 13 19 63.40 20.74 19.59 18.25 11.03 > 14 19 58.22 19.87 18.82 16.85 10.73 > 15 18 55.43 19.80 18.27 16.01 9.52 > 16 17 60.46 21.40 19.37 16.67 10.44 > 17 16 58.32 19.48 17.55 16.42 9.96 > 18 14 44.64 16.35 14.41 12.77 8.42 > 19 14 48.32 17.88 16.23 14.34 9.01 > 20 13 42.32 15.43 14.40 12.47 7.43 > 21 13 41.87 15.97 14.97 12.46 7.42 > 22 13 45.71 16.87 15.98 13.08 7.77 > 23 12 47.38 17.48 15.77 13.99 9.08 > 24 12 50.28 18.48 17.28 14.87 9.46 > 25 12 48.00 18.52 17.60 15.40 9.63 > 26 12 50.98 18.61 17.87 16.00 9.59 > 27 12 50.76 18.24 17.61 17.72 9.54 > 28 12 50.83 18.90 18.28 15.02 9.53 > 29 11 46.80 19.13 17.10 15.22 9.01 > 30 10 37.55 14.21 12.42 10.79 6.15 > 31 10 40.39 15.32 13.81 11.89 6.80 > 32 10 42.39 14.77 14.72 11.87 6.78 > 33 9 44.08 16.37 15.99 13.09 8.50 > 34 9 47.36 16.16 16.14 13.50 7.72 > 35 8 41.43 15.53 12.73 11.01 6.37 > 36 8 41.45 15.63 13.68 11.32 7.20 > 37 8 38.86 13.82 12.13 10.39 6.28 > 38 8 40.51 14.25 12.68 10.40 6.41 > 39 8 44.64 15.37 13.75 11.37 6.82 > 40 8 45.08 15.64 14.25 11.17 6.68 > 41 8 45.10 16.43 15.03 13.16 7.89 > 42 8 48.94 17.35 16.28 12.31 8.24 > 43 6 44.05 16.60 12.96 12.51 8.19 > 44 6 44.56 16.24 13.86 13.02 8.17 > 45 6 48.01 17.64 15.26 14.59 9.02 > 46 6 48.67 17.84 15.59 14.44 9.36 > 47 6 46.87 18.37 16.18 14.59 9.21 > 48 6 44.32 16.81 14.87 13.55 8.50 > 49 6 44.79 16.14 14.38 13.40 8.91 > 50 6 45.30 16.31 14.62 12.89 8.41 > 51 6 46.36 17.26 15.48 13.30 8.83 > 52 6 46.80 17.55 15.77 13.87 9.01 > 53 6 43.14 15.61 14.23 12.78 8.63 > 54 6 47.99 16.63 15.54 14.00 9.06 > 55 4 63.12 22.52 20.40 17.77 11.60 > 56 4 57.22 20.02 18.44 16.07 10.77 > 57 4 65.55 22.32 20.56 17.53 11.57 > 58 4 60.61 20.74 19.33 16.61 10.20 > 59 4 64.82 22.17 21.27 17.99 10.95 > 60 4 63.03 21.97 21.11 18.25 11.37 > 61 4 64.96 22.93 22.16 19.07 12.30 > 62 4 61.89 21.47 20.78 17.97 10.83 > 63 4 63.34 21.74 21.38 17.60 10.40 > 64 4 65.01 21.74 22.18 19.11 11.47 > 65 4 64.52 21.76 22.49 18.35 10.90 > 66 3 41.34 15.17 12.84 11.87 7.26 > 67 3 48.11 18.45 15.83 14.28 8.65 > 68 3 47.59 17.37 15.01 13.34 8.67 > 69 3 49.25 17.87 15.64 13.74 8.82 > 70 3 44.82 16.47 14.42 13.05 8.35 > 71 3 46.21 16.71 14.70 12.88 8.57 > 72 3 56.24 20.34 18.08 14.72 9.22 > 73 3 53.38 19.64 17.51 14.82 9.12 > 74 3 52.59 19.16 17.23 14.44 9.46 > 75 3 46.27 16.19 14.57 12.39 8.20 > 76 3 49.18 17.68 15.96 14.03 8.98 > 77 3 53.90 20.01 18.20 14.89 9.46 > 78 2 47.62 18.08 15.88 15.02 9.64 > 79 2 48.20 18.04 15.89 14.89 9.23 > 80 2 46.27 16.55 15.19 14.04 8.64 > 81 2 54.04 18.73 17.68 15.79 3.97 > 82 1 53.34 19.76 16.52 15.04 9.36 > 83 1 50.41 17.85 14.96 14.24 8.80 > 84 1 51.71 19.12 16.22 15.16 9.37 > 85 1 49.73 16.91 14.44 13.49 8.64 > 86 1 51.73 18.79 16.07 14.58 8.51 > 87 1 54.50 20.21 17.33 15.45 9.68 > 88 1 54.08 19.96 17.15 15.76 9.78 > 89 1 53.26 18.25 15.70 14.76 9.35 > 90 1 53.20 18.63 16.07 14.79 9.57 > 91 1 47.54 16.61 14.33 13.56 8.33 > 92 1 51.20 18.22 15.73 14.46 9.07 > 93 1 53.56 19.31 16.68 15.33 9.19 > 94 1 48.11 17.11 14.80 13.98 8.15 > 95 1 48.10 17.08 14.78 14.14 8.52 > 96 1 55.97 20.96 18.15 16.06 10.06 > 97 1 56.18 20.43 17.81 16.18 10.27 > 98 1 51.21 18.67 16.34 15.03 9.55 > 99 1 52.25 18.67 16.36 14.26 8.88 > 100 1 51.78 18.58 16.30 14.91 9.21 > 101 1 51.39 18.26 16.02 14.57 9.54 > 102 1 48.45 17.55 15.41 14.16 8.56 > 103 1 54.30 19.08 16.80 15.22 9.69 > 104 1 49.33 16.67 14.73 13.84 8.04 > 105 1 53.41 19.05 16.84 14.86 9.70 > 106 1 48.80 18.56 16.44 14.88 9.31 > 107 1 49.70 18.08 16.03 14.69 9.60 > 108 1 48.28 16.74 14.87 13.72 8.45 > 109 1 51.35 17.86 15.92 14.22 9.07 > 110 1 51.01 17.26 15.39 14.63 9.47 > 111 1 49.89 17.17 15.31 14.21 9.04 > 112 1 54.15 19.69 17.58 15.38 10.17 > 113 1 56.28 20.37 18.20 15.83 10.49 > 114 1 55.30 19.96 17.86 15.66 10.93 > 115 1 44.53 15.56 13.96 12.69 7.81 > 116 1 54.15 18.86 16.93 15.08 9.82 > 117 1 50.59 17.75 15.95 14.66 9.25 > 118 1 52.55 19.63 17.71 15.21 9.98 > 119 1 53.86 19.56 17.66 15.18 9.52 > 120 1 49.91 17.75 16.04 14.21 8.98 > 121 1 52.78 19.21 17.37 15.49 9.92 > 122 1 51.94 18.79 17.00 14.79 10.14 > 123 1 56.60 20.19 18.29 15.86 10.03 > 124 1 51.23 17.66 16.02 14.37 9.30 > 125 1 51.80 19.05 17.30 15.12 10.23 > 126 1 52.78 18.52 16.85 14.80 9.58 > 127 1 51.41 18.13 16.53 14.36 9.02 > 128 1 50.41 18.10 16.52 14.87 9.18 > 129 1 53.48 18.69 17.07 15.19 9.52 > 130 1 53.87 18.60 17.00 15.28 9.92 > 131 1 51.98 18.51 16.93 15.12 9.52 > 132 1 50.56 18.13 16.61 15.12 8.94 > 133 1 50.64 18.16 16.68 14.38 9.04 > 134 1 53.06 18.56 17.06 14.74 9.68 > 135 1 54.64 18.96 17.44 15.61 9.81 > 136 1 48.13 16.91 15.56 14.30 8.96 > 137 1 57.97 21.17 19.48 17.01 10.99 > 138 1 49.93 18.24 16.80 15.20 9.14 > 139 1 47.38 17.02 15.72 14.18 9.14 > 140 1 54.24 19.01 17.60 15.18 9.86 > 141 1 50.17 17.59 16.31 14.53 9.23 > 142 1 49.78 16.77 15.55 13.83 9.12 > 143 1 51.81 18.41 17.09 14.87 9.82 > 144 1 50.84 18.71 17.38 14.79 9.42 > 145 1 49.57 16.41 15.26 13.35 8.80 > 146 1 55.81 19.27 17.92 16.06 9.97 > 147 1 58.03 20.13 18.73 16.28 10.50 > 148 1 48.72 16.62 15.50 14.06 8.60 > 149 1 48.39 17.12 15.97 14.01 9.13 > 150 1 49.01 18.24 17.10 14.76 9.54 > 151 1 50.31 18.23 17.10 15.15 9.76 > 152 1 49.10 17.15 16.11 14.47 8.83 > 153 1 60.04 21.20 19.95 17.53 11.53 > 154 1 46.99 16.87 15.97 13.93 9.10 > 155 1 53.63 19.00 18.08 15.89 10.03 > 156 1 57.88 20.21 19.35 17.34 10.18 > 157 1 54.89 20.06 19.27 16.76 10.78 > 158 1 53.56 19.18 18.52 16.39 10.64 > 159 1 51.50 18.23 17.62 15.21 9.62 > 160 1 63.47 22.12 21.40 17.79 11.65 > 161 1 51.46 17.21 16.90 15.37 10.00 >> frogspca<-prcomp(frogs[2:6],scale=TRUE) >> summary(frogspca) > Importance of components: > PC1 PC2 PC3 PC4 PC5 > Standard deviation 2.0950 0.65315 0.27219 0.25087 0.21737 > Proportion of Variance 0.8778 0.08532 0.01482 0.01259 0.00945 > Cumulative Proportion 0.8778 0.96315 0.97796 0.99055 1.00000 >> plot(frogspca) >> biplot(frogspca) >> plot( frogspca$x[,1], frogspca$x[,2] , type="n", xlab="PC1", ylab="PC2") >> text( frogspca$x[,1], frogspca$x[,2], labels=c(Genetic.species)) >> > Thank you very much for any suggestions you might have, > John Romansic > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >