1

I have recently used the following script in order to perform a MCA analysis and visualize the plot (I found it on http://gastonsanchez.com/blog/how-to/2012/10/13/MCA-in-R.html).

The data are from the Data frame "Tea" contained in the R package "FactoMineR".

# load data tea
data(tea)

# select these columns
newtea = tea[, c("Tea", "How", "how", "sugar", "where", "always")]

# number of categories per variable
cats = apply(newtea, 2, function(x) nlevels(as.factor(x)))

# apply MCA
mca1 = MCA(newtea, graph = FALSE)

# data frame with variable coordinates
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cats), cats))

# data frame with observation coordinates
mca1_obs_df = data.frame(mca1$ind$coord)

# plot of variable categories
ggplot(data=mca1_vars_df, 
       aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) +
 geom_hline(yintercept = 0, colour = "gray70") +
 geom_vline(xintercept = 0, colour = "gray70") +
 geom_text(aes(colour=Variable)) +
 ggtitle("MCA plot of variables using R package FactoMineR")

It runs perfectly, but I would like to know how to introduce qualitative supplementary variables in the analysis. Since I'm not familiar with ggplot2 at all, I'm a bit lost here.

For instance, if I wanted "Tea" to be the supplementary variable, how should I modify the script?

#apply MCA
mca1 = MCA(newtea, graph = FALSE,quali.sup=1)

But how can I preserve this information in the ggplot script?

Milton
  • 13
  • 3
  • 1
    Programming questions are off-topic here & better suited to Stack Overflow. Yours can be migrated, but I'd recommend first editing the question to explain exactly what you want to plot - which part of the mca1 object -, & how you want it to appear. – Scortchi - Reinstate Monica Jun 02 '15 at 09:41
  • Thank for your comment! Actually, I would simply like to know how (1) you can distinguish between supplementary variables and active variables in the script above and (2) how you can preserve this distinction in the ggplot2 script. In case, feel free to move this topic to Stack Overflow! –  Jun 02 '15 at 17:02

1 Answers1

1

You would need to fetch the coordinates of the supplementary variables in the MCA object. They are in mca1$quali.sup$coord just as the coordinates of the active variables are in mca1$var$coord.

mca1 = MCA(newtea, graph = FALSE,quali.sup=1)

mca1_vars_df = data.frame(rbind(mca1$var$coord,
                                mca1$quali.sup$coord), 
                          Variable = rep(names(cats), cats))
scoa
  • 19,359
  • 5
  • 65
  • 80