【问题标题】:Changing axes labels for biplot() in R在 R 中更改 biplot() 的轴标签
【发布时间】:2016-09-26 20:57:51
【问题描述】:

我试图通过在 R 中制作双标图来可视化 PCoA{ape} 的结果。 轴现在获得默认标签轴 1 和轴 2,但我想编辑它。

这是我尝试过的代码:

biplot(pcoa.ntK, Y=NULL, plot.axes=c(1,2), rn=ntnames,
                           xlabs="PC1 (%)", ylabs="PC2 (%)")

但标签不会改变。 有人可以告诉我我在这里做错了什么吗? 我也想编辑标题,有没有人建议?

我的数据:

ntK <- matrix( 
  c(0.00000, 0.01500, 0.01832, 0.02061, 0.01902, 0.01270, 0.02111, 0.01655, 0.01520, 0.01691,
    0.01667, 0.00000, 0.01175, 0.01911, 0.01759, 0.01127, 0.01854, 0.01041, 0.00741, 0.02007,
    0.02432, 0.01404, 0.00000, 0.02551, 0.01972, 0.01838, 0.02505, 0.01484, 0.01391, 0.02687,
    0.01501, 0.01252, 0.01399, 0.00000, 0.01442, 0.01294, 0.01402, 0.01132, 0.01239, 0.01455,
    0.02343, 0.01951, 0.01830, 0.02440, 0.00000, 0.01727, 0.02470, 0.02021, 0.01699, 0.02482,
    0.01320, 0.01054, 0.01439, 0.01847, 0.01457, 0.00000, 0.01818, 0.01366, 0.00977, 0.01394,
    0.02468, 0.01950, 0.02206, 0.02251, 0.02343, 0.02040, 0.00000, 0.02028, 0.01875, 0.02558,
    0.02254, 0.01276, 0.01522, 0.02117, 0.02234, 0.01790, 0.02363, 0.00000, 0.01152, 0.02557,
    0.01804, 0.00792, 0.01244, 0.02019, 0.01637, 0.01116, 0.01904, 0.01004, 0.00000, 0.02099,
    0.01862, 0.01988, 0.02227, 0.02200, 0.02218, 0.01476, 0.02408, 0.02066, 0.01947, 0.00000), 
  nrow=10, 
  ncol=10)

library(ape)
ntnames <- c("A","B","C","D","E","F","G","H","I","J")
pcoa.ntK <- pcoa(ntK)

【问题讨论】:

  • rednt 数据怎么样?
  • 似乎rednt 应该是pcoa.ntKnamesaa 应该是ntnamesbiplot 调用。
  • 正如其他人在下面所说的那样,轴标题是硬编码的,它们的名称来自pcoa.ntK$vectors 的列名。所以一个快速的解决方案是重命名它。因此,如果您正在绘制前两台电脑。使用 colnames(pcoa.ntK$vectors)[1:2] &lt;- c("Mytitle1", "Mytitle2") ,然后像以前一样绘制。

标签: r axis-labels


【解决方案1】:

biplot 是一个通用函数。默认方法和用于使用 prcomp 包中的 prcomp 函数的对象的方法确实允许您指定轴标签和标题,但由于某种原因,编写被调用方法的人pcoa 类的对象不允许您指定它们。我认为您唯一的选择是编写您自己的biplot.pcoa 版本(或要求包维护者添加此选项)。

这是对 ape 包中函数的快速而肮脏的 hack,它可能会做你想做的事,但不能保证它不会破坏其他东西!

biplot.pcoa <- function (x, Y = NULL, plot.axes = c(1, 2), dir.axis1 = 1, dir.axis2 = 1, 
          rn = NULL, xlabs = NULL, ylabs = NULL, main = NULL, ...) 
{
  k <- ncol(x$vectors)
  if (k < 2) 
    stop("There is a single eigenvalue. No plot can be produced.")
  if (k < plot.axes[1]) 
    stop("Axis", plot.axes[1], "does not exist.")
  if (k < plot.axes[2]) 
    stop("Axis", plot.axes[2], "does not exist.")
  if (!is.null(rn)) 
    rownames(x$vectors) <- rn
  labels = colnames(x$vectors[, plot.axes])
  if (!is.null(xlabs)) labels[1] <- xlabs
  if (!is.null(ylabs)) labels[2] <- ylabs
  diag.dir <- diag(c(dir.axis1, dir.axis2))
  x$vectors[, plot.axes] <- x$vectors[, plot.axes] %*% diag.dir
  if (is.null(Y)) {
    limits <- apply(x$vectors[, plot.axes], 2, range)
    ran.x <- limits[2, 1] - limits[1, 1]
    ran.y <- limits[2, 2] - limits[1, 2]
    xlim <- c((limits[1, 1] - ran.x/10), (limits[2, 1] + 
                                            ran.x/5))
    ylim <- c((limits[1, 2] - ran.y/10), (limits[2, 2] + 
                                            ran.y/10))
    par(mai = c(1, 1, 1, 0.5))
    plot(x$vectors[, plot.axes], xlab = labels[1], ylab = labels[2], 
         xlim = xlim, ylim = ylim, asp = 1)
    text(x$vectors[, plot.axes], labels = rownames(x$vectors), 
         pos = 4, cex = 1, offset = 0.5)
    if (is.null(main)){
      title(main = "PCoA ordination", line = 2.5)
    } else title(main = main, line = 2.5)
  }
  else {
    n <- nrow(Y)
    points.stand <- scale(x$vectors[, plot.axes])
    S <- cov(Y, points.stand)
    U <- S %*% diag((x$values$Eigenvalues[plot.axes]/(n - 
                                                        1))^(-0.5))
    colnames(U) <- colnames(x$vectors[, plot.axes])
    par(mai = c(1, 0.5, 1.4, 0))
    biplot(x$vectors[, plot.axes], U, xlab = labels[1], ylab = labels[2])
    if (is.null(main)) {
    title(main = c("PCoA biplot", "Response variables projected", 
                   "as in PCA with scaling 1"), line = 4)
    } else title(main = main, line = 4)
  }
  invisible()
}

biplot(pcoa.ntK, xlabs = 'My x label', ylabs = 'My y label', main = 'My title')

【讨论】:

    【解决方案2】:

    您可以查看biplot.pcoa 的源代码,您会发现修改起来并不难。该软件包的作者决定根据输入以及绘图的主标题对轴标签进行硬编码。这是一个修改后的版本,在使用预定义的值之前,将首先检查是否使用了 xlabylabmain 的值:

    biplot.pcoa <- function (x, Y = NULL, plot.axes = c(1, 2), dir.axis1 = 1, dir.axis2 = 1, 
              rn = NULL, ...) 
    {
      k <- ncol(x$vectors)
      if (k < 2) 
        stop("There is a single eigenvalue. No plot can be produced.")
      if (k < plot.axes[1]) 
        stop("Axis", plot.axes[1], "does not exist.")
      if (k < plot.axes[2]) 
        stop("Axis", plot.axes[2], "does not exist.")
      if (!is.null(rn)) 
        rownames(x$vectors) <- rn
      args <- list(...)
      labels = ifelse(c("xlab", "ylab") %in% names(args), c(args$xlab, args$ylab), colnames(x$vectors[, plot.axes]))
      diag.dir <- diag(c(dir.axis1, dir.axis2))
      x$vectors[, plot.axes] <- x$vectors[, plot.axes] %*% diag.dir
      if (is.null(Y)) {
        limits <- apply(x$vectors[, plot.axes], 2, range)
        ran.x <- limits[2, 1] - limits[1, 1]
        ran.y <- limits[2, 2] - limits[1, 2]
        xlim <- c((limits[1, 1] - ran.x/10), (limits[2, 1] + 
                                                ran.x/5))
        ylim <- c((limits[1, 2] - ran.y/10), (limits[2, 2] + 
                                                ran.y/10))
        par(mai = c(1, 1, 1, 0.5))
        title <- ifelse("main" %in% names(args), args$main, "PCoA ordination")
        plot(x$vectors[, plot.axes], xlab = labels[1], ylab = labels[2], 
             xlim = xlim, ylim = ylim, asp = 1, 
             main = title)
        text(x$vectors[, plot.axes], labels = rownames(x$vectors), 
             pos = 4, cex = 1, offset = 0.5)
        #title(main = "PCoA ordination", line = 2.5)
      }
      else {
        n <- nrow(Y)
        points.stand <- scale(x$vectors[, plot.axes])
        S <- cov(Y, points.stand)
        U <- S %*% diag((x$values$Eigenvalues[plot.axes]/(n - 
                                                            1))^(-0.5))
        colnames(U) <- colnames(x$vectors[, plot.axes])
        par(mai = c(1, 0.5, 1.4, 0))
        title <- ifelse("main" %in% names(args), args$main, c("PCoA biplot", "Response variables projected", 
                                                                  "as in PCA with scaling 1"))
        biplot(x$vectors[, plot.axes], U, xlab = labels[1], ylab = labels[2], main = title)
        # title(main = c("PCoA biplot", "Response variables projected", 
        #                "as in PCA with scaling 1"), line = 4)
      }
      invisible()
    }
    

    然后:

    biplot(pcoa.ntK, Y=NULL, plot.axes=c(1,2), rn=ntnames,
       xlab="PC1 (%)", main = "Main Title")
    

    请记住,这不会改变原始功能,因此您每次加载包时都需要加载这个修改后的版本,并且需要像这样设置标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2011-07-18
      • 1970-01-01
      • 2020-10-27
      • 2019-02-01
      相关资源
      最近更新 更多