[R] Attempt to customise the "plotpc()" function

Nikos Alexandris nikos.alexandris at felis.uni-freiburg.de
Sat May 15 19:09:30 CEST 2010


Dear R-list,

Among the (R-)tools, I've seen on the net, for (bivariate) Principal Component 
scatter plots (+histograms), "plotpc" [1] is the one I like most.

By default it performs PCA on a bivariate dataset based on R's "princomp()" 
(which is the eigenvector-based algebraic solution to PCA). I would like to 
modify "plotpc()" in order be able, as an end-user, to define whether the PCA 
should be performed by using:
  (a) "princomp()" (the default currently) 
  (b) the "prcomp()" function (which is the SVD-based algebraic solution to 
PCA)

Of course I want to have control over the parameters that "prcomp()" accepts, 
namely:
  - data centering (argument "center")
  - data scaling (argument "scale")

Hopefully this is the right place to post this question(s). I started the 
modification by attempting first to get a "prcomp" version of "plotpc()" 
(named it "plotpc.svd()") by altering the following:

  - accept the arguments "pccenter" and "pcscale" in line 1:
--%<---
"function ( x , pccenter = TRUE, scale = FALSE, xrange =... )"
-->%---

  - accept both the "center" and the "scale" parameters for the "scale()" 
function in line 134:
--%<---
x <- scale (x , center = pccenter , scale = pcscale )
-->%---

  - change "princomp" to "prcomp" in line 140 as well as all required 
arguments (that I know off):
--%<---
pc <- prcomp ( x[, 1:2] , center = FALSE , scale = FALSE , retx = TRUE , 
na.action = na.fail )
-->%---

    -- in line 144:
--%<---
angle <- atan2(pc$rotation[2, 1], pc$rotation[1, 1])
-->%---

    -- in lines 147 and 148, I replaced "pc$scores" with "pc$x"

Running the modified function ends up with an error:

--%<---
# "x" is a data.frame with two columns and works fine with plotpc()
plotpc.svd (x , pccenter = TRUE , pcscale = FALSE )

Error in inherits(unit, "unit") : could not find function "un"

# getting more info
traceback()

5: inherits(unit, "unit")
4: is.unit(y)
3: textGrob(label = label, x = x, y = y, just = just, hjust = hjust, 
       vjust = vjust, rot = rot, check.overlap = check.overlap, 
       default.units = default.units, name = name, gp = gp, vp = vp)
2: grid.text(main, y = un(ymean + xlim[2] + xrange/10), gp = gpar(cex = 1, 
       font = 2))
1: plotpc.svd(x, pccenter = TRUE, pcscale = FALSE)
-->%---


I am bit lost now about where I should continue looking for required 
modifications in the code. Any hints?

Thanks in advance, Nikos



More information about the R-help mailing list