Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe this will help-- it's adapted from code I wrote some time back. It now draws arrows as well.</p> <pre><code>PCbiplot &lt;- function(PC, x="PC1", y="PC2") { # PC being a prcomp object data &lt;- data.frame(obsnames=row.names(PC$x), PC$x) plot &lt;- ggplot(data, aes_string(x=x, y=y)) + geom_text(alpha=.4, size=3, aes(label=obsnames)) plot &lt;- plot + geom_hline(aes(0), size=.2) + geom_vline(aes(0), size=.2) datapc &lt;- data.frame(varnames=rownames(PC$rotation), PC$rotation) mult &lt;- min( (max(data[,y]) - min(data[,y])/(max(datapc[,y])-min(datapc[,y]))), (max(data[,x]) - min(data[,x])/(max(datapc[,x])-min(datapc[,x]))) ) datapc &lt;- transform(datapc, v1 = .7 * mult * (get(x)), v2 = .7 * mult * (get(y)) ) plot &lt;- plot + coord_equal() + geom_text(data=datapc, aes(x=v1, y=v2, label=varnames), size = 5, vjust=1, color="red") plot &lt;- plot + geom_segment(data=datapc, aes(x=0, y=0, xend=v1, yend=v2), arrow=arrow(length=unit(0.2,"cm")), alpha=0.75, color="red") plot } fit &lt;- prcomp(USArrests, scale=T) PCbiplot(fit) </code></pre> <p>You may want to change size of text, as well as transparency and colors, to taste; it would be easy to make them parameters of the function. Note: it occurred to me that this works with prcomp but your example is with princomp. You may, again, need to adapt the code accordingly. Note2: code for <code>geom_segment()</code> is borrowed from the mailing list post linked from comment to OP.</p> <p><img src="https://i.stack.imgur.com/nATw1.png" alt="PC biplot"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload