Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I include subscripts with fviz_pca_var labels?

I have a PCA that I’m visualizing with fviz_pca_var and I want to include subscripts for some of my variables. How do I get these to display properly?

Also, is there any easier way to flip the y-axis so the values are inverted? (I still want -1 to be on the left).

df <- structure(list(`P[1]` = c(-0.9, -1, -1.1, -1.2, -1.28, -1.35, -1.41, -1.45, -1.47, -1.47, -1.45, -1.41, -1.35, -1.27, -1.17, -1.06, -0.93, -0.79, -0.64, -0.49, -0.33, -0.17, -0.01, 0.14, 
                                0.29, 0.44, 0.57, 0.68, 0.78, 0.86, 0.92, 0.97, 1, 1.02, 1.03, 1.03, 1.02, 1.02, 1.01, 1, 1, 0.99, 0.98, 0.96, 0.94, 0.92, 0.89, 0.86, 0.82, 0.79, 0.75), 
                     `Z[2]` = c(0.56, 0.5, 0.44, 0.38, 0.32, 
                                0.27, 0.22, 0.19, 0.18, 0.18, 0.22, 0.27, 0.36, 0.47, 0.6, 0.74, 0.88, 1.02, 1.15, 1.25, 1.32, 1.35, 1.33, 1.25, 1.11, 0.91, 0.67, 0.38, 0.06, -0.27, -0.61, -0.93, -1.22, -1.46, -1.66, -1.79, 
                                -1.86, -1.87, -1.82, -1.72, -1.56, -1.37, -1.14, -0.89, -0.62, -0.34, -0.06, 0.23, 0.51, 0.8, 1.08), 
                     `C3[1]` = c(-0.16, -0.14, -0.12, -0.1, -0.25, -0.53, -0.29, -0.83, -0.69, -0.06, -0.98, 
                                 -0.53, -0.8, 1.17, 0.64, 0.09, 0.61, 1.97, 0.84, 1.85, -0.98, -0.67, -0.82, -1, -0.47, -1.01, -0.79, -0.97, -1.33, -1.48, -1.59, -1.71, -1.8, -1.88, -1.94, -2.09, -1.96, -1.96, -1.95, -1.97, 
                                 -1.88, -1.82, -1.63, -1.52, -1.54, -1.4, -1.42, -1.29, -1.19, -0.83, -0.69), 
                     `C3[2]` = c(0.77, 0.79, 0.81, 0.82, 0.79, 0.71, 0.78, 0.63, 0.67, 0.86, 0.59, -0.34, 0.65, 1.23, 1.53, 1.7, 0.99, 
                                 2.07, 1.71, 3.01, -0.53, -0.53, 0.54, 0.43, 0.52, -0.59, 0.25, 0.1, -0.5, -0.57, -0.54, -0.57, -0.55, -0.55, -0.56, -0.83, -0.46, -0.46, -0.46, -0.61, -0.49, -0.51, -0.2, -0.13, -0.39, -0.26, 
                                 -0.54, -0.44, -0.44, 0.22, 0.34)), 
                row.names = c(NA, -51L), class = c("tbl_df", "tbl", "data.frame"))
pca <- princomp(df, scores = TRUE)
fviz_pca_var(pca, title = "", repel = TRUE, ggtheme = theme_bw()) +
  scale_x_reverse() +
  scale_y_continuous(limits = c(-0.2, 0.6))

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just need to add the parse = TRUE argument to the fviz_pca_var function.

fviz_pca_var(pca,
             title = "", 
             repel = TRUE, 
             #This is the only change I made 
             parse = T,
             ggtheme = theme_bw()) 
# Rest of your code

plot

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading