Last active
April 16, 2024 16:10
-
-
Save beatrizmilz/607203e4740d42b55cb3040782db5db5 to your computer and use it in GitHub Desktop.
Map with R/sf/ggplot2 - R-Ladies Chapters in Australia
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
# Exploring R-Ladies Chapters in Australia! | |
# By: Beatriz Milz - beamilz.com | |
# R-Ladies events dataset: | |
# https://raw.githubusercontent.com/rladies/meetup_archive/main/data/events.json | |
# Filtered by only events that have lat/long in the dataset. | |
# Getting the polygon for Australia ---- | |
temporary_dir <- fs::path_temp() | |
data_world <- geodata::world(path = temporary_dir) | |
data_world_sf <- data_world |> | |
sf::st_as_sf() | |
australia <- data_world_sf |> | |
dplyr::filter(GID_0 == "AUS") | |
crs_aus <- sf::st_crs(australia) | |
# R-Ladies events ----- | |
data_raw <- | |
jsonlite::fromJSON( | |
"https://raw.githubusercontent.com/rladies/meetup_archive/main/data/events.json", | |
simplifyDataFrame = TRUE | |
) | |
# Preparing the dataset ---- | |
rladies_sf <- data_raw |> | |
dplyr::filter(type != "cancelled") |> | |
tidyr::drop_na(lon, lat) |> | |
sf::st_as_sf(coords = c("lon", "lat")) |> | |
sf::st_set_crs(crs_aus) | |
rladies_sf_australia <- rladies_sf |> | |
dplyr::arrange(desc(start)) |> | |
dplyr::distinct(group_urlname, .keep_all = TRUE) |> | |
sf::st_filter(australia) |> | |
dplyr::mutate( | |
status = dplyr::case_when( | |
date >= as.Date("2023-01-01") ~ "Active", | |
TRUE ~ "Inactive" | |
), | |
name = stringr::str_remove(group_urlname, "rladies-") |> | |
stringr::str_to_title() | |
) |> | |
dplyr::filter(group_urlname != "rladies-eskisehir") | |
# Creating a ggplot! | |
library(ggplot2) | |
map_rladies <- ggplot() + | |
geom_sf( | |
data = australia, | |
fill = "white" | |
) + | |
geom_sf(data = rladies_sf_australia, aes(color = status)) + | |
scale_color_manual(values = c("#88398a", "#3b3b3b")) + | |
labs( | |
title = "R-Ladies Chapters in Australia", | |
subtitle = "There are 3 active chapters: \n Sydney, Melbourne and Canberra.", | |
color = "Status", | |
caption = "Plot by Beatriz Milz, using R and the packages:\n sf, geodata, ggplot2, tidyverse. Data from Meetup." | |
) + | |
geom_sf_label( | |
data = dplyr::filter(rladies_sf_australia, !name %in% c("Sydney", "Canberra")), | |
aes( | |
label = name, | |
color = status | |
), | |
nudge_x = 0, | |
nudge_y = -1.5, | |
show.legend = FALSE, | |
size = 3 | |
) + | |
geom_sf_label( | |
data = dplyr::filter(rladies_sf_australia, name %in% c("Sydney", "Canberra")), | |
aes( | |
label = name, | |
color = status | |
), | |
nudge_x = 4, | |
nudge_y = 0, | |
show.legend = FALSE, | |
size = 3 | |
) + | |
theme_void(base_size = 15) + | |
theme( | |
legend.position = "top", | |
plot.title = element_text(hjust = 0.5, color = "#562357"), | |
plot.subtitle = element_text(hjust = 0.5, color = "#3b3b3b"), | |
panel.grid.minor = element_blank(), | |
panel.grid.major = element_blank(), | |
plot.title.position = "plot", | |
text = element_text(color = "#80868b"), | |
axis.text.x = element_blank() | |
) | |
ggplot2::ggsave( | |
plot = map_rladies, | |
filename = "~/Desktop/map_rladies.jpg", | |
dpi = 600 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment