How can I add tags to my points on a PCA with ggplot2?

library(ggfortify)
setwd("C:/Users/M4NU.51LV4/Desktop/Pca en R")

chi=data.frame(pca2)

chicucha<- chi[c(1, 2, 3, 4)]
autoplot(prcomp(chicucha))
autoplot(prcomp(chicucha), data = chi, colour = 'GT')
autoplot(prcomp(chicucha), data = chi, colour = 'GT', loadings = TRUE)

Thanks, I managed to put the names, but these overlap, is there any code to avoid that?

Thanks, I managed to put the names, but these overlap, is there any code to avoid that

There is an extention package called ggrepel that you can use for that.

You could also try including the argument check_overlap = TRUE inside of your geom_text() call (but outside of aes()), and see if that helps.

how could I add it in the following code?

autoplot(prcomp(chicucha), data = chi, colour = 'GT', label = TRUE, loadings = TRUE)

how could I add it in the following code?

autoplot(prcomp(chicucha), data = chi, colour = 'GT', label = TRUE, loadings = TRUE)

There is no need to repeat your posts we all can see them.

As I said before, if you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Could you post the table chicucha for us? (The data you provided earlier, and which @williaml kindly posted, won't allow us to reproduce the steps you took.)

The easiest way to do this is to use the dput() function and post its output between a pair of triple backticks (```) like this:

```
[paste output of dput(chicucha) here]
```

If chicucha is big table, then you should use the output of dput(chicucha %>% slice(1:50)) instead -- this would be a big help to us in trying to help you.

Thank you very much for your support, I am new to the R environment, thanks for your answers, I hope this time I do it well

dput(chi)

chi
library(ggfortify)
library(ggplot2)
rownames(chi)<- chi$Taxon
chicucha<- chi[c(2, 3, 4, 5)]
chicucha
autoplot(prcomp(chicucha))
autoplot(prcomp(chicucha), data = chi, colour = 'GT')
autoplot(prcomp(chicucha), data = chi, colour = 'GT', label = TRUE, loadings = TRUE)

OK, in that case, you should post the output from dput(chi), meaning, you should copy the output, and paste it here between triple backticks (```), like this,

```
[paste output of dput(chi) here]
```

You need the triple backticks so we can copy it easily.

dput(chi)
chi

                         "Austrelmis", "Prionocyphon", "Neoplasta", "Gigantodax", "Simulium", 
                         "Tipula", "Alotanypus", "Pentaneura", "Corynoneura", "Cricotopus", 
                         "Tanytarsus", "Podonomus", "Podonomopsis", "Parochlus"), MOPF = c(75, 
                                                                                           5, 20, 93, 40, 98, 65, 30, 30, 60, 35, 15, 90, 95, 25, 85, 95
                         ), MA = c(25, 95, 0, 7, 0, 2, 15, 65, 0, 20, 5, 85, 10, 5, 75, 
                                   15, 5), RA = c(0, 0, 80, 0, 60, 0, 20, 5, 0, 20, 60, 0, 0, 0, 
                                                  0, 0, 0), TV = c(0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 
                                                                   0, 0), GT = c("detritívoro", "herbívoro", "depredador", "detritívoro", 
                                                                                 "depredador", "detritívoro", "detritívoro", "herbívoro", "herbívoro", 
                                                                                 "detritívoro", "depredador", "herbívoro", "detritívoro", "detritívoro", 
                                                                                 "herbívoro", "detritívoro", "detritívoro")), class = "data.frame", row.names = c("Andesiops", 
                                                                                                                                                                  "Meridialaris", "Polycentropus", "Austrelmis", "Prionocyphon", 
                                                                                                                                                                  "Neoplasta", "Gigantodax", "Simulium", "Tipula", "Alotanypus", 
                                                                                                                                                                  "Pentaneura", "Corynoneura", "Cricotopus", "Tanytarsus", "Podonomus", 
                                                                                                                                                                  "Podonomopsis", "Parochlus"))```¨

library(ggfortify)
library(ggplot2)
rownames(chi)<- chi$Taxon
chicucha<- chi[c(2, 3, 4, 5)]
chicucha
autoplot(prcomp(chicucha))
autoplot(prcomp(chicucha), data = chi, colour = 'GT')
autoplot(prcomp(chicucha), data = chi, colour = 'GT', label  = TRUE, loadings = TRUE)

That's better! But you cut off part of the output from dput(chi) -- could you try again and only paste the output of dput(chi) and don't include any other code?

                         "Austrelmis", "Prionocyphon", "Neoplasta", "Gigantodax", "Simulium", 
                         "Tipula", "Alotanypus", "Pentaneura", "Corynoneura", "Cricotopus", 
                         "Tanytarsus", "Podonomus", "Podonomopsis", "Parochlus"), MOPF = c(75, 
                                                                                           5, 20, 93, 40, 98, 65, 30, 30, 60, 35, 15, 90, 95, 25, 85, 95
                         ), MA = c(25, 95, 0, 7, 0, 2, 15, 65, 0, 20, 5, 85, 10, 5, 75, 
                                   15, 5), RA = c(0, 0, 80, 0, 60, 0, 20, 5, 0, 20, 60, 0, 0, 0, 
                                                  0, 0, 0), TV = c(0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 
                                                                   0, 0), GT = c("detritívoro", "herbívoro", "depredador", "detritívoro", 
                                                                                 "depredador", "detritívoro", "detritívoro", "herbívoro", "herbívoro", 
                                                                                 "detritívoro", "depredador", "herbívoro", "detritívoro", "detritívoro", 
                                                                                 "herbívoro", "detritívoro", "detritívoro")), class = "data.frame", row.names = c("Andesiops", 
                                                                                                                                                                  "Meridialaris", "Polycentropus", "Austrelmis", "Prionocyphon", 
                                                                                                                                                                  "Neoplasta", "Gigantodax", "Simulium", "Tipula", "Alotanypus", 
                                                                                                                                                                  "Pentaneura", "Corynoneura", "Cricotopus", "Tanytarsus", "Podonomus", 
                                                                                                                                                                  "Podonomopsis", "Parochlus"))```

