Skip to content

Instantly share code, notes, and snippets.

View thomasp85's full-sized avatar
🎯
Something new and exciting

Thomas Lin Pedersen thomasp85

🎯
Something new and exciting
View GitHub Profile
@thomasp85
thomasp85 / Denmark_to_Australia.R
Created June 11, 2018 11:23
Preview of view_zoom
library(ggplot2)
library(gganimate)
library(sf)
earth <- sf::st_as_sf(rnaturalearth::countries110)
views <- data.frame(rbind(
st_bbox(earth[earth$name == 'Denmark',]),
st_bbox(earth[earth$name == 'Australia',])
))
p <- ggplot() +
geom_sf(data = earth, fill = 'white') +
@thomasp85
thomasp85 / seal_tracking.R
Last active December 9, 2020 18:23
Animating Elephant Seal movement
library(sf)
library(ggplot2)
library(gganimate)
# Data from personal correspondance
# Collapse all dates to the same year
d$year <- format(d$date, '%Y')
d$stand_time <- as.POSIXct(paste0('2000-', format(d$date, '%m-%d %T')))
@thomasp85
thomasp85 / gapminder.r
Created June 6, 2018 19:05
Gapminder example with gganimate
library(gapminder)
library(ggplot2)
library(gganimate)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
@thomasp85
thomasp85 / script.R
Last active October 13, 2021 01:33
Temporal Network Viz
library(ggraph)
library(tidygraph)
library(gganimate)
# Data from http://konect.uni-koblenz.de/networks/sociopatterns-infectious
infect <- read.table('out.sociopatterns-infectious', skip = 2, sep = ' ', stringsAsFactors = FALSE)
infect$V3 <- NULL
names(infect) <- c('from', 'to', 'time')
infect$time <- as.POSIXct(infect$time, origin = Sys.time() - as.numeric(Sys.time()))
library(magick)
library(reshape2)
library(dplyr)
library(tidygraph)
library(particles)
library(animation)
plot_fun <- function(sim) {
df <- as_tibble(sim)
plot(df$x, df$y, col = df$color, pch = '.', axes = FALSE, xlim = c(-100, 317), ylim = c(-268, 100), xlab = NA, ylab = NA)
@thomasp85
thomasp85 / particles.R
Created February 26, 2018 19:33
Particles on CRAN
library(tidygraph)
library(particles)
library(jsonlite)
library(magick)
# Prepare text polygons
text <- read_json('text.json')
par_text <- text$layers[[3]]$paths
on_text <- text$layers[[2]]$paths
cran_text <- text$layers[[1]]$paths
@thomasp85
thomasp85 / ggplot_vs_patchwork.rmd
Created December 17, 2017 21:38
An R Notebook showing the difference in plotting ggplots and ggassemblies
---
title: "R Notebook"
output: html_notebook
---
# Difference in behavior between ggplot2 and patchwork plots in Notebooks
Consider the following ggplot
```{r}
@thomasp85
thomasp85 / trim_model.R
Created October 24, 2017 07:26
Trim all unnecessary data from model objects
library(future)
trim_model <- function(model, predictor = predict, ..., ignore_warnings = TRUE) {
# Cache the correct output
true_pred <- predictor(model, ...)
# Treat prediction warnings as errors?
if (!ignore_warnings) {
old_ops <- options(warn = 2)
on.exit(options(old_ops))
}

Keybase proof

I hereby claim:

  • I am thomasp85 on github.
  • I am thomasp85 (https://keybase.io/thomasp85) on keybase.
  • I have a public key ASDRGUror8ScAXuoC-MJVs06sOwPsdpyP9RU3GrLfkKU9Qo

To claim this, I am signing this object:

@thomasp85
thomasp85 / closed_spline_anim.r
Created September 1, 2017 13:19
Animate through several closed b-splines
library(ggforce) # need github version
library(tweenr)
library(animation)
# Define the different states
controls <- list(
data.frame(
x = runif(6),
y = runif(6),
col = 'steelblue',