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 / 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 / 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
upper <- seq(0, pi, length.out = 181)[-181]
upper <-cbind(x = cos(upper), y = sin(upper))
lower <- seq(pi, 2*pi, length.out = 181)[-181]
lower <- cbind(x = cos(lower), y = sin(lower))
right <- seq(1.5*pi, 2.5*pi, length.out = 181)[-181]
right <- cbind(x = cos(right), y = sin(right))
left <- seq(0.5*pi, 1.5*pi, length.out = 181)[-181]
left <- cbind(x = cos(left), y = sin(left))
full <- cbind(x = cos(seq(0, 2*pi, length.out = 361)[-361]),
y = sin(seq(0, 2*pi, length.out = 361)[-361]))
@thomasp85
thomasp85 / st_close.R
Last active July 15, 2018 01:34
An algorithm for closing erroneous polygons in sf
st_close <- function(x) {
UseMethod('st_close')
}
st_close.sfg <- function(x) x
st_close.POLYGON <- function(x) {
if (st_is_empty(x)) return(x)
x[] <- lapply(x[], close_mat)
x[vapply(x[], nrow, integer(1)) > 3]
}
st_close.MULTIPOLYGON <- function(x) {
@thomasp85
thomasp85 / airq.R
Last active January 10, 2019 09:22
air quality
library(gganimate)
airq <- airquality
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]
ggplot(airq, aes(Day, Temp, group = Month)) +
geom_line() +
geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') +
geom_point(size = 2) +
geom_text(aes(x = 31.1, label = Month), hjust = 0) +