Joel Fürstenberg-Hägg
2009-Nov-05 16:21 UTC
[R] Set colors in a PCA plot based on a gradient vector
Hi all, I'm making a PCA plot with eight variables (columns taken from a larger data frame "fieldTrial0809[idx,c(39:46)]"). I then want the symbols in the plot to be colored as a gradient from red to blue, depending on the value of another column in "fieldTrial0809[idx, c(48)]" containg temperatures from -12.1 to -5.4. I don't want to use the heat.colors(n) function, because then I just get colors from red to yellow... I've created a vector "colTemperature" with 12 colors and I've also made a new vector "tempACC", based on the values of "fieldTrial0809[idx, c(48)]" with 12 levels. What I don't know, is how to incorporate this vector in the text() function below:> df=fieldTrial0809[idx,c(39:46)] > PC=prcomp (df[,1:8]) > hcl=hclust(dist(df))> plot(PC$x[,1],PC$x[,2], col="white", main="8 variables") > text(PC$x[,1],PC$x[,2], labels=cutree(hcl,8), col=cutree(hcl,8)) # Change this, but how???>> col1=rgb(0.164,0.043,0.850) # Set the colors > col2=rgb(0.150,0.306,1.000) > col3=rgb(0.250,0.630,1.000) > col4=rgb(0.450,0.853,1.000) > col5=rgb(0.670,0.973,1.000) > col6=rgb(0.880,1.000,1.000) > col7=rgb(1.000,1.000,0.750) > col8=rgb(1.000,0.880,0.600) > col9=rgb(1.000,0.679,0.450) > col10=rgb(0.970,0.430,0.370) > col11=rgb(0.850,0.150,0.196) > col12=rgb(0.650,0.000,0.130) > colTemperature=c(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12) # Put all colors in a vector > > tempLT50_ACC=fieldTrial0809[idx, c(48)] > tempACC=cut(tempLT50_ACC, breaks=c(-5.4, -5.958333333, -6.516666667, -7.075, -7.633333333, -8.191666667, -8.75, -9.308333333, -9.866666667, -10.425, -10.98333333, -11.54166667,-12.1)) # Define the breaks > table(tempACC)tempACC (-12.1,-11.5] (-11.5,-11] (-11,-10.4] (-10.4,-9.87] (-9.87,-9.31] (-9.31,-8.75] 25 21 24 21 131 28 (-8.75,-8.19] (-8.19,-7.63] (-7.63,-7.08] (-7.08,-6.52] (-6.52,-5.96] (-5.96,-5.4] 33 14 11 7 17 11 Finally, I've got another question, how do you decide how many components to use in "text(... labels=cutree(hcl,8), ...)"? Do you try with different values until i looks nice or what..? Best regards, Joel _________________________________________________________________ Hitta kärleken i vinter! http://dejting.se.msn.com/channel/index.aspx?trackingid=1002952 [[alternative HTML version deleted]]
On 11/06/2009 03:21 AM, Joel F?rstenberg-H?gg wrote:> Hi all, > > > > I'm making a PCA plot with eight variables (columns taken from a larger data frame "fieldTrial0809[idx,c(39:46)]"). I then want the symbols in the plot to be colored as a gradient from red to blue, depending on the value of another column in "fieldTrial0809[idx, c(48)]" containg temperatures from -12.1 to -5.4. > > > > I don't want to use the heat.colors(n) function, because then I just get colors from red to yellow... > > > > I've created a vector "colTemperature" with 12 colors and I've also made a new vector "tempACC", based on the values of "fieldTrial0809[idx, c(48)]" with 12 levels. > > > > What I don't know, is how to incorporate this vector in the text() function below: > > > >> df=fieldTrial0809[idx,c(39:46)] >> PC=prcomp (df[,1:8]) >> hcl=hclust(dist(df)) >> > >> plot(PC$x[,1],PC$x[,2], col="white", main="8 variables") >> text(PC$x[,1],PC$x[,2], labels=cutree(hcl,8), col=cutree(hcl,8)) # Change this, but how??? >>library(plotrix) text(...,col=color.scale(fieldTrial0809[idx,48], extremes=c("red","blue"))> Finally, I've got another question, how do you decide how many components to use in "text(... labels=cutree(hcl,8), ...)"? Do you try with different values until i looks nice or what..? > >As in so many other areas of life, beauty is in the eye of the beholder. Jim