factoextra
factoextra copied to clipboard
Custom Variable Labels
I'm using fviz_pca_biplot, but I would like to use custom labels for the variable names that go over the arrows so that I can include subscript, superscript etc. Is there a way to do this?
@Rae309 from the function signature, help("fviz_pca_biplot")
, I do not think adding custom labels is possible. The most you can do is to include geom = c("text")
that will add labels over the arrows. A minimum reprex is given below; @kassambara maybe this can be a future enhancement?
data(iris)
res.pca <- prcomp(iris[, -5], scale = TRUE)
fviz_pca_biplot(res.pca, label = "var",
habillage=iris$Species,
addEllipses=TRUE, ellipse.level=0.95,
geom = c("text"),
ggtheme = theme_minimal())
@Rae309 if you are using the princomp
or prcomp
functions from the stats
package, then you can change variables names by updating the row names in $loadings
(princomp
) or $rotation
(prcomp
). Extending the reprex provided by @duttashi, the variable names are changed from "Sepal.Length", "Sepal.Width", "Petal.Length", and "Petal.Width" to "Sepal Length (mm)", "SEP W", "PL", and "petal_width".
data(iris)
res.pca <- prcomp(iris[, -5], scale = TRUE)
row.names(res.pca$rotation) <- c("Sepal Length (mm)", "SEP W", "PL", "petal_width")
fviz_pca_biplot(res.pca, label = "var",
habillage = iris$Species,
addEllipses = TRUE, ellipse.level = 0.95,
geom = c("text"),
ggtheme = theme_minimal()
)
@Rae309 if you are using the
princomp
orprcomp
functions from thestats
package, then you can change variables names by updating the row names in$loadings
(princomp
) or$rotation
(prcomp
). Extending the reprex provided by @duttashi, the variable names are changed from "Sepal.Length", "Sepal.Width", "Petal.Length", and "Petal.Width" to "Sepal Length (mm)", "SEP W", "PL", and "petal_width".data(iris) res.pca <- prcomp(iris[, -5], scale = TRUE) row.names(res.pca$rotation) <- c("Sepal Length (mm)", "SEP W", "PL", "petal_width") fviz_pca_biplot(res.pca, label = "var", habillage = iris$Species, addEllipses = TRUE, ellipse.level = 0.95, geom = c("text"), ggtheme = theme_minimal() )
Any suggestions for special characters like greek letters, superscripts and subscripts?
Is there any update on this? It would be particularly useful where variable names include characters that R objects to when included in column names.