1
$\begingroup$

I made a PCA plot and was trying to plot the eigenvenctors in R so that each data point is actually the sample name. All the samples are in column 1. The following are the R commands I am using:

    fn <- "Plot.txt"
    evecdat <- read.table(fn, col.names=c("Sample", "PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8", "PC9", "PC10", "pc11","pc12", "pc13", "pc14", "pc15","pc16", "pc17", "pc18", "pc19", "pc20", "pop", "color"))
    jpeg("Plot1.jpeg", width = 20, height = 20, units = 'in', res = 300)
    identify(evecdat$PC1, evecdat$PC2  = NULL, labels = seq_along(evecdat$Sample), pos = TRUE, n = length(evecdat$Sample), plot = FALSE, atpen = FALSE, offset = 0.5, tolerance = 0.25, …)

    plot(evecdat$PC1, evecdat$PC2, col=as.vector(evecdat$color), xlab="PC1", ylab="PC2", pch="24", cex.lab=2.5, cex=1.5, cex.axis=1)
    legend("topright", ncol=1, legend=c("X", "Y", "Z"), col=c("Blue", "Red", "Green"), pch="24", cex=2, bty = "o")
    while (!is.null(dev.list())) dev.off()

Here, all the row names are under the Sample column name that I defined earlier.

$\endgroup$
1
  • $\begingroup$ We don't have the "Plot.txt" data file so we can't test, but what problem do you have with the code? You get the plot without the name of the samples? Have you looked at text? $\endgroup$
    – llrs
    Commented Sep 12, 2018 at 7:39

1 Answer 1

2
$\begingroup$

Maybe try

plot(evecdat$PC1, evecdat$PC2, xlab="PC1", ylab="PC2", pch="24", cex.lab=2.5, cex.axis=1, type="n")  
text(evecdat$PC1, evecdat$PC2, evecdat$Sample, col=as.vector(evecdat$color))

This should plot the sample names as text (taken from the evecdat$Sample column), colored as specified in the evecdat$color column. See ?text for details.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.