How can I get my labels to move with my data?

Lucas T Source

I am currently using MethylKit to analyze and plot my data and I would like to use visualize my data. I am working on a server and have automated the script and it seems that while the plot will change, the location of the labels will not, as seen in the two plots I've attached below.

My code is as follows:

extractmeth <- getData(meth)
isometh = matrix(nrow=nrow(meth), ncol=length(ids))
colnames(isometh) <- ids
project.pca <- prcomp(t(isometh))

#Determine the proportion of variance of each component
project.pca.proportionvariances <- ((project.pca$sdev^2) / (sum(project.pca$sdev^2)))*100


#Biplots
par(mar=c(4,4,4,4), mfrow=c(1,3), cex=1.0, cex.main=0.8, cex.axis=0.8)

#Plots scatter plot for PC 1 and 2
pdf(file="PCABiplotsPC1-2.pdf")
plot(project.pca$x, 
     type = "n", 
     main = "Principal components analysis bi-plot", 
     xlab = paste("PC1, ", round(project.pca.proportionvariances[1], 2), "%"),
     ylab = paste("PC2, ", round(project.pca.proportionvariances[2], 2), "%")
)
points(project.pca$x, col="black", pch=16, cex=1)
text(project.pca$x, labels=ids, cex= 0.7, pos=3)
dev.off()

#Plots scatter plot for PC 1 and 3
pdf(file="PCABiplotsPC1-3.pdf")
plot(project.pca$x[,1], project.pca$x[,3], 
     type = "n", 
     main = "Principal components analysis bi-plot", 
     xlab = paste("PC1, ", round(project.pca.proportionvariances[1], 2), "%"), 
     ylab = paste("PC3, ", round(project.pca.proportionvariances[3], 2), "%")
)
points(project.pca$x[,1], project.pca$x[,3], col="black", pch=16, cex=1)
text(project.pca$x, labels=ids, cex= 0.7, pos=3)
dev.off()

Actually, in the process of writing this up, I noticed that in the code for the second plot, instead of:

text(project.pca$x, labels=ids, cex= 0.7, pos=3)

should it be:

text(project.pca$x[,1], project$x[,3], labels=ids, cex= 0.7, pos=3)

?

First PCA Plot

Second PCA Plot

r

Answers

answered 3 months ago dww #1

Since you do not specify the y coordinate in you text call, what happens is that "if y is missing, xy.coords(x, y) is used for construction of the coordinates."

When xy.coords is passed a data.frame or matrix, it assumes that the 1st column is x, and the 2nd is y. In your example, this means that project.pca$x which contains coordinates for all of the principal components is interpreted in both calls to text as providing the x,y coordinates in its first two columns.

So, to get the correct coordincates for PC1 and PC3, you can use

text(project.pca$x[,c(1,3)], labels=ids, cex= 0.7, pos=3)

or

text(x = project.pca$x[,1], y = project.pca$x[,3], labels=ids, cex= 0.7, pos=3)

comments powered by Disqus