Skip to content

Instantly share code, notes, and snippets.

@benmarwick
benmarwick / rotate-axis-labels-ggplot2.R
Last active March 30, 2024 08:00
I can never remember how to rotate the x-axis labels with ggplot2: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Adapted from https://stackoverflow.com/a/7267364/1036500 by Andrie de Vries
# This is it: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
library(ggplot2)
td <- expand.grid(
hjust=c(0, 0.5, 1),
vjust=c(0, 0.5, 1),
angle=c(0, 45, 90),
@benmarwick
benmarwick / dictionary_lookup.R
Created March 2, 2018 23:45
Use a character vector to count occurrences per row, in a data frame with rows of text, like a dictionary lookup, but with a very specific custom dictionary
library(usdanutrients)
library(tidyverse)
foods_simple <-
food %>%
separate(food, into = str_glue('V{1:11}'), sep = ",") %>%
distinct(V1) %>%
mutate(V1 = tolower(V1)) %>%
mutate(V1 = str_replace_all(V1, "[[:punct:]]", "")) %>%
rename(food = V1)
@benmarwick
benmarwick / c14_calibration.R
Created March 1, 2018 23:23
Simple calibration of radiocarbon ages using BChron
csv <-c('"Sample","Lab_Code","Age","One_sd"
"GM-C14-14","OxA-34804",12665,55
"GM-C14-16","OxA-34805",9240,40
"GM-C14-26","OxA-34806",2829,28')
c14_data <- read.csv(text = csv, header = TRUE)
names(c14_data)
# read in the filie
library(googlesheets)
library(readxl)
library(httr)
#-------------------------------------------------------------
# mapping police violence, https://mappingpoliceviolence.org/
# get google sheet info
mapping_police_violence <- gs_url("https://docs.google.com/spreadsheets/d/1dKmaV_JiWcG8XBoRgP8b4e9Eopkpgt7FL7nyspvzAsE/edit#gid=0")
@benmarwick
benmarwick / analyse_large_text_file_chunked.R
Last active April 5, 2018 10:57
Use R to analyse a large text file that is too big to read in all at once
library(chunked)
library(tidyverse)
# I want to look at the daily page views of Wikipedia articles
# before 2015... I can get zipped log files
# from here: https://dumps.wikimedia.org/other/pagecounts-ez/merged/2012/2012-12/
# I get bz file, unzip to get this:
my_file <- 'pagecounts-2012-12-14/pagecounts-2012-12-14'
# function to do a dodged half-boxplot and jittered points next to each other
#
# data_in should be a data frame
# factor_col should be a bare column name (not a string)
# although it will work if that column is factor or a character type
# numeric_col is the y axis continuous variable
# offset is the width of the boxplots and jittered point cloud
#
# the basic approach is to draw a boxplot without the tails
# (e.g. only the interquartile range) and then use segments to add the
## GOAL:
## re-create a figure similar to Fig. 2 in Wilson et al. (2018),
## Nature 554: 183-188. Available from:
## https://www.nature.com/articles/nature25479#s1
##
## combines a boxplot (or violin) with the raw data, by splitting each
## category location in two (box on left, raw data on right)
## call required packages
@benmarwick
benmarwick / korean-aster-raster-data.R
Last active February 15, 2018 09:12
Get various climate and terrain data files for Korea, do some things in R
# we have downloaded the files from NASA Land Processes Distributed Active
# Archive Center (LP DAAC) at https://earthexplorer.usgs.gov/
# and unzipped the downloaded file
# which contains many zip files...
# read in the Zipped files
setwd("C:/Users/bmarwick/Downloads/030432578418876/DEM-ASTER-Korea")
library(fs)
zip_files <- dir_ls(regexp = "[.]zip$")
# data from http://reportcard.ospi.k12.wa.us/DataDownload.aspx
school_data <- readxl::read_excel("2_03_AIM-EOC-MSP-SBA Assessments School (with suppression - new format).xlsx")
# Level 4 is Advanced (exceeding state standard): http://www.k12.wa.us/assessment/StateTesting/FAQ.aspx
library(tidyverse)
# what does the data look like?
glimpse(school_data)
@benmarwick
benmarwick / face_detection_api.R
Last active February 10, 2018 06:25
get data from 'The Officer Down Memorial Page'
#------
# for tutorials see:
# - https://bigdataenthusiast.wordpress.com/tag/microsoft-cognitive-services-using-r/
# - https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
# try facial analysis for sex and gender
# scrape each page for each year
img_srcs <-
map(odmp_urls,