Plotting Factor Analysis Results

小灰灰 2022-08-07 17:48 225阅读 0赞

(This article was first published on Minding the Brain, and kindly contributed to R-bloggers)

A recent factor analysis project (as discussed previously here, here, and here) gave me an opportunity to experiment with some different ways of visualizing highly multidimensional data sets. Factor analysis results are often presented in tables of factor loadings, which are good when you want the numerical details, but bad when you want to convey larger-scale patterns – loadings of 0.91 and 0.19 look similar in a table but very different in a graph. The detailed code is posted on RPubs because embedding the code, output, and figures in a webpage is much, much easier using RStudio’s markdown functions. That version shows how to get these example data and how to format them correctly for these plots. Here I will just post the key plot commands and figures those commands produce.

First, a bar graph showing each measure’s factor loadings with each factor in a separate facet (subplot):

  1. #For each test, plot the loading as length and fill color of a bar
  2. # note that the length will be the absolute value of the loading but the
  3. # fill color will be the signed value, more on this below
  4. ggplot(loadings.m, aes(Test, abs(Loading), fill=Loading)) +
  5. facet_wrap(~ Factor, nrow=1) + #place the factors in separate facets
  6. geom_bar(stat="identity") + #make the bars
  7. coord_flip() + #flip the axes so the test names can be horizontal
  8. #define the fill color gradient: blue=positive, red=negative
  9. scale_fill_gradient2(name = "Loading",
  10. high = "blue", mid = "white", low = "red",
  11. midpoint=0, guide=F) +
  12. ylab("Loading Strength") + #improve y-axis label
  13. theme_bw(base_size=10) #use a black-and-white theme with set font size

Created by Pretty R at inside-R.org










Fig. 1 from Mirman et al., 2015, Nature Communications

Second, the full pairwise correlation matrix with a stacked bar graph showing each measure’s (absolute) loading on each factor:

  1. library(grid) #for adjusting plot margins
  2. #place the tests on the x- and y-axes,
  3. #fill the elements with the strength of the correlation
  4. p1 <- ggplot(corrs.m, aes(Test2, Test, fill=abs(Correlation))) +
  5. geom_tile() + #rectangles for each correlation
  6. #add actual correlation value in the rectangle
  7. geom_text(aes(label = round(Correlation, 2)), size=2.5) +
  8. theme_bw(base_size=10) + #black and white theme with set font size
  9. #rotate x-axis labels so they don't overlap,
  10. #get rid of unnecessary axis titles
  11. #adjust plot margins
  12. theme(axis.text.x = element_text(angle = 90),
  13. axis.title.x=element_blank(),
  14. axis.title.y=element_blank(),
  15. plot.margin = unit(c(3, 1, 0, 0), "mm")) +
  16. #set correlation fill gradient
  17. scale_fill_gradient(low="white", high="red") +
  18. guides(fill=F) #omit unnecessary gradient legend
  19. p2 <- ggplot(loadings.m, aes(Test, abs(Loading), fill=Factor)) +
  20. geom_bar(stat="identity") + coord_flip() +
  21. ylab("Loading Strength") + theme_bw(base_size=10) +
  22. #remove labels and tweak margins for combining with the correlation matrix plot
  23. theme(axis.text.y = element_blank(),
  24. axis.title.y = element_blank(),
  25. plot.margin = unit(c(3,1,39,-3), "mm"))
  26. library(gridExtra) #for combining the two plots
  27. grid.arrange(p1, p2, ncol=2, widths=c(2, 1)) #side-by-side, matrix gets more space

Created by Pretty R at inside-R.org










Fig. 2 from Mirman et al., in press, Neuropsychologia

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读