-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackedBarChart.R
52 lines (43 loc) · 1.69 KB
/
StackedBarChart.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library(ggplot2)
library(data.table)
library(dplyr)
library(plyr)
library(here)
# Replace the name of each student with an anonymous identifier
# (Index of student name in sorted student list)
anonymize <- function(name, students){
indx <- match(name, students)
return(paste0("S", indx))
}
frequency <- function(count, total) {
per <- (count / total) * 100
return(paste0(round(per, digits = 2), "%"))
}
# Read in data
records <- read.csv(here("records.csv"), header = TRUE)
# Create anonymous ids
students <- sort(unique(records$Student))
ids <- sapply(records$Student, anonymize, students)
temp <- data.table(Student=ids, Class=records$Error.Class, Problem=records$Problem)
# filter out the no syntax errors
student_errors <- filter(temp, Class != "No syntax errors")
# Generate all graphs
for (x in c("FoodChain", "Family", "Total")){
if (x == "FoodChain") {
DT <- student_errors["Food Chain", on = "Problem"]
} else if (x == "Family") {
DT <- student_errors["Family", on = "Problem"]
} else {
DT <- student_errors
}
student_counts <- DT[, .(rowCount = .N), by = Student]
classes <- DT[, .(rowCount = .N), by = Class]
total <- sum(classes$rowCount)
class_counts <- data.table(Class=classes$Class, rowCount=classes$rowCount, Freq=sapply(classes$rowCount, frequency, total))
errors_per_student <- count(DT, c("Student", "Class"))
# Specify graph destination
pic_name = paste0("Student_Errors_", x, ".png")
# Plot and save images
sbc <- ggplot(data = DT, aes(x = Student)) + geom_bar(aes(fill = Class), position = position_stack(reverse = TRUE)) + coord_flip() + theme(legend.position = "top")
ggsave(filename = here("Images", pic_name), sbc, width = 10, height = 8)
}