[R] Solved: Help plotting some data
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Aug 17 13:20:22 CEST 2001
Hi!
The other day I asked a couple of questions about plotting some data.
Firstly I wanted to specify the colours used for the text plotted using:
> text(chem.predict[,1:2], labels = km$cluster)
Matt Wiener suggested:
> col.vec <- c("black", "red", "blue")
> text(chem.predict[,1:2], labels = km$cluster, color =
col.vec[km$clusters])
Cheers Matt, that worked great.
Secondly I wanted to know how to plot just the loadings of a PCA as
biplot arrows, i.e. plot a biplot but without plotting the labels for
the scores.
I solved this by taking the biplot.default code and adding an option
pscores to the call. This is FALSE by default. I then put an if clause
before the bit of the code that plots the labels for the PCA scores, so
that they only get plotted if you specify pscores=TRUE. My function for
doing this is copied below, with a couple of extra modifications so that
I can get the plot as I required.
Cheers for the help, and for such an excellent piece of software. I'm a
R newbie and am constantly surprised how easy it is get R to do the
things you want to do with it.
Gav
my.biplot <- function (x, y, var.axes = TRUE, col, cex = c(1, 0.4),
xlabs = NULL, ylabs = NULL, expand = 1, xlim = NULL, ylim = NULL,
arrow.len = 0.1,
pscores = FALSE, xlab="", ylab="", ...)
{
labx <- xlab
laby <- ylab
n <- nrow(x)
p <- nrow(y)
if (missing(xlabs)) {
xlabs <- dimnames(x)[[1]]
if (is.null(xlabs))
xlabs <- 1:n
}
xlabs <- as.character(xlabs)
dimnames(x) <- list(xlabs, dimnames(x)[[2]])
if (missing(ylabs)) {
ylabs <- dimnames(y)[[1]]
if (is.null(ylabs))
ylabs <- paste("Var", 1:p)
}
ylabs <- as.character(ylabs)
dimnames(y) <- list(ylabs, dimnames(y)[[2]])
if (length(cex) == 1)
cex <- c(cex, cex)
if (missing(col)) {
col <- par("col")
if (!is.numeric(col))
col <- match(col, palette())
col <- c(col, col + 1)
}
else if (length(col) == 1)
col <- c(col, col)
unsigned.range <- function(x) c(-abs(min(x)), abs(max(x)))
rangx1 <- unsigned.range(x[, 1])
rangx2 <- unsigned.range(x[, 2])
rangy1 <- unsigned.range(y[, 1])
rangy2 <- unsigned.range(y[, 2])
if (missing(xlim) && missing(ylim))
xlim <- ylim <- rangx1 <- rangx2 <- range(rangx1, rangx2)
else if (missing(xlim))
xlim <- rangx1
else ylim <- rangx2
ratio <- max(rangy1/rangx1, rangy2/rangx2)/expand
on.exit(par(oldpar))
oldpar <- par(pty = "s")
plot(x, type = "n", xlim = xlim, ylim = ylim, col = col[1],
xlab=labx, ylab=laby, ...)
if (pscores)
text(x, xlabs, cex = cex[1], col = col[1], ...)
par(new = TRUE)
plot(y, axes = FALSE, type = "n", xlim = xlim * ratio, ylim = ylim *
ratio, xlab = "", ylab = "", col = col[1], ...)
axis(3, col = col[2])
axis(4, col = col[2])
box(col = col[1])
text(y, labels = ylabs, cex = cex[2], col = col[2], ...)
if (var.axes)
arrows(0, 0, y[, 1] * 0.8, y[, 2] * 0.8, col = col[2],
length = arrow.len)
invisible()
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list