gpt4 book ai didi

r - 在 R 中可视化纵向分类数据的好方法

转载 作者:行者123 更新时间:2023-12-03 09:22:07 25 4
gpt4 key购买 nike

[ 更新:尽管我已经接受了一个答案,但如果您有其他可视化想法(无论是 R 还是其他语言/程序),请添加另一个答案。分类数据分析的文本似乎没有过多地说明纵向数据的可视化,而纵向数据分析的文本似乎没有过多地说明将类别成员随时间推移的主题内变化可视化。对这个问题有更多的答案将使它成为一个更好的资源,可以解决标准引用文献中没有太多涵盖的问题。]

一位同事刚刚给了我一个纵向分类数据集供我查看,我正试图弄清楚如何在可视化中捕捉纵向方面。我在这里发帖,因为我想在 R 中执行此操作,但请告诉我是否也可以交叉发布到 Cross-Validated,因为通常不鼓励交叉发布。

快速背景:数据跟踪了参加学术咨询计划的学生每学期的学术地位。数据采用长格式,有五个变量:“id”、“cohort”、“term”、“standing”和“termGPA”。前两个标识学生和他们在咨询计划中的学期。后三个是记录学生学业成绩和 GPA 的术语。我使用 dput 在下面粘贴了一些示例数据.

我创建了一个马赛克图(见下文),按队列、排名和学期对学生进行分组。这显示了每个学期在每个学术水平类别中的学生比例。但这并没有捕捉到纵向方面——随着时间的推移跟踪个别学生的事实。我想跟踪具有特定学术地位的学生群体随着时间的推移所走的道路。

例如:在 2009 年秋季(“F09”)中获得“AP”(学术试用)的学生中,有多少比例在 future 的学期中仍然是 AP,哪些比例进入了其他类别(例如,GS,“良好的信誉”)?自进入咨询计划以来,在不同类别之间的移动方面是否存在差异?

我无法弄清楚如何在 R 图形中捕捉这个纵向方面。 vcd包具有可视化分类数据的功能,但似乎没有解决纵向分类数据。是否有可视化纵向分类数据的“标准”方法? R 是否有为此设计的软件包?长格式适合这种类型的数据还是宽格式会更好?

我会很感激关于解决这个特定问题的建议,以及对文章、书籍等的建议,以了解更多关于可视化纵向分类数据的信息。

这是我用来制作马赛克图的代码。该代码使用下面列出的数据 dput .

library(RColorBrewer)

# create a table object for plotting
df1.tab = table(df1$cohort, df1$term, df1$standing,
dnn=c("Cohort\nAcademic Standing", "Term", "Standing"))

# create a mosaic plot
plot(df1.tab, las=1, dir=c("h","v","h"),
col=brewer.pal(8,"Dark2"),
main="Fall 2009 and Fall 2010 Cohorts")

这是马赛克图(附带问题:有什么方法可以让 F10 队列的列直接位于 F09 队列的下方并具有相同的宽度,即使 F10 队列中的某些术语没有数据?) :

enter image description here

