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 / 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()))
@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 / 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 / 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') +
library(gganimate) # thomasp85/gganimate
library(cartogram)
library(geogrid) # Need github version jbaileyh/geogrid
library(rnaturalearth)
library(sf)
library(scico)
us <- ne_states('united states of america', returnclass = 'sf')
us <- us[!us$woe_name %in% c('Alaska', 'Hawaii'), ]
us <- st_transform(us, '+proj=eqdc +lat_0=39 +lon_0=-96 +lat_1=33 +lat_2=45 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs')
@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) {
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 / 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) +
@thomasp85
thomasp85 / pdf_animation.rmd
Created October 11, 2018 19:40
An example of embedding a gganimate animation in a pdf
---
title: "PDF Animation Test"
output: pdf_document
header-includes:
- \usepackage{animate}
---
`gganimate` now supports animations inside PDF documents. This feature is only
viewable with Acrobat Reader, however. Remember to include
`\usepackage{animate}` in the preamble and set `fig.show='animate'` in the chunk
@thomasp85
thomasp85 / gganimate_cookbook.rmd
Created January 29, 2019 11:58
Code for gganimate talk RStudio::conf 2019
---
title: "gganimate cookbook code"
output:
html_document:
df_print: tibble
highlight: kate
css: gganimate_cookbook.css
---
```{r setup}