Skip to content

Instantly share code, notes, and snippets.

@juliasilge
Created March 10, 2022 00:09
Show Gist options
  • Save juliasilge/050a501b5fcefe194e5d8dfd83f94194 to your computer and use it in GitHub Desktop.
Save juliasilge/050a501b5fcefe194e5d8dfd83f94194 to your computer and use it in GitHub Desktop.
Network graph for #TidyTuesday ERASMUS student mobility
library(tidyverse)
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:dplyr':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:purrr':
#> 
#>     compose, simplify
#> The following object is masked from 'package:tidyr':
#> 
#>     crossing
#> The following object is masked from 'package:tibble':
#> 
#>     as_data_frame
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(ggraph)
theme_set(silgelib::theme_roboto())

url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-03-08/erasmus.csv"
erasmus <- read_csv(url)
#> Rows: 164635 Columns: 24
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (21): project_reference, academic_year, mobility_start_month, mobility_e...
#> dbl  (3): mobility_duration, participant_age, participants
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

student_graph <-
    erasmus %>%
    filter(mobility_duration >= 7)  %>%
    count(sending_country_code, receiving_country_code, wt = participants, name = "students") %>%
    filter(sending_country_code != receiving_country_code) %>%
    mutate(across(contains("country_code"), countrycode::countrycode,
                  origin = "eurostat", destination = "country.name")) %>%
    filter(students > 10) %>%
    graph_from_data_frame(directed = FALSE)


student_graph %>%
    ggraph(layout = "linear", circular = TRUE) +
    geom_edge_arc(aes(edge_alpha = students, edge_width = students), color = "midnightblue") +
    geom_node_point(size = 5) +
    geom_node_text(aes(label = name), repel = TRUE, family = "RobotoCondensed-Regular")

Created on 2022-03-09 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment