[R] Customizing a Plot in Correspondence Analysis

Milan Bouchet-Valat nalimilan at club.fr
Fri Jun 15 14:40:33 CEST 2012


Le jeudi 14 juin 2012 à 20:32 -0400, Salvador Ramirez a écrit :
> Hello,
> 
> Using the Correspondence Analysis package (ca), I wonder if there is a way
> to further customize the plot beyond the options given in plot.ca provided
> by the mentioned package.
> 
> The correspondence analysis I am doing consists of two datasets sharing
> only the rows, so the plot draw the total of columns (from both datasets)
> in the graph and the rows as expected. However, as in my case the number
> of columns in one dataset is huge, I would like to omit those points and
> labels in the plot. The list of parameters of plot.ca does not allow me to
> do this so I wonder if there is a way to force this in R.
> 
> An example to see what I want is the following:
> 
> library(ca)
> data(author)
> plot(ca(author), arrows=c(FALSE,TRUE))
> 
> The object "author" is composed of two datasets, I would like to omit the
> points corresponding to columns from a-m, while still maintaining the
> points in the second datasets, i.e. columns from n-z.
> 
> Any help is more than appreciated.
This is not possible with the standard plot.ca() function. I have
written small functions in my package[1] that return a ca object with
only the rows or columns you specified. This is an ugly hack, but it
works as long as you plot the result directly. These functions are:

# Restrain CA to a subset of rows (only for plotting!)
rowSubsetCa <- function(ca, indices) {
    ret <- ca
    for (i in 4:7) {
        ret[[i]] <- list()
        ret[[i]] <- ca[[i]][indices]
    }
    del <- which(!(1:nrow(ca$rowcoord) %in% indices))
    ret$rowsup <- as.numeric(sapply(ca$rowsup[ca$rowsup %in% indices],
                                    function(x) x - sum(del < x)))
    ret$rowcoord <- matrix()
    ret$rowcoord <- ca$rowcoord[indices,,drop=FALSE]
    ret$rownames <- ca$rownames[indices]
    ret
}

# Restrain CA to a subset of columns (only for plotting!)
colSubsetCa <- function(ca, indices) {
    ret <- ca
    for (i in 9:12) {
        ret[[i]] <- list()
        ret[[i]] <- ca[[i]][indices]
    }
    ret$colsup <- ret$colsup[ret$colsup %in% indices]
    ret$colsup <- as.numeric(lapply(ca$colsup, function(x) x - sum(indices < x)))
    ret$colcoord <- matrix()
    ret$colcoord <- ca$colcoord[indices,,drop=FALSE]
    ret$colnames <- ca$colnames[indices]
    ret
}

In you case, you can do something like
plot(colSubsetCa(ca(author), 1:13))


Hope this helps


1:
https://r-forge.r-project.org/scm/viewvc.php/pkg/rcmdr-tms/R/showCorpusCa.R?view=markup&root=rcmdr-tms



More information about the R-help mailing list