Skip to content

Instantly share code, notes, and snippets.

@phaya
Created July 17, 2020 09:15
Show Gist options
  • Save phaya/a17ba07d401a77c907d77d53a4d58db6 to your computer and use it in GitHub Desktop.
Save phaya/a17ba07d401a77c907d77d53a4d58db6 to your computer and use it in GitHub Desktop.
1964-2019 network of astronauts who share space missions
library(tidyverse)
library(igraph)
# Get the Data
space_missions <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-14/astronauts.csv')
# Data bug fix
space_missions <-
space_missions %>%
mutate(total_number_of_missions=ifelse(id == 1091,2,total_number_of_missions)) %>%
mutate(nationwide_number=ifelse(id == 1271,344,nationwide_number)) %>%
mutate(astro_id=as.numeric(paste0(number,nationwide_number,total_hrs_sum)))
# Astronauts data base
astronauts <-
space_missions %>%
select(id=astro_id, fullname=name, sex, year_of_birth, nationality, military_civilian,
total_number_of_missions, total_hrs_sum, total_eva_hrs) %>%
distinct()
# create astronauts graph
edge_list <-
inner_join(space_missions, space_missions, by="mission_title") %>%
filter(astro_id.x != astro_id.y) %>%
select(astro_id.x, astro_id.y, year_of_mission.x) %>%
rename(to=astro_id.x, from=astro_id.y, year_of_mission=year_of_mission.x)
g <- graph_from_data_frame(edge_list, vertices=astronauts, directed = FALSE)
g <- simplify(g, edge.attr.comb="first")
# create color
V(g)$nat <- astronauts$nationality %>% fct_lump_n(2, other_level = "Other")
V(g)$color <-
case_when(
V(g)$nat == "1" ~ "blue",
V(g)$nat == "2" ~ "red",
V(g)$nationality == "Spain" ~ "black",
TRUE ~ "gray"
)
#generate layout
layout <- norm_coords(layout.fruchterman.reingold(g), xmin = -1, xmax = 1, ymin = -1, ymax = 1)
#save graph
png(file="1964-2019.png", width=1600, height=1200)
plot(g,layout=layout,
vertex.label="", vertex.size=2,
vertex.frame.color=V(g)$color, edge.width=1.5,
main="1964-2019")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment