Skip to content

Instantly share code, notes, and snippets.

🎯
systemfonts

Thomas Lin Pedersen thomasp85

🎯
systemfonts
Block or report user

Report or block thomasp85

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@thomasp85
thomasp85 / styling.R
Created Feb 26, 2019
Single-quote styler
View styling.R
transformers <- styler::tidyverse_style()
transformers$token$fix_quotes <- function (pd_flat) {
str_const <- pd_flat$token == 'STR_CONST'
str_const_change <- grepl('^"(.*)"$', pd_flat$text[str_const])
raw_str <- sub('^"(.*)"$', '\\1', (pd_flat$text[str_const][str_const_change]))
raw_str <- gsub('(\\\\)?\'', '\\\\\'', raw_str)
raw_str <- gsub('\\\\"', '"', raw_str)
pd_flat$text[str_const][str_const_change] <- paste0('\'', raw_str, '\'')
pd_flat
}
@thomasp85
thomasp85 / gganimate_cookbook.rmd
Created Jan 29, 2019
Code for gganimate talk RStudio::conf 2019
View gganimate_cookbook.rmd
---
title: "gganimate cookbook code"
output:
html_document:
df_print: tibble
highlight: kate
css: gganimate_cookbook.css
---
```{r setup}
@thomasp85
thomasp85 / pdf_animation.rmd
Created Oct 11, 2018
An example of embedding a gganimate animation in a pdf
View pdf_animation.rmd
---
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 / airq.R
Last active Jan 10, 2019
air quality
View airq.R
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) +
View yinyang.R
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 Jul 15, 2018
An algorithm for closing erroneous polygons in sf
View st_close.R
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) {
View sf_transitions.R
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')
View Denmark_to_Australia.R
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 Jan 10, 2019
Animating Elephant Seal movement
View seal_tracking.R
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 Jun 6, 2018
Gapminder example with gganimate
View gapminder.r
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) +
You can’t perform that action at this time.