How to put the specimen ID in the graphics

Hi! I'm a beginner in R.
I have a question important to me that I don't know how to solve. I would like to see the ID (i.e., catalog number) of each individual in my graphics (PCA, Regression..) to identify each one of them. The ID's are written in the TPS file that I have imported into R, my problem is that I don't know what function I have to use to make them appear in the graphics. I used the function: text(PCA$x, PCA$y, row.names(gpa$coords)) but I don't get the IDs but the numbers. I would be very grateful if you could help me.

My workflow is as follows:

library(geomorph)
tps <-readland.tps(file.choose("Semilandmarks_File.tps"), specID = c("ID"), readcurves = TRUE, warnmsg = T)
slides <- define.sliders(c(2:8))
gpa <- gpagen(A=tps,curves = slides, PrinAxes = TRUE, Proj = TRUE, ProcD=FALSE, print.progress = TRUE)
gdf <- geomorph.data.frame(gpa) attributes(gpa)
PCA <- gm.prcomp(gpa$coords) summary(PCA)
plot(PCA)
> text(PCA$x, PCA$y, row.names(gpa$coords))

Many thanks!! :slight_smile:

Hi,

Welcome to the RStudio community!

I'm not familiar with the gpagen function, so I don't know what the data frame looks like, but normally the approach you took should work, unless there is are no rownames in the data frame. Here is a working example

myData = data.frame(x = 1:10, y = runif(10), row.names = letters[1:10])

plot(myData$x, myData$y)
text(myData$x, myData$y, rownames(myData), offset = 0.3, pos = 4)

Created on 2020-08-10 by the reprex package (v0.3.0)

Make sure the gpa$coords object is a dataframe with rownames. If you keep having trouble, create a reprex so we can run your code. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Finally, you could consider using ggplot instead of the basic plot function, as this is way move powerful and versatile. The geom_label or geom_text will do the trick there.

Hope this helps,
PJ

1 Like

Hi!

Many thanks for that very good and very exhaustive reply.

I have used your working example and, as in your image, letters appear in each specimen of my graph. However, I still don't see the catalog number of the individuals. As you mention, I think that my problem is that the gpa$coords object is not a dataframe with rownames. After doing it, I can see the IDs in the console (see attached file). Although, I still don't know how to represent them in the graph. Even with the gpa$coords object as dataframe with rownames, the labels don't appear on the PCA graph. I will try to familiarize myself with the ggplot package and see if I can fix the problem.

Thanks so much for the help.

A.

Hi,

You can access that with the dimnames function I think:

dimnames(gpa$coords)[[3]]

Hope this helps,
PJ

1 Like

And to visualize it would it be something like this?:

plot(PCA)
dimnames(gpa$coords)[[3]]
> text(PCA$x, PCA$y, row.names(dimnames(gpa$coords)[[3]]))

Sorry if it's a silly question. Thank you very much!!!

Hi,

No without the rownames :slight_smile:

plot(PCA)
text(PCA$x, PCA$y, dimnames(gpa$coords)[[3]])

PJ

1 Like

Oh, great!!! I finally succeeded, I'm so happy :))

THANK YOU SO MUCH PJ

1 Like

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