我正在尝试绘制一个散点图,显示每个问题为每个学生获得的分数百分比,并计算整个班级的平均值。
maxmarks.paper <- c(4,5,2,4,3,4,5,4,6,3,3,5,3,4,4,4,3,3,2,5,4)
results.student1 <- c(4,3,2,3,3,4,5,4,3,3,0,0,2,4,1,1,2,0,1,0,0)
results.student2 <- c(2,2,0,4,1,1,0,1,4,2,0,0,1,0,0,2,0,0,1,1,0)
#etc.
percentage1 <- results.student1 / maxmarks.paper * 100
percentage2 <- results.student2 / maxmarks.paper * 100
#etc.
我正在查看this教程,但我不明白这是如何工作的。
plot(x = "questions 1:20", y= "percentage of marks gained per student" ...)
有人可以帮助绘制散点图吗?
答案 0 :(得分:1)
好的,这里是您的代码以更简洁的方式表达,以及我认为您的要求的情节,但仍然不完全清楚:
maxmarks.paper <- c(4,5,2,4,3,4,5,4,6,3,3,5,3,4,4,4,3,3,2,5,4)
# Input the results as a data frame rather than individual vectors
results <- data.frame(
student1 = c(4,3,2,3,3,4,5,4,3,3,0,0,2,4,1,1,2,0,1,0,0),
student2 = c(2,2,0,4,1,1,0,1,4,2,0,0,1,0,0,2,0,0,1,1,0)
)
# Calculate the percentages all at once instead of repeating the equation over and over
percentages <- data.frame(apply(results, 2, function(student) {
return(student / maxmarks.paper * 100)
}))
# Calculate the class average on each question and store it in the same table
percentages$class_average <- apply(percentages, 1, mean)
# Plot the average mark for each question and label the axes
plot(x = 1:21, y = percentages$class_average, xlab = "Questions 1:21", ylab = "Class Average (%)", pch = 19)
# Add points to the chart for each student
for (i in 1:2) {
points(x = 1:21, y = percentages[[paste0("student", i)]], col = "green", pch = 19)
}
答案 1 :(得分:0)
使用此公式,您可以绘制比较每对学生的散点图,例如
Create application without a manifest
要计算整个班级的平均值,您需要总结每个问题的数据。对于散点图,您只使用两个变量;如果你有20个问题,那么你只能为每对问题做到这一点。
答案 2 :(得分:0)
对于每个学生,您似乎正在尝试显示每个问题的百分比标记。您需要在同一图表上绘制多条线。
继续 efbbrown 的回答,(存储results
并更有效地计算百分比)。
maxmarks.paper <- c(4, 5, 2, 4, 3, 4, 5, 4, 6, 3, 3, 5, 3, 4, 4, 4, 3, 3, 2, 5, 4)
# Input the results as a data frame rather than individual vectors
results <- data.frame(
student1 = c(4, 3, 2, 3, 3, 4, 5, 4, 3, 3, 0, 0, 2, 4, 1, 1, 2, 0, 1, 0, 0),
student2 = c(2, 2, 0, 4, 1, 1, 0, 1, 4, 2, 0, 0, 1, 0, 0, 2, 0, 0, 1, 1, 0)
)
# Calculate the percentages all at once instead of repeating the equation
over and over
percentages <- data.frame(apply(results, 2, function(student) {
return(student / maxmarks.paper * 100)
}))
现在绘制图表:
plot (x = 1:21, y = percentages$student1, type = "p", xlab = "Questions in paper", ylab = "Class Average (%)")
type = "p"
参数将仅绘制点,添加另一个向量,使用lines
函数。
lines(x = 1:21, y = percentages$student2, type = "p", xlab = "Questions in paper", ylab = "Class Average (%)")
您可以为每位学生运行此操作,将他们的百分比添加到图表中。要计算所有学生的平均使用率:
# from efbbrown's answer
percentages$class.average <- apply(percentages, 1, mean)
然后,您可以使用以下方法将其添加到图表中:
lines(x = 1:21, y = percentages$class.average, type = "l", xlab = "Questions in paper", ylab = "Class Average (%)")
type = "l"
将仅显示该行,同样type = "o"
将同时显示行和点。