【问题标题】:Multiple Correspondence Analysis in R. Plotting supplementary categorical variables by using ggplot2R中的多重对应分析。使用ggplot2绘制补充分类变量
【发布时间】:2015-08-16 17:02:43
【问题描述】:

我最近使用以下脚本来执行 MCA 分析并可视化绘图(我在 http://gastonsanchez.com/blog/how-to/2012/10/13/MCA-in-R.html 上找到了它)。

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

# 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")

它运行完美,但我想知道如何在分析中引入定性补充变量。由于我根本不熟悉 ggplot2,所以我有点迷失在这里。

例如,如果我想“茶”作为补充变量,我应该如何修改脚本?

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

但是如何在 ggplot 脚本中保存这些信息呢?

【问题讨论】:

  • 编程问题在这里是题外话,更适合 Stack Overflow。您的可以迁移,但我建议您首先编辑问题以准确解释您想要绘制的内容 - mca1 对象的哪一部分 - 以及您希望它如何显示。
  • 感谢您的评论!实际上,我只是想知道(1)如何区分上述脚本中的补充变量和活动变量,以及(2)如何在 ggplot2 脚本中保留这种区别。以防万一,请随时将此主题移至 Stack Overflow!

标签: r ggplot2


【解决方案1】:

您需要在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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2019-04-14
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多