Still cutting it off. How about you try running these commands, one at a time:

sink('dput_output.txt')
dput(chi)
sink()

This will send the output of the dput() command to a file called 'dput_output.txt', which you can then open, copy, and paste here. Could you try that?

structure(list(Taxon = c("Andesiops", "Meridialaris", "Polycentropus",
"Austrelmis", "Prionocyphon", "Neoplasta", "Gigantodax", "Simulium",
"Tipula", "Alotanypus", "Pentaneura", "Corynoneura", "Cricotopus",
"Tanytarsus", "Podonomus", "Podonomopsis", "Parochlus"), MOPF = c(75,
5, 20, 93, 40, 98, 65, 30, 30, 60, 35, 15, 90, 95, 25, 85, 95
), MA = c(25, 95, 0, 7, 0, 2, 15, 65, 0, 20, 5, 85, 10, 5, 75,
15, 5), RA = c(0, 0, 80, 0, 60, 0, 20, 5, 0, 20, 60, 0, 0, 0,
0, 0, 0), TV = c(0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0,
0, 0), GT = c("detritívoro", "herbívoro", "depredador", "detritívoro",
"depredador", "detritívoro", "detritívoro", "herbívoro", "herbívoro",
"detritívoro", "depredador", "herbívoro", "detritívoro", "detritívoro",
"herbívoro", "detritívoro", "detritívoro")), class = "data.frame", row.names = c("Andesiops",
"Meridialaris", "Polycentropus", "Austrelmis", "Prionocyphon",
"Neoplasta", "Gigantodax", "Simulium", "Tipula", "Alotanypus",
"Pentaneura", "Corynoneura", "Cricotopus", "Tanytarsus", "Podonomus",
"Podonomopsis", "Parochlus"))

This works, but in general you should place the output between a pair of triple backticks, like this:

```
[paste output of dput(chi) here]
```

One thing that confuses me is that if you apply prcomp() to this table, you don't get PCA coordinates for individual taxa; you get them for each of the numeric columns of chi -- how did you get taxon coordinates to make your plot?

This is how to use ggrepel with your sample data, please notice the way I'm posting my answer, that would be a proper reprex, if you are going to keep asking questions on the community is better if you put a little effort on following the reprex guide so you can make your examples this way.

library(ggfortify)
library(ggplot2)
library(ggrepel)

chi <- structure(list(Taxon = c("Andesiops", "Meridialaris", "Polycentropus",
                         "Austrelmis", "Prionocyphon", "Neoplasta", "Gigantodax", "Simulium",
                         "Tipula", "Alotanypus", "Pentaneura", "Corynoneura", "Cricotopus",
                         "Tanytarsus", "Podonomus", "Podonomopsis", "Parochlus"), MOPF = c(75,
                                                                                           5, 20, 93, 40, 98, 65, 30, 30, 60, 35, 15, 90, 95, 25, 85, 95
                         ), MA = c(25, 95, 0, 7, 0, 2, 15, 65, 0, 20, 5, 85, 10, 5, 75,
                                   15, 5), RA = c(0, 0, 80, 0, 60, 0, 20, 5, 0, 20, 60, 0, 0, 0,
                                                  0, 0, 0), TV = c(0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0,
                                                                   0, 0), GT = c("detritívoro", "herbívoro", "depredador", "detritívoro",
                                                                                 "depredador", "detritívoro", "detritívoro", "herbívoro", "herbívoro",
                                                                                 "detritívoro", "depredador", "herbívoro", "detritívoro", "detritívoro",
                                                                                 "herbívoro", "detritívoro", "detritívoro")), class = "data.frame", row.names = c("Andesiops",
                                                                                                                                                                  "Meridialaris", "Polycentropus", "Austrelmis", "Prionocyphon",
                                                                                                                                                                  "Neoplasta", "Gigantodax", "Simulium", "Tipula", "Alotanypus",
                                                                                                                                                                  "Pentaneura", "Corynoneura", "Cricotopus", "Tanytarsus", "Podonomus",
                                                                                                                                                                  "Podonomopsis", "Parochlus"))

rownames(chi)<- chi$Taxon
chicucha<- chi[c(2, 3, 4, 5)]

autoplot(prcomp(chicucha), data = chi, colour = 'GT', loadings = TRUE) +
    geom_text_repel(aes(label = Taxon, color = GT))

Created on 2020-03-05 by the reprex package (v0.3.0.9001)

Muchas gracias, disculpen las molestias.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.