Skip to content

Instantly share code, notes, and snippets.

@ryantimpe
Last active November 25, 2018 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantimpe/1b4434d19bf1ab938609882eb63f6c39 to your computer and use it in GitHub Desktop.
Save ryantimpe/1b4434d19bf1ab938609882eb63f6c39 to your computer and use it in GitHub Desktop.
Plot phylopic images to compare raptor size in Jurassic Park to reality
library(tidyverse); library(rvest);
library(png)
#Phylopic images here:
https://github.com/ryantimpe/ChartOfTheDay/tree/master/data
#Text wrapping function ---
wrapper <- function(x, ...) {paste(strwrap(x, ...), collapse = "\n")}
#Scale image into feet
process_png_y <- function(pic, feet=6){
dat <- pic[,,4] %>%
as.data.frame() %>%
mutate(y = n()-row_number()) %>%
gather(x, value, 1:(ncol(.)-1)) %>%
mutate(x = as.numeric(str_remove_all(x, "V"))) %>%
filter(value > 0)
xscale <- (max(dat$y)/max(dat$x))^(-1)
dat2 <- dat %>%
#Rescale to 1ft == 50px
mutate(ys = floor(y/max(y)*feet*50), xs = floor(x/max(x)*feet*50*xscale)) %>%
group_by(x = xs, y = ys) %>%
summarize(value = max(value)) %>%
ungroup()
return(dat2)
}
fact <- "Actual velociraptors were the size of turkeys"
fb1 <- tribble(
~Source, ~Animal, ~Height,
"Human size", "Homo", 6,
"Jurassic Park", "Velociraptor", 4.75,
"Turkey-size", "Meleagris", 2.5,
"Turkey-size", "Velociraptor", 2
) %>%
mutate(Pic = purrr::map2(Animal, Height, function(ani, hght){
readPNG(paste0("data/", ani, ".png")) %>%
process_png_y(hght)
})) %>%
unnest(Pic) %>%
group_by(Source, Animal) %>%
mutate(x = ifelse(Animal == "Velociraptor", -x, x),
x = ifelse(Source == "Turkey-size", x + floor(median(x)/4), x)) %>%
ungroup()
# Universals ----
chart_footer_bonus <- paste0("Turkey Triva
@ Ryan Timpe .com")
#Colors
trk.col.orange <- c("#ff6141")
trk.col.red <- c("#8B0000", "#ff4040")
trk.col.gold <- c("#D4AF37", "#CFB53B", "#C5B358")
trk.col.blue <- c("#00436b")
trk.col.ltblue <- c("#5384ff")
trk.col.bkgrnd <- c("#fcedcc")
#Chart theme
trk_chart_theme <- theme(
panel.grid = element_blank(),
strip.background = element_rect(fill = "#00436b"),
strip.text = element_text(color = "white", face = "bold"),
plot.background = element_rect(fill = "#fcedcc"),
plot.title = element_text(size = 14, face = "bold"),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8),
panel.background = element_rect(fill = "#fcedcc"),
legend.position = "bottom"
)
fb1 %>%
ggplot(aes(x=x, y=y, fill = Source)) +
geom_raster() +
scale_fill_manual(values = c("Human size" = trk.col.blue, "Turkey-size" = trk.col.gold[2],
"Jurassic Park" = trk.col.red[1])) +
scale_y_continuous(name = "Height (Ft)", breaks = seq(50, 300, by=50), labels = 1:6) +
coord_fixed() +
labs(title = fact,
subtitle = wrapper("Hollywood took some liberties with human-sized velociraptors in Jurassic Park...
and they plucked off the feathers.", 85),
caption = paste0("Images: phylopic.org\n", chart_footer_bonus)) +
theme_minimal() +
trk_chart_theme +
theme(
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 12, face = 'bold'),
axis.title.y = element_text(size = 14, face = "bold")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment