Skip to content

Instantly share code, notes, and snippets.

@benmarwick
benmarwick / giur-zones-with-ggplot2.R
Last active June 18, 2023 02:58
Geometric Index of Unifacial Retouch Zone visualisation with ggplot2
library(ggplot2)
# coords for vertices of polygons that make the zones
polydata <- rbind(
data.frame(x = c(0, 0.01, 0.99, 1),
y = c(0, 0.25, 0.25, 0),
group = "zone 1",
fill = "grey80", xc = .5, yc = 0.125),
data.frame(x = c(0, 0.025, 0.495, 0.495),
y = c(-0.01, -0.25, -0.25, -0.01),
@benmarwick
benmarwick / Writing-session-productivity.R
Created July 12, 2018 17:13
Writing session productivity
library("googlesheets")
suppressPackageStartupMessages(library("dplyr"))
third_party_gap <- "https://docs.google.com/spreadsheets/d/1xbMp7UZ7Nusjb2u1b1RihZrNzPMINZ8BHy9x2urGTzo/edit#gid=0" %>%
gs_url()
wp <-
third_party_gap %>%
gs_read(ws = "Writing progress")
@benmarwick
benmarwick / gdoc-revisions-analysis.R
Last active July 16, 2024 13:24
How to get access to specific revisions (and details about it) of a google drive document? cf. https://github.com/tidyverse/googledrive/issues/218
# related to my question here: https://github.com/tidyverse/googledrive/issues/218
# How to get access to specific revisions of a google drive document? Yes, we can do that.
# How to get the username for each revision, and the size of the document at each revision (bytes or words, assuming we are specifically talking about docs and not sheets or other types of files) Yes, we can do that.
# Goal is to measure the contributions of each author to a collaboratively-authored google doc with as little work as possible
# How to get access to specific revisions of a google drive document?
# Getting the revisions for a Google Doc
@benmarwick
benmarwick / cluster-images-by-hand-drawn-outlines
Last active August 4, 2018 17:01
Cluster images traced by hand, using EFA and PCA, and plot
# to make traces...
# Import an EPS file in Inkscape
# Trace over it to show half of vessel profile, from top of rim to centre of base, about 20-30 segements
# detelete EPS from from Inkscape
# export PNG of trace from Inkscape
library(magick)
lf <- list.files("../traces", full.names = TRUE)
@benmarwick
benmarwick / cluster-images.R
Created May 19, 2018 03:54
Cluster images by similarity, and plot
# read in the images...
library(magick)
img_paths <- list.files("../outline_P/", full.names = TRUE )
n <- 100 # just get a sample
imgs <- image_read(img_paths[1:n])
# reduce size
imgs_cropped <- image_crop(imgs, "100x100")
@benmarwick
benmarwick / cp-analysis.R
Last active May 10, 2018 00:08
sketch for ffsg change point analysis
# Change point analysis
library(tidyverse)
# get the data of population per state and fatalities per state
source(here::here("/Analysis/Tables/permillcalculation.R"))
# compute fatalities per 1000 peple per year per state, using
# the custom function in permillcalculation.R
ratio_data <- permillcalc()
@benmarwick
benmarwick / simulate-Wright-Fisher-Model-of-selection-and-random-genetic-drift.R
Last active November 22, 2024 12:58
Simulates the Wright-Fisher Model of selection and random genetic drift
# data.frame to be filled
wf_df <- data.frame()
# effective population sizes
sizes <- c(5, 10, 100, 500)
# starting allele frequencies
starting_p <- c(.01, .1, .5, .8)
# number of generations
@benmarwick
benmarwick / gist:70f92dd61700abab1b590afa0040e3fa
Created April 27, 2018 22:17
using sf for points in polygon spatial join
library(sf)
library(tidyverse)
# read in the shapefile first, that gives us the CRS for the analysis
polygons <- st_read("polygons.shp")
# read in the points
points <- read_csv('points.csv')
@benmarwick
benmarwick / cartogram-us-states.R
Created April 16, 2018 07:32
Plot a cartogram for the USA using crime data by joining a plain data frame to a SpatialPolygonsDataFrame
require(maps)
require(sp)
require(maptools)
require(tmap)
require(cartogram)
# get a SpatialPolygonsDataFrame of US states
usa <- map("state", fill = TRUE)
IDs <- sapply(strsplit(usa$names, ":"), function(x) x[1])
usa <- map2SpatialPolygons(usa, IDs=IDs, proj4string=CRS("+proj=longlat +datum=WGS84"))
@benmarwick
benmarwick / munsell-plot.R
Last active March 16, 2019 00:28
Make a stratigraphic-style plot-schematic of sediment colour using munsell values
# read in data
soil_colour <-
readxl::read_excel("KWL_chronology.xlsx",
sheet = "soil_color")
layer_depths <-
readxl::read_excel("KWL_chronology.xlsx",
sheet = "depth") # some of the Pit IDs don't match the Pit IDs in the soil_colour sheet...
library(tidyverse)