Last active
October 13, 2015 06:38
-
-
Save tts/4154941 to your computer and use it in GitHub Desktop.
Treemap of Aalto courses by school and department
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################### | |
# | |
# Treemap of Aalto courses by school and department. | |
# The color palette indicates the average ECTS | |
# of the courses of the department. | |
# | |
# Data from Linked Open Aalto Data Service | |
# http://data.aalto.fi/ | |
# | |
# Tuija Sonkkila 27.11.2012, 18.5.2013 | |
# | |
# Note that ECON courses do not have | |
# any granularity | |
# | |
# The official school acronyms of today are | |
# TaiK -> ARTS | |
# ECON -> BIZ | |
# CHEM | |
# ELEC | |
# ENG | |
# SCI | |
# | |
#################################################### | |
library(SPARQL) | |
endpoint <- "http://data.aalto.fi/sparql" | |
q <- ' | |
SELECT ?courseCode ?ectsValue ?deptCode ?schoolCode | |
WHERE | |
{ | |
GRAPH <http://data.aalto.fi/id/courses/noppa/> | |
{ | |
?course <http://purl.org/vocab/aiiso/schema#code> ?courseCode ; | |
<http://linkedscience.org/teach/ns#ects> ?ects . | |
BIND (IF (regex(?ects, "-"), | |
REPLACE(?ects, "^.*-", ""), | |
?ects) AS ?ectsValue) | |
?dept <http://purl.org/vocab/aiiso/schema#teaches> ?course ; | |
<http://purl.org/vocab/aiiso/schema#code> ?deptCode ; | |
<http://purl.org/vocab/aiiso/schema#part_of> ?school . | |
?school <http://purl.org/vocab/aiiso/schema#organization> ?dept ; | |
<http://purl.org/vocab/aiiso/schema#code> ?schoolCode . | |
} | |
} | |
ORDER BY ?schoolCode ?deptCode ?courseCode' | |
res <- SPARQL(url=endpoint, query=q)$results | |
# Take a copy of the res dataframe | |
coursedata <- res | |
# Change the datatype of ects | |
coursedata$ectsValue <- as.numeric(coursedata$ectsValue) | |
# EDIT 18.5.2013 Rename ECON -> BIZ, TaiK -> ARTS, Eri -> OU (Open University) | |
coursedata$schoolCode <- sub('ECON', 'BIZ', coursedata$schoolCode) | |
coursedata$schoolCode <- sub('TaiK', 'ARTS', coursedata$schoolCode) | |
coursedata$schoolCode <- sub('ERI', 'OU', coursedata$schoolCode) | |
# Factorize dept names | |
# | |
# 29.11.2012 don't do this, results in a malplotted map | |
# see https://blogs.aalto.fi/suoritin/2012/11/28/course-data-up-on-a-treemap/ | |
# coursedata$deptCode <- factor(coursedata$deptCode) | |
# Count number of courses per department | |
deptsCourses <- aggregate(courseCode ~ deptCode, | |
data = coursedata, | |
FUN = "length") | |
# Calculate average ects by department with aggregating by department and school | |
depts <- aggregate(x = coursedata$ectsValue, | |
by = list(dept = coursedata$deptCode, | |
school = coursedata$schoolCode), | |
FUN = mean) | |
# Merge the two dataframes by the department code | |
coursedata.merged <- merge(x = deptsCourses, | |
y = depts, | |
by.x = "deptCode", | |
by.y = "dept") | |
# Clean up variable names | |
colnames(coursedata.merged) <- c("dept", "courses", "school", "ECTS average") | |
# Plot a treemap with the treemap library and with a certain Brewer palette | |
library(treemap) | |
library(RColorBrewer) | |
png("aalto.courses.treemap.png", width = 1024, height = 768, res = 72) | |
tmPlot(coursedata.merged, | |
index = c("school", "dept"), | |
vSize = "courses", | |
vColor = "ECTS average", | |
type = "value", | |
palette = "YlOrBr", | |
range = c(0, 11), | |
title = "Treemap of Aalto courses by School and department", | |
subtitle = "Average ECTS by department") | |
dev.off() | |
# A treemap with the portfolio library. The color is hard-coded though | |
# | |
# library(portfolio) | |
# | |
# png("aalto.courses.portfolio.treemap.png", width = 1024, height = 768, res = 72) | |
# map.market(id = coursedata.merged$dept, | |
# area = coursedata.merged$courses, | |
# group = coursedata.merged$school, | |
# color = coursedata.merged$'ECTS average', | |
# lab = c(TRUE, TRUE), | |
# main="Aalto courses by School") | |
# dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment