unable to change labels in PCA prcomp from row numbers to site names

434 Views Asked by At

I have followed the answers in https://stackoverflow.com/questions/38900698/how-to-change-labels-from-pca-using-prcomp-to-sample-names but am unable to get it to work. I have set up a dummy file (called tf4) with 7 columns and 12 rows and imported it into R Studio. The first few lines look like this

head(tf4)

# A tibble: 6 x 7
    sp1   sp2   sp3   sp4   sp5   sp6 Site 
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1     1     1     1     1     1     1 A1   
2     0     1     1     1     1     0 A2   
3     0     1     1     1     1     1 B1   
4     1     1     1     1     1     0 C1   
5     0     0     1     1     0     1 D4   
6     1     0     1     1     1     0 E7

I then set the row names to column 7

#setting the row names to column 7

row.names(tf4)<-tf4$Site
head(tf4)

and get a warning message

Warning message: Setting row names on a tibble is deprecated.

and when I inspect again using head(tf4) the row names have not been reset and remain as before.

But when I look at the file in R studio 'environment', it has reset the row names

snip and sketch image of the file in environment

however, when I run prcomp

model<-prcomp(tf4[,-7],scale=TRUE)
biplot(model)

the site identifiers are not used in the plot

snip and sketch view of biplot

I cannot work this out. It seems that setting the row names to column 7 is not working correctly.

  1. Am I missing a step?

  2. do I need an extra instruction in prcomp?

2

There are 2 best solutions below

0
On

The example given by Serkan indicated where I was going wrong. It showed a rownames instruction between the prcomp and the biplot. So with my own data file I tried this

model<-prcomp(tf4[,-7],scale=TRUE)

rownames<-tf4$Site

biplot(model)

and the biplot was successfully labelled snip and sketch image of successful biplot so my conclusion is that I was setting rownames too early.

0
On

I am not quite certain what you are looking for. But I think this is it;

# PCA with mtcars; ###
pca <- prcomp(mtcars[,c(1,2)], scale. = TRUE)

# Rename Rownames of Components; ####
rownames(pca$rotation) <- c("Row 1", "Row 2")

# Biplot; ####
biplot(pca)

This changes the names from mpg and cyl to Row 1 and Row 2. Let me know if it is this you are looking for. However, without specific codes I, at least, cant tell where you went wrong.

Best.