这是用于创建表格和绘图的数据:
df1 =
structure(list(id = c(101L, 102L, 103L, 104L, 105L, 106L, 107L,
108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L,
119L, 120L, 121L, 122L, 123L, 124L, 125L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L,
116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 101L,
102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L,
113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L,
124L, 125L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L,
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 125L, 101L, 102L, 103L, 104L, 105L, 106L,
107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L,
118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L,
115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L,
101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L,
112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L,
123L, 124L, 125L), cohort = structure(c(1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 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4L, 6L, 6L, 4L,
4L), .Label = c("AP", "CP", "DQ", "GS", "DM", "NE", "WD"), class = "factor"),
termGPA = c(1.433, 1.925, 1, 1.68, NA, 1.579, 1.233, NA,
NA, NA, NA, 2.009, 1.675, 0, 1.5, 1.86, 0.5, 0.94, NA, NA,
1.777, 1.1, 1.133, 1.675, NA, 2, 1.25, 1.66, 0, NA, 1.525,
2.25, NA, NA, NA, NA, 1.66, 2.325, 0, 2.308, 1.6, 1.825,
2.33, NA, NA, 2.65, 2.65, 2.85, 3.233, NA, 1.25, 1.575, NA,
NA, 1, 2.385, 3.133, 0, 0, 1.729, 1.075, 0, 4, NA, 2.74,
0, 1.369, 2.53, 0, 2.65, 2.75, 0, 0.333, 3.367, 1, NA, 0.1,
NA, NA, 1, 2.2, 2.18, 2.31, 1.75, 3.073, 0.7, NA, 1.425,
NA, 2.74, 2.9, 0.692, 2, 0.75, 1.675, 2.4, NA, NA, 3.829,
2.33, 2.3, 1.5, NA, NA, NA, 2.69, 1.52, 0.838, 2.35, 1.55,
NA, 1.35, 0.66, NA, NA, 1.35, 1.9, 1.04, NA, 1.464, 2.94,
NA, NA, 3.72, 2.867, 1.467, NA, 3.133, NA, 1, 2.458, 1.214,
NA, 3.325, 2.315, NA, 1, 2.233, NA, NA, 2.567, 1, NA, 0,
3.325, 2.077, NA, NA, 3.85, 2.718, 1.385, NA, 2.333, NA,
2.675, 1.267, 1.6, 1.388, 3.433, 0.838, NA, NA, 0, NA, NA,
2.6, 0, NA, NA, 1, 2.825, NA, NA, 3.838, 2.883)), .Names = c("id",
"cohort", "term", "standing", "termGPA"), row.names = c("101.F09.s09",
"102.F09.s09", "103.F09.s09", "104.F09.s09", "105.F10.s09", "106.F09.s09",
"107.F09.s09", "108.F10.s09", "109.F10.s09", "110.F10.s09", "111.F10.s09",
"112.F09.s09", "113.F09.s09", "114.F09.s09", "115.F09.s09", "116.F09.s09",
"117.F09.s09", "118.F09.s09", "119.F10.s09", "120.F10.s09", "121.F09.s09",
"122.F09.s09", "123.F09.s09", "124.F09.s09", "125.F10.s09", "101.F09.f09",
"102.F09.f09", "103.F09.f09", "104.F09.f09", "105.F10.f09", "106.F09.f09",
"107.F09.f09", "108.F10.f09", "109.F10.f09", "110.F10.f09", "111.F10.f09",
"112.F09.f09", "113.F09.f09", "114.F09.f09", "115.F09.f09", "116.F09.f09",
"117.F09.f09", "118.F09.f09", "119.F10.f09", "120.F10.f09", "121.F09.f09",
"122.F09.f09", "123.F09.f09", "124.F09.f09", "125.F10.f09", "101.F09.s10",
"102.F09.s10", "103.F09.s10", "104.F09.s10", "105.F10.s10", "106.F09.s10",
"107.F09.s10", "108.F10.s10", "109.F10.s10", "110.F10.s10", "111.F10.s10",
"112.F09.s10", "113.F09.s10", "114.F09.s10", "115.F09.s10", "116.F09.s10",
"117.F09.s10", "118.F09.s10", "119.F10.s10", "120.F10.s10", "121.F09.s10",
"122.F09.s10", "123.F09.s10", "124.F09.s10", "125.F10.s10", "101.F09.f10",
"102.F09.f10", "103.F09.f10", "104.F09.f10", "105.F10.f10", "106.F09.f10",
"107.F09.f10", "108.F10.f10", "109.F10.f10", "110.F10.f10", "111.F10.f10",
"112.F09.f10", "113.F09.f10", "114.F09.f10", "115.F09.f10", "116.F09.f10",
"117.F09.f10", "118.F09.f10", "119.F10.f10", "120.F10.f10", "121.F09.f10",
"122.F09.f10", "123.F09.f10", "124.F09.f10", "125.F10.f10", "101.F09.s11",
"102.F09.s11", "103.F09.s11", "104.F09.s11", "105.F10.s11", "106.F09.s11",
"107.F09.s11", "108.F10.s11", "109.F10.s11", "110.F10.s11", "111.F10.s11",
"112.F09.s11", "113.F09.s11", "114.F09.s11", "115.F09.s11", "116.F09.s11",
"117.F09.s11", "118.F09.s11", "119.F10.s11", "120.F10.s11", "121.F09.s11",
"122.F09.s11", "123.F09.s11", "124.F09.s11", "125.F10.s11", "101.F09.f11",
"102.F09.f11", "103.F09.f11", "104.F09.f11", "105.F10.f11", "106.F09.f11",
"107.F09.f11", "108.F10.f11", "109.F10.f11", "110.F10.f11", "111.F10.f11",
"112.F09.f11", "113.F09.f11", "114.F09.f11", "115.F09.f11", "116.F09.f11",
"117.F09.f11", "118.F09.f11", "119.F10.f11", "120.F10.f11", "121.F09.f11",
"122.F09.f11", "123.F09.f11", "124.F09.f11", "125.F10.f11", "101.F09.s12",
"102.F09.s12", "103.F09.s12", "104.F09.s12", "105.F10.s12", "106.F09.s12",
"107.F09.s12", "108.F10.s12", "109.F10.s12", "110.F10.s12", "111.F10.s12",
"112.F09.s12", "113.F09.s12", "114.F09.s12", "115.F09.s12", "116.F09.s12",
"117.F09.s12", "118.F09.s12", "119.F10.s12", "120.F10.s12", "121.F09.s12",
"122.F09.s12", "123.F09.s12", "124.F09.s12", "125.F10.s12"), reshapeLong = structure(list(
varying = list(c("s09as", "f09as", "s10as", "f10as", "s11as",
"f11as", "s12as"), c("s09termGPA", "f09termGPA", "s10termGPA",
"f10termGPA", "s11termGPA", "f11termGPA", "s12termGPA")),
v.names = c("standing", "termGPA"), idvar = c("id", "cohort"
), timevar = "term"), .Names = c("varying", "v.names", "idvar",
"timevar")), class = "data.frame")

