R 中的多重对应分析。使用 ggplot2 绘制补充分类变量
Multiple Correspondence Analysis in R. Plotting supplementary categorical variables by using ggplot2
我最近使用了以下脚本来执行 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,所以我在这里有点迷路。
比如我想让"Tea"作为补充变量,应该怎么修改脚本?
#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))
我最近使用了以下脚本来执行 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,所以我在这里有点迷路。
比如我想让"Tea"作为补充变量,应该怎么修改脚本?
#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))