factoextra icon indicating copy to clipboard operation
factoextra copied to clipboard

Custom Variable Labels

Open Rae309 opened this issue 6 years ago • 4 comments

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 avatar Jun 08 '18 21:06 Rae309

@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())

duttashi avatar Jul 31 '18 17:07 duttashi

@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()  
)

AldridgeCaleb avatar Jul 12 '23 14:07 AldridgeCaleb

@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()  
)

Any suggestions for special characters like greek letters, superscripts and subscripts?

ArkosicGreywacke avatar Nov 24 '23 15:11 ArkosicGreywacke

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.

GavinDuley avatar Jan 25 '24 14:01 GavinDuley