R.中的多重对应分析使用ggplot2绘制补充分类变量 [英] Multiple Correspondence Analysis in R. Plotting supplementary categorical variables by using ggplot2

查看:1710
本文介绍了R.中的多重对应分析使用ggplot2绘制补充分类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用了下面的脚本来执行MCA分析并将情节可视化(我发现它在 http://gastonsanchez.com/blog/how-to/2012/10/13/MCA-in-R.html )。

数据来自R包FactoMineR中的数据框Tea。

 #加载数据茶
数据(茶)

#选择这些列
newtea = tea [,c(Tea,How,how,sugar,where,always)]

#每个变量的类别数量
cats = apply(newtea,2,function(x)nlevels(as.factor(x)))

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

#可变坐标数据框
mca1_vars_df = data.frame(mca1 $ var $ coord,Variable = rep(names(cats),cats))

#数据框与观察坐标
mca1_obs_df = data.frame(mca1 $ ind $ coord)

#变量类别图
ggplot(data = mca1_vars_df,
aes x = Dim.1,y = Dim.2,label = rownames(mca1_vars_df)))+
geom_hline(yintercept = 0,color =gray70)+
geom_vline(xintercept = 0,color = gray70)+
geom_text(aes(color = Variable))+
ggtitle(使用R package FactoMineR的变量的MCA图)

它运行完美,但我想要t o知道如何在分析中引入定性补充变量。由于我对ggplot2完全不熟悉,所以我在这里有点失落。例如,如果我想把茶作为补充变量,我应该如何修改脚本?



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

但是我怎么能在ggplot脚本中保存这些信息呢?

解决方案

您需要获取 MCA 对象中补充变量的坐标。它们位于 mca1 $ quali.sup $ coord 中,就像活动变量的坐标位于 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))


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?

解决方案

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绘制补充分类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