Skip to content

Instantly share code, notes, and snippets.

@tukachev
Created April 11, 2024 20:01
Show Gist options
  • Save tukachev/853704ae7f8d43b4220dc90143b90f4b to your computer and use it in GitHub Desktop.
Save tukachev/853704ae7f8d43b4220dc90143b90f4b to your computer and use it in GitHub Desktop.
library(httr)
library(jsonlite)
library(dplyr)
library(ggplot2)
# Download full database
astronaut_db_url <-
'https://supercluster-iadb.s3.us-east-2.amazonaws.com/adb.json'
astronauts_db <-
jsonlite::fromJSON(content(GET(astronaut_db_url), "text"))
# Save database to file
writeLines(jsonlite::toJSON(astronauts_db, auto_unbox = TRUE),
'astro_db.json')
# Loading Data
astronauts_db <- jsonlite::fromJSON('astro_db.json')
# Extracting Time in Space
s_space_time <- astronauts_db$astronauts %>%
filter(!is.na(totalMinutesInSpace)) %>%
mutate(totalDaysInSpace = totalMinutesInSpace / (60 * 24)) %>%
select(name, totalDaysInSpace) %>%
arrange(totalDaysInSpace) %>%
tibble()
#Filter out zero values
s_space_time_filtered <-
s_space_time[s_space_time$totalDaysInSpace > 0,]
total_astronauts <- nrow(s_space_time_filtered)
# subtitle
summary_data <- summary(s_space_time_filtered$totalDaysInSpace)
percentage <- round(summary_data[3] / total_astronauts * 100, 2)
median_days <- round(summary_data[3], 1)
subtitle <- stringr::str_wrap(
paste0(
"Out of ",
total_astronauts,
" astronauts, ",
total_astronauts / 2,
" individuals, or 50 %,",
" spent ",
median_days,
" days or less in space, which is the median duration of space missions."
),
55
)
# Visualization
ggplot(s_space_time_filtered, aes(x = totalDaysInSpace)) +
geom_histogram(fill = 'red',
color = "white",
binwidth = 0.1) +
scale_x_continuous(
trans = 'log10',
breaks = c(0.01, 0.1, 1, 10, 25, 100, 1000),
labels = c("1e-02", "1e-01", "1", "10", "25", "100", "1000")
) +
scale_y_continuous(
breaks = seq(0, 60, 10),
expand = c(0, 0),
limits = c(0, 65)
) +
labs(
x = 'Days in Space (log scale)',
y = 'Number of Astronauts',
title = 'Time Spent in Space',
subtitle = subtitle,
caption = "Author: ChatGPT\nSource: Astronaut Database - Supercluster.com"
) +
theme(
text = element_text(family = "PT Sans", size = 18),
plot.title = element_text(color = 'white', size = 22),
plot.subtitle = element_text(color = 'white', size = 16),
plot.caption = element_text(color = 'white', size = 12),
axis.text = element_text(color = 'white'),
axis.title = element_text(color = 'white'),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color = 'white', size = 0.5),
panel.background = element_rect(fill = 'black'),
plot.background = element_rect(fill = 'black')
)
ggsave(
"time_spent_in_space.png",
width = 6,
height = 6,
dpi = 300,
scale = 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment