autoplot - How to adjust loading labels?
ggfortify, ggplot2, r
Solution
You can get the coordinates by `layer_data(g, 2)`. But `autoplot(prcomp.obj)` passes other arguments to `ggbiplot()`, so you can change `label` and `loadings.label` position using arguments of `ggbiplot()`, such as `loadings.label.hjust` (see `?ggbiplot`).
example code:
arrow_ends <- layer_data(g, 2)[,c(2,4)]
autoplot(prcomp(df[,-11], scale=TRUE), data=df,
loadings.label=TRUE, loadings=TRUE,
loadings.label.size=8, loadings.colour='blue',
label.size=5, loadings.label.vjust = 1.2) + # change loadings.label position
geom_point(data = arrow_ends, aes(xend, yend), size = 3) + # the coordinates from layer_data(...)
geom_text(vjust=-1, label=rownames(df)) +
theme(plot.background=element_blank(),
panel.background=element_rect(fill='transparent',color='black',size=1),
legend.text=element_text(hjust=1),
legend.key=element_blank())
Problem
I would like to be able to adjust the positions of the loading labels, so that they do not fall atop the the arrows. However, I do not know where the adjustments need to be made. The `geom_text` can be used to adjust the position of the site positions, but I cannot find where the vectors are stored in `str(g)`. ``` library(ggplot2) library(ggfortify) df <- data.frame(replicate(10,sample(-10:10,10,rep=TRUE))) names(df) <- c('up','down','left','right','circle','square','triangle','x','r1','l1') rownames(df) <- paste('Dummy Site', seq(0,9,1)) g <- autoplot(prcomp(df[,-11], scale=TRUE), data=df, loadings.label=TRUE, loadings=TRUE, loadings.label.size=8, loadings.colour='blue', label.size=5) + geom_text(vjust=-1, label=rownames(df)) + theme(plot.background=element_blank(), panel.background=element_rect(fill='transparent',color='black',size=1), legend.text=element_text(hjust=1), legend.key=element_blank()) g ``` I've looked in `ggplot2::theme` and I've examined the help docs for `autoplot`, but can't find any mention of the adjusting label position. Bonus points if it can adjust based on the vector of the arrow, but a static adjustment would be acceptable. Currently, here is what the plot looks like: