Developmental / Cultural / Experimental Psychologist

Code

Here’s some code I threw together to make pretty visualisations for factor analyses.  

Here you can see how I’ve visualised the factor loadings of 26 variables into four (predicted) factors. This is much nicer than exclusively presenting a big, ugly, table of numbers, and much better than presenting a cleaner table with some values omitted. You will still present the ugly table (but mostly to link the index values to the variables) but now it’s easier to understand the relationship between the factors.

The code is below the figure.

EXAMPLE PLOTS.001.jpeg
library(ggplot2)
library(psych)

### SO YOU'VE ALREADY RUN THE FACTOR ANALYSIS, YEAH? IT'LL LOOK SOMETHING LIKE THE BELOW

factor_df <- fa(df,nfactors=4,n.obs = NA,n.iter=1, rotate="oblimin", 
                scores="regression", residuals=FALSE, SMC=TRUE, covar=FALSE,
                missing=FALSE,impute="median", min.err = 0.001,  max.iter = 50,symmetric=TRUE, 
                warnings=TRUE, fm="minres", alpha=.1,p=.05,oblique.scores=FALSE,
                np.obs=NULL, use="pairwise",correct=.5,weight=NULL)

### COOL, NOW USE THIS CODE TO EXTRACT YOUR LOADING VALUES AND YOUR OWN UNIQUE VARIABLE NAMES

loadings <- factor_df$loadings

write.file.csv(loadings, row.names=TRUE, file = "DUMMY DATA.csv")

factor_names <- factor_df$rownames

####################################################################################################################
####################################################################################################################

#### NOW HERE'S THE NEW STUFF. IMPORT YOUR LOADINGS KEEPING ALL YOUR UNIQUE VALUES AND IDS

loading_df <- read.csv("DUMMY DATA.csv", 
                         header = TRUE, 
                         stringsAsFactors = FALSE)

### IF YOUR VARIABLES ARE ORDERED IN SOME MANNER, ASSIGN THEM AN INDEX VALUE (ELSE IT WILL DEFAULT TO PRESENTING YOUR VARIABLES IN ALPHABETICAL ORDER)

loading_df$index <- c(1:26) ### MAKE SURE THE SECOND NUMBER = NUMBER OF VARIABLES

#### USE THIS CODE TO IDENTIFY PREDICTED FACTORS. PERHAPS BECAUSE YOU PRE-REG'D THIS ANALYSIS, OR BECAUSE YOU HAVE KNOWN SCALES IN YOUR DATA
loading_df[c(1:8), "pred_factor" ] = 1
loading_df[c(9:14), "pred_factor" ] = 2
loading_df[c(15:17), "pred_factor" ] = 3
loading_df[c(18:23), "pred_factor" ] = 4
loading_df[c(24:26), "pred_factor" ] = 5

min_thresh <- .5 #SET YOUR THRESHOLD FOR WHAT COUNTS AS 'LOADING ON A FACTOR'
negmin_thresh <- min_thresh * -1 ### AND DON'T TOUCH THIS

### NOW, CREATE AS MANY PLOTS AS YOU HAVE FACTORS. YOU'LL RE-RUN THIS CODE A FEW TIMES.
### JUST MAKE SURE TO UPDATE THE MR1/MR2/MRx VALUE IN THE FIRST AND SECOND LINE
### AND UPDATE THE TITLE IN THE LAST LINE

PLOT_1 <- ggplot(loading_df, aes(x= as.factor(index), y = as.numeric(MR1), fill = as.factor(pred_factor))) +
  geom_col(aes(alpha = ifelse(MR1 >= min_thresh, 1, 0))) +
  scale_alpha(range=c(.5,1)) +
  geom_hline(aes(yintercept = min_thresh), linetype = "dashed", colour = "gray60") +
  geom_hline(aes(yintercept = negmin_thresh), linetype = "dashed", colour = "gray60") +
  geom_hline(aes(yintercept = 1), linetype = "dashed", colour = "gray60") +
  geom_hline(aes(yintercept = 0), linetype = "dashed") +
  coord_polar() +
  ylim(-1, 1) +
  theme( 
    axis.ticks.x = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    panel.background = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none",
    plot.title = element_text(face = "bold", hjust = .5),
    plot.caption = element_text(hjust = .5)
  ) +
  ggtitle("FACTOR A")

### IF YOU HAVE MORE THAN 4 FACTORS, YOU'LL HAVE TO READ UP ON HOW TO USE GRID.ARRANGE.

master_plot <- grid.arrange(PLOT_A, PLOT_B, PLOT_C, PLOT_D, ncol=2)
master_plot

### NOW PRESENT YOUR BEAuTIFUL FIGURES ALONGSIDE YOUR FULL (BUT UGLY) TABLE OF LOADING VALUES
### MAKE SURE TO INCLUDE YOUR INDEX VALUES SO YOUR READERS CAN IDENTIFY WHAT THEY'RE LOOKING AT
### DON'T FORGET TO INCLUDE YOUR INTER-FACTOR CORRELATIONS IN THE TEXT

### THIS ISN'T COPYRIGHT OR ANYTHING, MOST OF THIS CODE IS COBBLED FROM OTHERS WORK ANYHOW.
### BUT BE COOL AND LET ME KNOW IF YOU USE THESE FIGURES IN YOUR PAPER :)

### EMAIL ME: ROHANKAPITANY@GMAIL.COM
### FOLLOW ME: @PSYCASM
### CITE ME: https://scholar.google.co.jp/citations?user=TkSZGtEAAAAJ&hl=en&oi=ao
### VISIT ME: rohankapitany.science