最佳答案

这里有一些绘制数据的想法。我使用了 ggplot2,并且在某些地方重新格式化了数据。

图1

enter image description here
我使用堆叠条形图来模拟您的马赛克图并解决对齐问题。

图2

enter image description here
每个学生的数据点由一条灰线连接,让人联想到平行坐标图。为点着色显示分类地位。在 y 轴上使用 GPA 有助于分散点以减少过度绘制,并显示站立和 GPA 的相关性。一个主要问题是许多有效的standing数据点丢失是因为它们缺少匹配的 termGPA 值。

图 3

enter image description here
在这里,我创建了一个名为 initial_standing 的新变量用于分面。每个面板包含在 cohort 和 initial_standing 中都匹配的学生。将 id 绘制为文本会使这个数字有点困惑,但在某些情况下可能很有用。

图 4

enter image description here
这个图就像一个热图,其中每一行都是一个学生。我控制了id的顺序轴强制 initial_standing 和同类群组保持在一起。如果您有更多行,您可能需要考虑按某种类型的聚类对行进行排序。

library(ggplot2)

# Create new data frame for determining initial standing.
standing_data = data.frame(id=unique(df1$id), initial_standing=NA, cohort=NA)

for (i in 1:nrow(standing_data)) {
id = standing_data$id[i]
subdat = df1[df1$id == id, ]
subdat = subdat[complete.cases(subdat), ]
initial_standing = subdat$standing[which.min(subdat$term)]
standing_data[i, "initial_standing"] = as.character(initial_standing)
standing_data[i, "cohort"] = as.character(subdat$cohort[1])
}

standing_data$cohort = factor(standing_data$cohort, levels=levels(df1$cohort))
standing_data$initial_standing = factor(standing_data$initial_standing,
levels=levels(df1$standing))

# Add the new column (initial_standing) to df1.
df1 = merge(df1, standing_data[, c("id", "initial_standing")], by="id")

# Remove rows where standing is missing. Make some plots tidier.
df1 = df1[!is.na(df1$standing), ]

# Create id factor, controlling the sort order of the levels.
id_order = order(standing_data$initial_standing, standing_data$cohort)
df1$id = factor(df1$id, levels=as.character(standing_data$id)[id_order])


p1 = ggplot(df1, aes(x=term, fill=standing)) +
geom_bar(position="fill", colour="grey20", size=0.5, width=1.0) +
facet_grid(cohort ~ .) +
scale_fill_brewer(palette="Set1")

p2 = ggplot(df1, aes(x=term, y=termGPA, group=id)) +
geom_line(colour="grey70") +
geom_point(aes(colour=standing), size=4) +
facet_grid(cohort ~ .) +
scale_colour_brewer(palette="Set1")

p3 = ggplot(df1, aes(x=term, y=termGPA, group=id)) +
geom_line(colour="grey70") +
geom_point(aes(colour=standing), size=4) +
geom_text(aes(label=id), hjust=-0.30, size=3) +
facet_grid(initial_standing ~ cohort) +
scale_colour_brewer(palette="Set1")


p4 = ggplot(df1, aes(x=term, y=id, fill=standing)) +
geom_tile(colour="grey20") +
facet_grid(initial_standing ~ ., space="free_y", scales="free_y") +
scale_fill_brewer(palette="Set1") +
opts(panel.grid.major=theme_blank()) +
opts(panel.grid.minor=theme_blank())

ggsave("plot_1.png", p1, width=10, height=6.25, dpi=80)
ggsave("plot_2.png", p2, width=10, height=6.25, dpi=80)
ggsave("plot_3.png", p3, width=10, height=6.25, dpi=80)
ggsave("plot_4.png", p4, width=10, height=6.25, dpi=80)

关于r - 在 R 中可视化纵向分类数据的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11513149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com