Multiple Correspondence Analysis in R. Plotting su

2019-04-17 13:21发布

问题:

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?

回答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))


标签: r ggplot2