Created
January 20, 2019 03:48
-
-
Save Ryo-N7/79712aba28b7fc26d2c0eff388fce8ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# load packages | |
pacman::p_load(tidyverse, scales, ggbeeswarm, extrafont, glue, magick) | |
# load fonts | |
loadfonts() | |
# load data | |
launches_raw <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-15/launches.csv") | |
# wrangle data | |
launches_jp_type <- launches_raw %>% | |
filter(state_code == "J") %>% | |
mutate(type = as_factor(type), | |
type = fct_reorder(.f = type, .x = launch_date, | |
.fun = min)) %>% | |
mutate(success = if_else(category == "O", "Success", "Failure") %>% as_factor()) | |
# set color values | |
cols <- c("Success" = "grey", "Failure" = "red") | |
# PLOT | |
launches_jp_type %>% | |
ggplot(aes(launch_year, fct_rev(type), color = success)) + | |
geom_beeswarm(groupOnX = FALSE, cex = 1.5) + | |
scale_color_manual(values = cols, name = "Launch Result") + | |
scale_x_continuous(breaks = scales::pretty_breaks(20)) + | |
geom_rect(xmin = 1965, xmax = 2020, fill = NA, color = "black", | |
ymin = 3.4, ymax = 7.825, alpha = 0.2) + | |
labs(title = "History of Japan's Space Vehicles", | |
subtitle = glue(" | |
The H-IIA (operated by Mitsubishi Heavy Industries) has been a reliable vehicle for the | |
Japan Aerospace Exploration Agency (JAXA) throughout this century."), | |
x = "Year", | |
y = "Launch Vehicle Type", | |
caption = " | |
Source: FiveThirtyEight | |
By @R_by_Ryo") + | |
theme_minimal() + | |
theme(panel.grid.minor.x = element_blank(), | |
plot.title = element_text(size = 14), | |
plot.subtitle = element_text(size = 10), | |
text = element_text(family = "Roboto Condensed"), | |
legend.position = "bottom") + | |
annotate(geom = "text", | |
label = glue(" | |
Only 1 of 40 launches of this type have failed."), | |
x = 1967, y = 5.5, hjust = 0, family = "Roboto Condensed") | |
# save plot | |
ggsave(glue("../2019_week_3/jp_space_vehicles_plot.png")) | |
# add logo with Magick using Thomas Mock's custom function | |
# check out the explanation in his blog post: https://themockup.netlify.com/posts/2019-01-09-add-a-logo-to-your-plot/ | |
add_logo <- function(plot_path, logo_path, logo_position, logo_scale = 10){ | |
# Requires magick R Package https://github.com/ropensci/magick | |
# Useful error message for logo position | |
if (!logo_position %in% c("top right", "top left", "bottom right", "bottom left")) { | |
stop("Error Message: Uh oh! Logo Position not recognized\n Try: logo_positon = 'top left', 'top right', 'bottom left', or 'bottom right'") | |
} | |
# read in raw images | |
plot <- magick::image_read(plot_path) | |
logo_raw <- magick::image_read(logo_path) | |
# get dimensions of plot for scaling | |
plot_height <- magick::image_info(plot)$height | |
plot_width <- magick::image_info(plot)$width | |
# default scale to 1/10th width of plot | |
# Can change with logo_scale | |
logo <- magick::image_scale(logo_raw, as.character(plot_width/logo_scale)) | |
# Get width of logo | |
logo_width <- magick::image_info(logo)$width | |
logo_height <- magick::image_info(logo)$height | |
# Set position of logo | |
# Position starts at 0,0 at top left | |
# Using 0.01 for 1% - aesthetic padding | |
if (logo_position == "top right") { | |
x_pos = plot_width - logo_width - 0.01 * plot_width | |
y_pos = 0.01 * plot_height | |
} else if (logo_position == "top left") { | |
x_pos = 0.01 * plot_width | |
y_pos = 0.01 * plot_height | |
} else if (logo_position == "bottom right") { | |
x_pos = plot_width - logo_width - 0.01 * plot_width | |
y_pos = plot_height - logo_height - 0.01 * plot_height | |
} else if (logo_position == "bottom left") { | |
x_pos = 0.01 * plot_width | |
y_pos = plot_height - logo_height - 0.01 * plot_height | |
} | |
# Compose the actual overlay | |
magick::image_composite(plot, logo, offset = paste0("+", x_pos, "+", y_pos)) | |
} | |
# add_logo and save | |
plot_logo <- add_logo(plot_path = "../2019_week_3/jp_space_vehicles_plot.png", | |
logo_path = "https://upload.wikimedia.org/wikipedia/commons/8/85/Jaxa_logo.svg", | |
logo_position = "top right", | |
logo_scale = 10) | |
image_write(image = plot_logo, "../2019_week_3/jp_vehicles_logo_plot.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment