Skip to content

Instantly share code, notes, and snippets.

@noamross
noamross / mgcv-posterior-animate.R
Created September 2, 2018 00:29
Animating smoothing uncertainty in a GAM
library(tidyverse)
library(gganimate)
library(mgcv)
library(mvtnorm)
# Fit a GAM to the data
mod <- gam(hp ~ s(mpg), data=mtcars, method="REML")
# Get the linear prediction matrix
newdat = data.frame(
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')
@rushgeo
rushgeo / minerva_batchtools_template.sh
Created June 14, 2018 15:37
Template single script for submitting a master R job to Minerva (LSF-based) cluster, which then submits smaller jobs
#!/bin/sh
# Simplified template based on a script from Jonathan Heiss
# create batchtools.lsf.tmpl in the working directory - this template used for child jobs
cat <<'TEMPLATE' > batchtools.lsf.tmpl
## Default resources can be set in your .batchtools.conf.R by defining the variable
## 'default.resources' as a named list.
#BSUB-J <%= job.name %> # Name of the job
#BSUB-o <%= log.file %> # Output is sent to logfile, stdout + stderr by default
@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') +
@halhen
halhen / elevation.R
Last active November 4, 2020 16:11
# Download elevation tif from eg http://www.eea.europa.eu/data-and-maps/data/digital-elevation-model-of-europe
# First, convert elevation tif to a space delimited xyz (lng lat elevation) file
# $ gdal_translate -of XYZ elevation3x3.tif /tmp/file.xyz
df <- read_delim('/tmp/file.xyz', delim=' ', col_names=FALSE)
df %>%
mutate(X3 = na_if(X3, 0)) %>%
ggplot(aes(X1, -X2 + 20 * X3/max(X3, na.rm=TRUE), group=X2)) +
geom_line(size=0.05) +
@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])
@rafapereirabr
rafapereirabr / stacked_map_R_ggplot2.md
Last active March 29, 2023 18:41
Creating a stacked map in R using ggplot2

This gist shows in two steps how to tilt and stack maps using ggplot2 in order to create an image like this one: [![enter image description here][1]][1]

Let's load the necessary libraries and data to use a reproducible example:

# load libraries
  library(rgeos)
  library(UScensus2000tract)
  library(ggplot2)
@fitnr
fitnr / county-epsg.csv
Last active February 20, 2025 23:24
List of State Plane coordinate systems and their various ID codes.
COUNTYFIPS NAME STATEPLANEFIPS EPSG
16079 Shoshone County 1103 26970
16073 Owyhee County 1103 26970
16071 Oneida County 1101 26968
16077 Power County 1101 26968
16075 Payette County 1103 26970
06115 Yuba County 0402 26942
06111 Ventura County 0405 26945
06113 Yolo County 0402 26942
31177 Washington County 2600 32104
@hadley
hadley / curriculum.md
Created September 27, 2013 20:24
My first stab at a basic R programming curriculum. I think teaching just these topics without overall motivating examples would be extremely boring, but if you're a self-taught R user, this might be useful to help spot your gaps.

Notes:

  • I've tried to break up in to separate pieces, but it's not always possible: e.g. knowledge of data structures and subsetting are tidy intertwined.

  • Level of Bloom's taxonomy listed in square brackets, e.g. http://bit.ly/15gqPEx. Few categories currently assess components higher in the taxonomy.

Programming R curriculum

Data structures

@brentp
brentp / exp.md
Last active August 20, 2018 13:09
sketch power calculation