Question

Here is a classical example found on the help page for plotTangentSpace in package geomorph. I just add two lines: the construction of the vector Myname and the following line that name one dimension of the array Y.gpa$coords according to Myname.

library (geomorph)
data(plethodon)
Y.gpa<-gpagen(plethodon$land)    #GPA-alignment
ref<-mshape(Y.gpa$coords)
Myname = 41:80
dimnames(Y.gpa$coords)=list(NULL, NULL, Myname)
plotTangentSpace(Y.gpa$coords, label=T)

As you can see, on the plot created by plotTangentSpace the points are labeled 1:40 rather than 41:80 as I aimed to do by renaming Y.gpa$coords. I'd like to label the points according to Myname. For this example my name is just a vector of numbers but I'd like it to works for character type also. How can I achieve this?

Était-ce utile?

La solution

If you have a look at the code for plotTangentSpace (just type it in the R console), you first find the 'argument list':

function (A, axis1 = 1, axis2 = 2, warpgrids = TRUE, label = FALSE) 

As you see you can turn labels 'on' or 'off' (label = TRUE or FALSE), but there is no argument for setting the actual value of the labels. Further down you find the code for the default, hard-coded labels (seq(1, n)) in two places:

    if (label == T) {
        text(pcdata[, axis1], pcdata[, axis2], seq(1, n), 
            adj = c(-0.7, -0.7))

...where pcdata, axis1, and n are defined in the beginning of the function.

Thus, if you want to set the value of the labels, it seems that you need to rewrite the function slightly. One possibility is to add a labels argument to the arglist:
function (A, axis1 = 1, axis2 = 2, warpgrids = TRUE, label = FALSE, labels = NULL)

...and change the arguments in both text calls:

  text(pcdata[, axis1], pcdata[, axis2], labels, 
       adj = c(-0.7, -0.7))

You also need to access the tps function in the geomorph namespace. This can be achived by adding geomorph::: before both instances of tps:

  geomorph:::tps(ref, shape.min, 20)
  geomorph:::tps(ref, shape.max, 20)

Then assign the updated function to a new function name:

plotTangentSpace2 <- function (A, axis1 = 1, axis2 = 2, warpgrids = TRUE, label = FALSE, labels = NULL){
lots-of-stuff
text(pcdata[, axis1], pcdata[, axis2], labels, adj = c(-0.7, -0.7)) # in both places
more-stuff
geomorph:::tps(ref, shape.min, 20)
geomorph:::tps(ref, shape.max, 20)
}

Plot with the updated function, using 'Myname' as labels:
plotTangentSpace2(Y.gpa$coords, label = TRUE, labels = Myname) enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top