Skip to content

Instantly share code, notes, and snippets.

View plotting-archaeology-papers-with-R-code.R
# also at https://gist.github.com/benmarwick/f11ae49ab9afde0071b133012ff76cbc
ctv <- "https://raw.githubusercontent.com/benmarwick/ctv-archaeology/master/README.md"
library(tidyverse)
library(glue)
archy_ctv_readme <- readLines(ctv)
# get just the articles
@benmarwick
benmarwick / viralarchive.Rmd
Created Jun 1, 2021
Object recognition in Images in #viralarchive tweets
View viralarchive.Rmd
I used the Python library GetOldTweets3 to get the tweets because the rtweet package cannot get tweets older than 6-9 days. Details about this Python library are here: https://github.com/Mottl/GetOldTweets3
I used this line in the shell to get tweets using the #viralarchive hashtag:
```{bash, engine.opts="-l", eval = F}
GetOldTweets3 --querysearch 'viralarchive' --maxtweets 10000
```
@benmarwick
benmarwick / ggplot-to-jpg-set-dpi.R
Created Dec 19, 2020
How to save a ggplot as a JPG file with specific dimensions and a high dpi
View ggplot-to-jpg-set-dpi.R
# How to save a ggplot as a JPG file with a
# specific dpi and dimensions, for example,
# because a publisher requires it
library(ggplot2)
p <-
ggplot(mtcars) +
aes(mpg,
disp) +
View dsm-course-checking.R
library(tidyverse)
library(rvest)
# what quarter and year do we want to check for the availability of DSM courses?
qrt_year <- "AUT2020"
# this is the URL to our canonical list of DSM courses
webpage <- "https://www.washington.edu/uaa/advising/single-pages/data-science-minor/"
@benmarwick
benmarwick / .github-workflows-main.yml
Last active Feb 14, 2020
GitHub workflow to render all Rmd files in a GitHub repo, e.g. for testing student assignments
View .github-workflows-main.yml
# from https://github.com/cboettig/compendium/blob/master/.github/workflows/main.yml
on: [push]
name: render all R Markdown documents
jobs:
render:
name: render all R Markdown documents
runs-on: macOS-latest
steps:
@benmarwick
benmarwick / rich-diffs-rmd-in-local-git-repo.R
Last active Jun 21, 2021
GitHub doesn't show rich diffs for Rmd files. That can make collaborative writing tough. Here's how to see rich diffs of two commits of a single R Markdown document on a GitHub repo or local Git repo
View rich-diffs-rmd-in-local-git-repo.R
# How to see rich diffs of two commits of a single R Markdown document in a local Git repo
# https://github.com/lorenzwalthert/gitsum
library("gitsum")
library("tidyverse")
# To browse the commits locally, open an RStudio
# project that is using Git version control, then ...
# Set the path within the project to the Rmd file
@benmarwick
benmarwick / Tehrani_Collard_2002.R
Created Dec 30, 2019
Replicating Tehrani & Collard 2002
View Tehrani_Collard_2002.R
# paste from http://www.ceacb.ucl.ac.uk/ceacb_files/misc/Tehrani_Collard_2002.pdf
# edit to get each textile design on one line
raw_input <-
c("Ersari 1 0 1 0 1 1 0 1 0 1 1 0 1 1 100000011010000001011001111101011000011111100110111011011011011011110000000 0
Saryk 1 0 1 0 1 1 1 1 1 1 1 0 1 0 100000011010111101010100001111100000010101010110011011100000001011100000000 0
Salor 1 0 1 1 0 1 0 1 0 1 0 1 0 0 011110110101000001011000001011110111010101000100111101111101001101100000101 0
PSDP Tekke 1 1 0 0 0 1 0 1 1 0 1 0 0 0 011000011111111111011010001010100000010101101101000000000000000000001101000 0
SDP Tekke 1 1 0 0 0 1 0 1 1 0 0 1 0 0 000011111001110011111010000000000010100001100000000000001100101010001011111 1
Yomut 0 0 0 0 0 1 0 1 1 0 0 0 1 1 000000000000101100000001100000011000010100000000000000000000000000000000000 0")
@benmarwick
benmarwick / gist:cffb82fb6d9192354d495a2e0ded4e55
Last active Sep 13, 2019
Plot the population of Han Chinese in Taiwan from Wikipedia
View gist:cffb82fb6d9192354d495a2e0ded4e55
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 Aug 12, 2019
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
View inspect-rmd-diff-from-last-commit.R
# reviewer pkg comes from https://ropenscilabs.github.io/reviewer/index.html
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 Jul 28, 2019
Stacked, overlapping time series plots on a common y-axis with ggplot2
View gist:82776e452d61cfbd44972ae6a4c23c21
# 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),