R Biplot Psych
Working on a recent project to characterize the underlying factors of urban typology, we needed a quick means of visualizing the factor loadings of each indicator. We had used the “pysch” library’s “fa” function to get the analysis done. However, a quick visualization approach was not forthcoming. One of my research assistants found this fantastic example using ‘ggplot2’. However, getting the ggplot function call to work was not quite as straightforward as in the example shown.
In case you are in a similar situation, where you have obtained the loading matrix directly from the ‘fa’ function and want to use the beautiful rendering of the ggplot facet-bar approach, below is code that you might find useful.
Use the R package psych. The function pairs.panels in psych package can be also used to create a scatter plot of matrices, with bivariate scatter plots below the diagonal, histograms on the diagonal, and the Pearson correlation above the diagonal. This is one of a set of How To'to do various things using R (R Core Team,2019), particularly using the psych (Revelle,2020) package. The current list of How To’s includes: 1.Installing R and some useful packages 2.Using R and the psych package to nd omega h and w t. 3.Using R and the psych forfactor analysisand principal components analysis.
The additional steps involve converting the loading matrix to a dataframe and then converting the row names to a dataframe column. Ignore the descriptive factor names chosen for this example, and the input file can be dataframe or matrix.
How To Interpret Pca Biplot
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 | library(data.table) library(reshape2) options(width=240) df=read.csv('precities.csv') #scree(df) #fa.parallel(df2) #loadings # print(fa(df,9,rotate='oblimin',fm='minres')$loadings,cut=.2) load=load$loadings load=data.frame(load) colnames(load)[1]<-'Indicators' colnames(load)[2:10]<-c('Urbanization','Density, metro','Cost, innovation','Public/active transit','Congestion','BRT','Bikeshare','Urban poverty','Urban sprawl') load.m<-melt(load,id='Indicators',variable.name='Factors',value.name='Loading',measure=colnames(load)[2:10]) #load.m <- melt(load, variable.name='Factors', value.name='Loading', measure = c('Urbanization','Density, metro','Cost, innovation','Public/active transit','Congestion','BRT','Bikeshare','Urban poverty','Urban sprawl')) loadPlot<-ggplot(load.m,aes(Indicators,abs(Loading),fill=Loading))+facet_wrap(~Factors,nrow=1)+geom_bar(stat='identity')+coord_flip()+ scale_fill_gradient2(name='Loading',high='blue',mid='white',low='red',midpoint=0,guide=F)+ylab('Loading')+theme_bw(base_size=10) |