Skip to content

Instantly share code, notes, and snippets.

@benmarwick
benmarwick / gist:cffb82fb6d9192354d495a2e0ded4e55
Last active September 13, 2019 00:25
Plot the population of Han Chinese in Taiwan from Wikipedia
library(tidyverse)
pop_han_in_tw <- tribble(
~year, ~population,
1684, 120000,
1764, 666210,
1782, 912920,
1811, 1944737,
@benmarwick
benmarwick / inspect-rmd-diff-from-last-commit.R
Last active February 22, 2023 20:43
How to compare the current commit of an RMarkdown file with the most recent commit. Useful when we merge a pull request and want to check again how it has changed the document
# reviewer pkg comes from https://ropenscilabs.github.io/reviewer/index.html
# remotes::install_github("ropenscilabs/reviewer")
browseURL(reviewer::diff_rmd("path/to/my-document.Rmd",
# this gets the sha of the previous commit
git2r::commits(n=2)[[2]]$sha)$raw)
# result will open in a web browser
@benmarwick
benmarwick / gist:82776e452d61cfbd44972ae6a4c23c21
Last active July 28, 2019 01:24
Stacked, overlapping time series plots on a common y-axis with ggplot2
# starting with code from
# https://gist.github.com/tomhopper/faa24797bb44addeba79?fbclid=IwAR3FFiHpUhTESqi_ylASZP5-zYD0lOrQyyHM2MSEvnVRCIykmjNREGTv4Uk
library(ggplot2)
library(grid)
library(dplyr)
#' Create some data to plot
n <- 100
df <- data.frame(DateTime = seq(10, 20, length.out = 100),
library(tidyverse)
library(gh)
.token = "" # get one from https://github.com/settings/tokens
# get all pkgs on GitHub mirror of CRAN
n <- 5000 # 16125 # https://github.com/cran
cran_repos <- gh("/users/:username/repos",
username = "cran",
@benmarwick
benmarwick / prentiss-1998-pca.R
Last active August 13, 2019 00:51
Exploring the PCA published by Prentiss (1998) to understand the usefulness of the Sullivan and Rozen debitage typology
#------------------------------------------------------
# Exploring the PCA published by Prentiss (1998) to understand the
# usefulness of the Sullivan and Rozen debitage typology
# read in & tidy the data -----------------------------------------------
library(tidyverse)
# got these data from table 7 (p. 644) of https://www.jstor.org/stable/2694112
# OCR'd using https://tabula.technology/
prentiss <- readr::read_csv("tabula-Prentiss 1988.csv")
@benmarwick
benmarwick / app.R
Last active August 19, 2020 09:17
Shiny app to get outlines of an object in an image using Canny edge detection
library("shiny")
library("EBImage") # >= 4.19.3
library(imager)
ui <- fluidPage(# Application title
titlePanel("Image outline, chords, and landmarks"),
@benmarwick
benmarwick / gist:8cf22ecb74ac511f8ac1c70aef6038a7
Last active April 29, 2023 21:31
How to make diamond plots after Bergstrom and West (2018) "Why scatter plots suggest causality, and what we can do about it"
# https://arxiv.org/pdf/1809.09328.pdf & https://twitter.com/CT_Bergstrom/status/1035327464644333568
# they use Mathematica, boo! So let's make them with R
# starting with https://stackoverflow.com/q/33396168/1036500
library(ggplot2)
p <- ggplot() +
geom_point(data = anscombe,
@benmarwick
benmarwick / tidy-multiple-summaries.R
Last active July 1, 2019 14:25
Tidily summarizing multiple metric attributes of artefacts
# Summarising multiple metric attributes of artefacts
p1 <- read_excel("data/riumailuoi.xlsx", sheet = 'phase1')
summary_stats_table <- function(x) {
x %>%
select(Mass,
Length,
Width,
Thickness,
# list of lists
url <- "https://en.wikipedia.org/wiki/Lists_of_World_Heritage_Sites"
# Table of sites per country can be found at each of these pages
# probably the simplest entry point
# Africa
africa <- "https://en.wikipedia.org/wiki/List_of_World_Heritage_Sites_in_Africa"
# Americas
@benmarwick
benmarwick / point_dist_to_polygon.Rmd
Created February 16, 2019 10:45
Two methods to get distance from point to enclosing polygon boundary
---
title: 'C14 analysis: Coastal vs Inland'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Import and clean the data: