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 / animate.R
Created February 3, 2016 19:43
Animating graph over time
library(ggraph)
library(gganimate)
library(igraph)
# 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$timebins <- as.numeric(cut(infect$time, breaks = 100))
# We want that nice fading effect so we need to add extra data for the trailing
@thomasp85
thomasp85 / onetime.md
Created March 29, 2016 13:19
Verifying that +thomasp85 is my blockchain ID. https://onename.com/thomasp85
#devtools::install_github('thomasp85/ggforce')
#devtools::install_github("dgrtwo/gganimate")
#devtools::install_github('thomasp85/ggraph')
#devtools::install_github("hadley/ggplot2")
library(ggraph)
library(ggforce)
library(gganimate)
library(ggplot2)
library(igraph)
@thomasp85
thomasp85 / Histogram_animation.R
Last active December 17, 2021 11:20
An example of animating the build up of a histogram with dropping balls using tweenr, gganimate and ggplot2
library(tweenr) # Available on CRAN
library(ggforce) # Install from thomasp85/ggforce
library(gganimate) # Install from dgrtwo/gganimate
set.seed(2)
x <- sample(9,20, prob=c(1,2,3,4,5,4,3,2,1), replace=T)
df <- data.frame(x = x, y = 15)
dfs <- list(df)
for(i in seq_len(nrow(df))) {
dftemp <- tail(dfs, 1)
dftemp[[1]]$y[i] <- sum(dftemp[[1]]$x[seq_len(i)] == dftemp[[1]]$x[i])
@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',

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 / 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))
}
@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 / 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
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)