Correspondence Analysis in Archaeology
  • Home
  • Guide by worked examples
    • Aim of Correspondence Analysis
    • Association between rows and columns
    • Number of dimensions useful for data interpretation
    • Interpreting the CA scatterplot: dimensions interpretation
    • Interpreting the CA scatterplot (continued): correlation between row profiles and dimensions
    • Quality of the representation
    • Assembling the whole picture
    • Extension: clustering rows and/or columns
    • Another worked example
  • References
  • CA in R
    • CAinterprTools (R package)
    • R function for various CA scatterplots
    • R function for improved CA scatterplot
    • R function for perceptual-map-like CA scatterplot
    • R function for plotting Pareto chart of categories contribution
    • R Script for CA
    • Additional R Script for CA
    • R Script for the Significance of CA's Dimensions
  • Other Tools for Statistics
    • R package for seriation via CA
    • R function for scalar-stress probability calculation
    • R function for post. prob. for different relations btw 2 Bayesian 14C phases
    • R function for Posterior Probability Density plot
    • R function for binary Logistic Regression
    • R function for binary Logistic Regression internal validation
    • R function for optimism-adjusted AUC
    • R function for Brainerd-Robinson similarity coefficient
    • R function for univariate outliers detection
    • R function for plotting Jenks natural breaks classification
    • R function for permutation-based Chi square test of independence
    • R function for permutation t-test
    • R function for visually displaying Mann-Whitney test
    • R function for visually displaying Kruskal-Wallis test
    • Kruskal-Wallis Excel Template
    • Chi-squared Excel Template
    • Excel Template for Robust Statistics
  • GIS
  • Blog
  • About me
  • Guestbook/Comments
'ca.scatter': R function for plotting different types of Correspondence Analysis scatterplots
'ca.scatter' is an R function which allows to plot different types of Correspondence Analysis scatterplots. It depends on the 'ca' and 'FactoMineR' packages, and aims at making easier to obtain some of the interesting charts provided by those two amazing packages. Users are refereed to the aforementioned packages in case they want greater control of some settings. Note: the function has been integrated in the 'CAinterprTools' package (as of version 0.5) described in this same site (LINK).

As for the rationale and context of use of the returned charts, users are referred to the documentation of 'ca' and 'FactoMineR' packages. Information can also be found both in this same site and in my 2013 article on Archeologia e Calcolatori journal. For perceptual map-like scatterplots, also check my function described in this same site (LINK).
The function is quite straightforward:
ca.scatter(data, x, y, type)

where data is the input dataset, x and y are the dimensions of interest, and type is a number (from 1 to 4) which indicates the desired type of scatterplot (see also the figures below, for which the 'greenacre_data' dataset has been used):
-type=1: regular scatterplot for rows and columns
-type=2: Standard Biplot (2 plots are returned: one with row-categories vectors displayed, one for columns-categories vectors) 
-type=3: scaterplot of row categories with groupings shown by different colors; scatterplot for column categories is also returned
-type=4: 3D scatterplot with cluster tree for row categories; scatterplot for column categories is also returned.
Picture
type=1
Picture
type=3
Picture
type=2
Picture
type=4
To load the function into R, just copy and paste the function below into the R console, and press return (or you can download the .R file HERE):
ca.scatter <- function(data,x,y,type){
  numb.dim.cols<-ncol(data)-1
  numb.dim.rows<-nrow(data)-1
  dimensionality <- min(numb.dim.cols, numb.dim.rows)
  res.ca <- ca(data)
  ca.factom <- CA(data, ncp=dimensionality, graph=FALSE)
  resclust.rows<-HCPC(ca.factom, nb.clust=-1, metric="euclidean", method="ward", order=TRUE, graph.scale="inertia", graph=FALSE, cluster.CA="rows")
  resclust.cols<-HCPC(ca.factom, nb.clust=-1, metric="euclidean", method="ward", order=TRUE, graph.scale="inertia", graph=FALSE, cluster.CA="columns")
  if (type==1) {
    plot.CA(ca.factom, axes=c(x,y), autoLab = "auto", cex=0.75)
  } else {
    if (type==2) {
      plot(res.ca, mass = FALSE, dim=c(x,y), contrib = "none", col=c("black", "red"), map ="rowgreen", arrows = c(FALSE, TRUE)) #for rows
      plot(res.ca, mass = FALSE, dim=c(x,y), contrib = "none", col=c("black", "red"), map ="colgreen", arrows = c(TRUE, FALSE)) #for columns
    } else {
      if (type==3) {
        plot(resclust.rows, axes=c(x,y), choice="map", draw.tree=FALSE, ind.names=TRUE, new.plot=TRUE)
        plot(resclust.cols, axes=c(x,y), choice="map", draw.tree=FALSE, ind.names=TRUE, new.plot=TRUE)
      } else {
        if (type==4) {
          plot(resclust.rows, axes=c(x,y), choice="3D.map", draw.tree=TRUE, ind.names=TRUE, new.plot=TRUE)
          plot(resclust.cols, axes=c(x,y), choice="3D.map", draw.tree=TRUE, ind.names=TRUE, new.plot=TRUE)
        } 
      }
    }
  }
}
Final note: 
in order for the function to work, the 'ca' and 'FactoMineR' packages must be installed in R.
Have you found this website helpful?  Consider to leave a comment in this page.

Powered by Create your own unique website with customizable templates.