Skip to content

Instantly share code, notes, and snippets.

@karawoo
karawoo / reprex.R
Created April 24, 2020 21:32
reprex demo
#######################
#### Reprex demo ####
#######################
## 2020-04-24
# A simple reprex --------------------------------------------------------------
dat <- data.frame(a = 1:2, b = LETTERS[1:2])
mean(dat$a)
@karawoo
karawoo / count-code-reviews.R
Created October 28, 2019 20:48
Count number of code reviews on a repo
############################################
#### Calculate number of code reviews ####
############################################
## Needs a GitHub PAT or you'll hit rate limiting
library("gh")
library("purrr")
library("glue")
library("cholera")
library("tidyverse")
library("magick")
library("grid")
library("egg")
# Get image
bree <- image_read("https://pbs.twimg.com/media/EBPHuylUIAAziJt?format=jpg")
# Add x/y coordinates of Broad St. pump and convert format
@karawoo
karawoo / ampad_accession.R
Created July 3, 2019 16:34
Follower accession counts for @AMPADPortal
## Follower accession chart for AMPADportal
## Inspired by: https://blog.ouseful.info/2013/04/05/estimated-follower-accession-charts-for-twitter/
library("rtweet")
library("tidyverse")
followers <- get_followers("AMPADPortal")
followers_df <- lookup_users(followers$user_id)
process_user_data <- function(data) {
## Start with the code and data here: https://github.com/fivethirtyeight/data/blob/master/police-deaths/plot.R
## Then:
## Calculate total deaths by year
dat <- data_for_plot %>%
group_by(year) %>%
mutate(total = sum(count))
## Colors I chose
mycolors <- c("#00bf7b", "#59004d", "#ffcb89", "#a76e61", "#ac270d", "#7890ff",
## Load tidyverse package, which contains ggplot2, dplyr, tidyr, etc.
library("tidyverse")
## Create some data
set.seed(432)
dat <- tibble(fert = c(rnorm(n = 10, mean = 0.6, sd = 0.2),
rnorm(n = 10, mean = 0.4, sd = 0.2)),
treat = rep(c("25-25", "28-28"), each = 10))
## Plot boxplots with points on top + yellow X for mean & legend
@karawoo
karawoo / data.csv
Last active June 29, 2016 20:19
Reading in CSVs with headers on multiple rows
B0S2 B0S3 B0S4
abundance percent cover height abundance percent cover height abundance percent cover height
5 50 71 8 30 9 45
5 3 47 1 1
## Load ggplot package
library("ggplot2")
## Create sample data
dat <- structure(list(x = c(2.69753522456158, 9.77329733015504,
0.579014160879888, 6.01934352936223,
5.53224114519544, 7.52123757028021, 1.1418573057279,
5.81543114883825, 1.98635061332025,
8.98725295660552),
y = c(1.1341254551895, 8.69686821205542, 1.0851298507303,
@karawoo
karawoo / stacked_area.R
Created February 20, 2016 21:53
Stacked area charts
library("ggplot2")
library("dplyr")
###########################################################
#### This does not work to create stacked area chart ####
###########################################################
## Create data
set.seed(40)
dat <- data.frame(x = rep(c(1:10), 3),
# In response to: https://twitter.com/duffy_ma/status/628300075110936576
df <- data.frame(x=1:10, y = 1:10)
library("plyr")
df$y <- mapvalues(df$y, from = c(5), to = c(NA)) # make sure to keep `y` column numeric
lengthdata <- length(df$x)
for(i in 1:(lengthdata-1)) {
if (is.na(df$y[i])) { # use is.na to test for NAs