更改plotTangentSpace在" geomorph"中创建的绘图上的点标签包

时间:2013-11-14 10:48:45

标签: arrays r graph plot pca

以下是包plotTangentSpacegeomorph帮助页面上的经典示例。我只添加两行:向量Myname的构造以及根据Y.gpa$coords命名数组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)

正如您所看到的,在plotTangentSpace创建的情节中,点数标记为1:40而不是41:80,因为我的目标是重命名Y.gpa$coords。我想根据Myname标记点数。对于这个例子,我的名字只是一个数字向量,但我也希望它适用于字符类型。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

如果你看一下plotTangentSpace的代码(只需在R控制台中输入它),你首先找到'参数列表':

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

如您所见,您可以将标签'打开'或'关闭'(label = TRUEFALSE),但没有关于设置标签实际值的参数。再向下,您可以在两个位置找到默认的硬编码标签(seq(1, n))的代码:

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

...其中pcdataaxis1n在函数的开头定义。

因此,如果要设置标签的值,似乎需要稍微重写该函数。一种可能性是向labels添加arglist参数:
function (A, axis1 = 1, axis2 = 2, warpgrids = TRUE, label = FALSE, labels = NULL)

...并更改text次调用中的参数:

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

您还需要访问tps命名空间中的geomorph函数。这可以通过在geomorph:::

的两个实例之前添加tps来实现
  geomorph:::tps(ref, shape.min, 20)
  geomorph:::tps(ref, shape.max, 20)

然后将更新的函数分配给新的函数名称:

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)
}

使用更新的功能绘图,使用'Myname'作为标签:
plotTangentSpace2(Y.gpa$coords, label = TRUE, labels = Myname) enter image description here