Skip to content

Instantly share code, notes, and snippets.

@jkaupp
Created April 18, 2017 16:55
Show Gist options
  • Save jkaupp/3929ea32e87378ed1bc6adb0999ef54c to your computer and use it in GitHub Desktop.
Save jkaupp/3929ea32e87378ed1bc6adb0999ef54c to your computer and use it in GitHub Desktop.
LOPUS Script
# The following code was written by Jake Kaupp at Queen's University, Kingston, Canada
# for looking at LOPUS observation data
# Date: February 2017
library(jsonlite)
library(plyr)
library(tidyverse)
library(lubridate)
# Data file is located in the same direoctory as this script
gorp_file <- list.files(pattern = "json") %>%
fromJSON(simplifyDataFrame = TRUE)
# Change unix time to ms
observations <- gorp_file$observations %>%
mutate_each(funs(./1000), starts_with("timestamp"))
# Convert start and stop to POSIXct objects
observations$timestamp_on <- as.POSIXct(observations$timestamp_on, origin = "1970-01-01", tz = "EST")
observations$timestamp_off <- as.POSIXct(observations$timestamp_off, origin = "1970-01-01", tz = "EST")
# Find the minimum time
min_time <- min(observations$timestamp_on)
max_time <- max(observations$timestamp_off)
# Create summary data
clean_data <- observations %>%
mutate(span = as.period(interval(timestamp_on, timestamp_off), unit = "seconds")) %>%
mutate_at(c("timestamp_off","timestamp_on"), function(x) difftime(x, min_time, units = "secs")) %>%
separate_rows(value, sep = ",")
axis_order <- c("Typical-L",
"Typical-Lab",
"Typical-TQ",
"Interactive-SQ",
"Interactive-1o1_SQ",
"Interactive-WC",
"Interactive-Prd",
"Interactive-SP",
"Interactive-SI",
"Noninstructive-SL",
"Noninstructive-W_students",
"Noninstructive-Other_student")
summary_data <- clean_data %>%
group_by(key) %>%
summarize(count = n(),
duration = sum(span, na.rm = TRUE)) %>%
filter(!grepl("Comment", key, ignore.case = TRUE)) %>%
mutate(fill = case_when(grepl("Typical", .$key) ~ "blue",
grepl("Interactive", .$key) ~ "black",
grepl("Noninstructive", .$key) ~ "purple",
TRUE ~ "grey"))
# Write it to a csv
summary_data %>%
write.csv("LOPUS Data.csv")
# Bar plot of summary data by count
ggplot(summary_data, aes(x = key, y = count)) +
geom_col(aes(fill = fill, color = fill)) +
scale_x_discrete(limits = rev(axis_order), breaks = rev(axis_order), drop = FALSE) +
scale_color_identity() +
scale_fill_identity() +
coord_flip()
# Ordered bar plot of actitivites in summary data
ggplot(summary_data, aes(x = reorder(key, count), y = count)) +
geom_col() +
coord_flip()
#Gorp Time span
max_time <- as.period(interval(min_time, max_time), unit = "minutes") %>%
as.numeric()/60
max <- round_any(max_time, 10, ceiling)
ggplot(clean_data, aes(y = key, group = key)) +
geom_segment(aes(x = timestamp_on, xend = timestamp_off, y = factor(key), yend = factor(key)), size = 5) +
scale_x_continuous(breaks = seq(0,max,2)*60, labels = seq(0,max,2)) +
theme_minimal() +
theme(panel.grid.minor = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment