Last active
April 14, 2016 13:32
-
-
Save jonocarroll/1ce3ba63171eca2de22731c2503a1f48 to your computer and use it in GitHub Desktop.
Plot the #auunconf slack users' timezones on a map
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
## Create a world timezone map of the #auunconf slack users' timezones | |
## data extracted via hrbrmstr/slackr | |
## Timezone shapefile: http://efele.net/maps/tz/world/ | |
## | |
## Blogged @ http://jcarroll.com.au/2016/04/14/slack-timezones/ | |
## load relevant packages | |
pacman::p_load(rgdal, maptools, ggplot2) | |
pacman::p_load(ggthemes, albersusa, ggalt) | |
pacman::p_load(extrafont) | |
# devtools::install_github("hrbrmstr/slackr") | |
pacman::p_load(slackr) | |
## load the world timezone shapefile and process | |
worldtz <- readOGR(dsn=".", layer="tz_world_mp") | |
worldtz@data$id = rownames(worldtz@data) | |
worldtz_map <- broom::tidy(worldtz, region="id") | |
## I wanted to shift the center of the projection to the Pacific. | |
## It looks like @hrbrmstr has some code that does this on stackoverflow, | |
## but between one computer failing the ogr2ogr steps and another | |
## crashing when generating the ggplot, I am abandoning this for now. | |
## http://stackoverflow.com/questions/32591368/pacific-centric-robinson-projection-with-ggplot-in-r | |
# system("ogr2ogr world_part1.shp tz_world_mp.shp -clipsrc -180 -90 0 90") | |
# system("ogr2ogr world_part2.shp tz_world_mp.shp -clipsrc 0 -90 180 90") | |
# system('ogr2ogr world_part1_shifted.shp world_part1.shp -dialect sqlite -sql "SELECT ShiftCoords(geometry,360,0) FROM world_part1"') | |
# system("ogr2ogr world_0_360_raw.shp world_part2.shp") | |
# system("ogr2ogr -update -append world_0_360_raw.shp world_part1_shifted.shp -nln world_0_360_raw") | |
# world <- readOGR("world_0_360_raw.shp", "world_0_360_raw") | |
# world_robin <- spTransform(world, CRS("+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")) | |
# worldtz_map <- broom::tidy(world_robin) | |
# save(worldtz_map, file="world_robin_pacific.RData") | |
# load("world_robin_pacific.RData") | |
## merge the map with the map data | |
worldtz.df = merge(worldtz_map, worldtz@data, by="id") | |
## users via slackr | |
## access API token and environment variables stored in ~/.slackr | |
slackr_setup() | |
users <- slackr_users() | |
# save(users, file="users.RData") | |
load("users.RData") | |
## the available users data (names redacted) | |
# dplyr::glimpse(users) | |
# Observations: 32 | |
# Variables: 35 | |
# $ id (chr) "U0ZQXD867", "U0ZSDK6GH", "U0ZQTP351", "U0ZQVHU03", "U1088MC9E", "U0ZTED8J1", "U0ZS72KEU", "U0ZR0L5S7", ... | |
# $ team_id (chr) "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", "T0X0SF0R1", ... | |
# $ name (chr) ############################### | |
# $ deleted (lgl) FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,... | |
# $ status (lgl) NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ... | |
# $ color (chr) "9b3b45", "5b89d5", "99a949", "4cc091", "43761b", "235e5b", "684b6c", "5a4592", "d58247", "e0a729", "df3... | |
## <snip> | |
## merge the users in with the data/map | |
worldtz.df.users <- merge(worldtz.df, users, by.x="TZID", by.y="tz", all=TRUE) | |
## create a factor identifying whether a timezone has some users | |
worldtz.df.users$hasUser <- as.factor(ifelse(is.na(worldtz.df.users$name), 0, 1)) | |
## create the projected map | |
gg <- ggplot(worldtz.df.users) | |
gg <- gg + theme_map() | |
gg <- gg + labs(title=paste0("#auunconf slack users' timezone locations"), | |
subtitle="Data extracted via slackr::slackr_users on 14-Apr-2016", | |
caption="Timezone shapefile: http://efele.net/maps/tz/world/ | Blog: www.jcarroll.com.au ") | |
gg <- gg + geom_map(map=worldtz_map, | |
aes(x=long, y=lat, map_id=id.x, fill=hasUser), | |
color="grey60", size=0.2) | |
gg <- gg + coord_proj() | |
gg <- gg + scale_fill_manual(guide="none", values=c("white", "cyan")) | |
gg <- gg + theme(text=element_text(size=16, family="Arial Narrow")) | |
gg | |
## save a .png copy | |
ggsave(gg, filename="auunconf_slackr_users_map.png", height=8, width=8, dpi=300) | |
## upload to the #auunconf #general channel | |
dev_slackr("#general") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment