How to label by colour in PCA plot?
0
0
Entering edit mode
10 weeks ago
Info.shi ▴ 30

Hi everyone.

I plot PCA using below command.

data1 <- data[,apply(data, 2, function(x) all(x > 0))]

pca.result <- prcomp(data1,center = TRUE, scale. = TRUE)

autoplot(pca.result, label=TRUE)


I want to label all .Non in one color and .Acc in one color.

I tried using ggplot but in that case which file I should retrieve is problem.

Kindly please me code to label the color using autoplot.

Thank you.

label PCA plot. colure • 755 views
0
Entering edit mode

You need to hand both the pca.result, and data1 with a column of the identifiers to the plot function. So if your column of .Non and .Acc is called "type", you would say:

autoplot(pca.result, data = data1, colour = 'type')


The cran docs for how to plot pca might help.

0
Entering edit mode

Thank you but I want different color for all .Non (Red) and .Non (Blue).

But this autoplot(pca.result, data = data1, colour = 'type') convert all in one colour.

That would be great if you can suggest me more.

Thank you.

0
Entering edit mode

If it's giving them all the same color, then they must all have the same label in the 'type' column. Can you provide a few rows of your data? Here's an example of a few rows of data that would get unique colors for each point type, because there are distinct labels for each type:

       C1     C2     C3     C4 type
g1  0.360  1.490  0.505 -0.800  Non
g2  1.270  0.939 -0.438  0.194  Non
g3 -0.179 -1.320 -0.963  1.400  Acc
g4  0.221  0.303 -0.464 -0.484  Acc
g5 -1.010  1.590 -0.576  0.689  Non


What does your label column look like?

0
Entering edit mode

Thank you so much.

My matrix file look like as follows.

0
Entering edit mode

That's the PCA result. What we need to see is the first few lines of the input data matrix: data1. If the data1 matrix has the same values as in the "Replicate" column of the PCA result, then each dot will get a different color, because each line has a unique value (1.Acc, 2.Acc). If you want to classify them, as Acc or Nom, you night have to create a column for just those values. Ether way, showing the first few lines of data1 would be informative.