Skip to content

Instantly share code, notes, and snippets.

@fmichonneau
Last active June 26, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmichonneau/de79f7fe3dddd38becaef4add09d20c5 to your computer and use it in GitHub Desktop.
Save fmichonneau/de79f7fe3dddd38becaef4add09d20c5 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(maps)
library(gganimate)
library(countrycode)
wkshp_data <- read_csv("https://data.softwarecarpentry.org/api/queries/118/results.csv?api_key=GR8itg5dtYtq1A1Myv8bR5rrDNMZRlGc4cMVSnfB") %>%
filter(start_date < "2019-07-01")
wkshp_data <- wkshp_data %>%
filter(latitude < 90) %>%
mutate(year = lubridate::year(start_date))
wkshp_data_no_online <- wkshp_data %>%
filter(country != "W3" &
!grepl("online", tag_name)) %>%
mutate(country_name = countrycode(
country,
"iso2c",
"country.name"
))
summ_by_country <- wkshp_data_no_online %>%
count(year, country_name) %>%
complete(year, country_name, fill = list(n = 0)) %>%
group_by(country_name) %>%
mutate(cum_n = cumsum(n),
cum_n = replace(cum_n, cum_n == 0, NA))
## first test, only points
ggplot() +
geom_map(data=world, map=world,
aes(x = long, y = lat, group = group, map_id = region),
fill = "lightgrey", size = 0.5) +
geom_point(data = wkshp_data,
aes(
x = longitude,
y = latitude,
color = as.factor(year))
) +
coord_quickmap() +
theme_minimal() +
transition_time(start_date) +
ease_aes('linear')
## to shade by country, we need to standardize the country names
world <- map_data("world") %>%
mutate(region_2 = countrycode::countrycode(
region,
"country.name",
"country.name"
))
res <- map_df(
unique(summ_by_country$year),
function(.x) {
left_join(world, filter(summ_by_country, year == .x),
by = c("region_2" = "country_name")) %>%
mutate(
year = replace_na(year, min(year, na.rm = TRUE)),
start_date = parse_date(
paste0(year, "-12-31"))
)
}
)
## and add the columns we need
res <- bind_rows(
world %>%
mutate(
n = NA,
cum_n = NA,
start_date = as.Date("2011-12-31")
),
res
)
anim <- ggplot() +
geom_map(aes(fill = cum_n, x = long, y = lat, map_id = region),
data = res, map = world) +
scale_fill_viridis_c(na.value = "gray70",
breaks = c(1, 10, 100, 500),
trans = "log", name = "Number of Workshops") +
geom_point(data = wkshp_data_no_online,
aes(x = longitude, y = latitude,
color = "#dda631"),
inherit.aes = FALSE) +
coord_quickmap() +
theme_minimal() +
theme(legend.position = "bottom") +
labs(
x = "", y = ""
) +
scale_color_identity(guide = FALSE) +
transition_time(start_date)
## gif version
anim_map <- animate(anim, width = 800, height = 600, res = 144)
anim_save("map-workshop-through-time-animation.gif", anim_map)
### Workshops through time -----------------------------------------------------
wkshp_data <- wkshp_data %>%
arrange(start_date) %>%
mutate(n_wkshp = row_number())
ggplot(wkshp_data, aes(start_date, n_wkshp)) +
geom_line() +
geom_point(size = 2) +
transition_reveal(start_date) +
labs(title = 'Number of Carpentries workshops (since 2012)',
y = '', x = '') +
theme_minimal()
### Waffle plots ---------------------------------------------------------------
wfle <- wkshp_data %>%
mutate(workshop_type = case_when(
grepl("SWC", tag_name) ~ "Software Carpentry",
grepl("DC", tag_name) ~ "Data Carpentry",
grepl("LC", tag_name) ~ "Library Carpentry",
grepl("TTT", tag_name) ~ "Instructor Training",
TRUE ~ "issue"
)) %>%
count(year, workshop_type) %>%
ungroup() %>%
group_by(year) %>%
mutate(tot = sum(n)) %>%
ungroup() %>%
mutate(pad = max(tot) - tot)
stopifnot(sum(wfle$workshop_type == "issue") == 0)
## install from GitHub:
## remotes::install_github("hrbrmstr/waffle")
library(waffle)
library(emojifont)
plt <- wfle %>%
split(.$year) %>%
purrr::map(function(.x) {
title <- .x$year[1]
pad <- .x$pad[1]
message(str(title))
cols <- c(
"Data Carpentry" = "darkcyan",
"Software Carpentry" = "#2e3c91",
"Instructor Training" = "#f6a570")[unique(.x$workshop_type)]
.x <- .x %>%
select(workshop_type, n) %>%
arrange(factor(workshop_type,
c("Software Carpentry", "Data Carpentry", "Instructor Training")))
dt <- pull(.x, n) %>%
set_names(.x$workshop_type)
waffle(dt, rows = 5, pad = pad/5, keep = FALSE,
size = .5, legend = "none",
title = title, colors = cols)
})
svg(height = 8, width = 11)
iron(plt[[1]], plt[[2]], plt[[3]], plt[[4]],
plt[[5]], plt[[6]], plt[[7]])
dev.off()
## All workshops
d <- data.frame(n = c(1, 10, 100), x = 1:3, y = rep(1, 3))
svg("number_workshops.svg", height = 2, width = 6)
wfle %>%
group_by(year) %>%
summarize(
n = sum(n)
) %>%
ggplot() +
geom_text(aes(year, I(1), size = n),
label = fontawesome("fa-users"),
family = "fontawesome-webfont") +
geom_text(aes(year, I(.7), label = n)) +
geom_text(aes(year, I(.2), label = year)) +
scale_size(range = c(5, 10)) +
theme_void() +
theme(plot.margin = margin(50, 4.5, 4.5, 4.5), legend.position = "none") +
coord_cartesian(clip = "off")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment