Changing axes labels for biplot() in R -
i trying visualize results of pcoa{ape}
making biplot in r. axes default labels axis 1 , axis 2, want edit this.
this code have tried:
biplot(pcoa.ntk, y=null, plot.axes=c(1,2), rn=ntnames, xlabs="pc1 (%)", ylabs="pc2 (%)")
but labels don't change. can tell me i'm doing wrong here? , edit title, tips this?
my data:
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)
biplot
generic function. default method , method use objects come using prcomp
function in stats
package allow specify axis labels , title, reason person wrote method called objects of class pcoa
hasn't allowed specify them. think option write own version of biplot.pcoa
(or ask package maintainer add option).
this quick , dirty hack of function in ape
package might want, no promises won't have broken else!
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 single eigenvalue. no plot can 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 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')
Comments
Post a Comment