A few years back, Chitrasen managed to use ggvis
for PCA - with the significantly higher interactivity available for ggvis
I really want to use it going forward. However, I'm unable to locate any information outside of Chitrasen's code for making this a reality.
While the model in the link above provides accurately plotted points and scale, vector loading arrows and variable names are missing. I think this must be the result of there not being any equivalent to geom_segment
for ggvis
yet.
I've tried splicing in the relevant bits of code from ggbiplot
to skirt the issue:
nobs.factor <- sqrt(pcobj$n.obs)
d <- pcobj$sdev
u <- sweep(pcobj$scores, 2, 1/(d * nobs.factor), FUN = "*")
v <- pcobj$loadings
df.u <- as.data.frame(sweep(u[, 1:2], 2, d[1:2]^1,
FUN = "*"))
v <- sweep(v, 2, d^1, FUN = "*")
df.v <- as.data.frame(v[, 1:2])
names(df.u) <- c("xvar", "yvar")
names(df.v) <- names(df.u)
df.u <- df.u * nobs.factor
r <- sqrt(qchisq(0.69, df = 2)) * prod(colMeans(df.u^2))^(1/4)
v.scale <- rowSums(v^2)
df.v <- r * df.v/sqrt(max(v.scale))
df.v$angle <- with(df.v, (180/pi) * atan(yvar/xvar))
df.v$hjust = with(df.v, (1 - 1.5 * sign(xvar))/2)
theta <- c(seq(-pi, pi, length = 50), seq(pi, -pi,
length = 50))
circle <- data.frame(xvar = r * cos(theta), yvar = r *
sin(theta))
The above code creates the information needed for the vector loadings, which is then passed to the code below:
g <- g + geom_segment(data = df.v, aes(x = 0, y = 0, xend = xvar, yend = yvar),
arrow = arrow(length = unit(1/2,"picas")),
color = color, linetype = linetype, alpha = alpha_arrow)
However, I keep coming back to the same brick wall of not being able to create individual segments. Intuitively, it should be possible since ggbiplot
is taking information from a princomp
object, which is exactly what Chitrasen's model is using as well. I know you can make some lines in ggvis
using layer_lines
and layer_paths
, but I've as yet been unable to get these to radiate from the x and y zero point.
I'm wondering if anyone out there has any advice on how to proceed/if it's even possible with current ggvis
capabilities? The potential of tooltips and slider bars with full-blown PCA has me salivating....