kassambara / factoextra

Extract and Visualize the Results of Multivariate Data Analyses
http://www.sthda.com/english/rpkgs/factoextra
354 stars 102 forks source link

Custom Variable Labels #71

Open Rae309 opened 6 years ago

Rae309 commented 6 years ago

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?

duttashi commented 6 years ago

@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())
AldridgeCaleb commented 1 year ago

@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()  
)
ArkosicGreywacke commented 11 months ago

@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?

GavinDuley commented 8 months ago

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.