Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Just typin' R code

Jonathan Carroll jonocarroll

👨‍💻
Just typin' R code
View GitHub Profile
@jonocarroll
jonocarroll / fakelegend.R
Created May 7, 2020
Add a fake legend annotation to a ggplot
View fakelegend.R
library(ggplot2)
library(dplyr)
## fake data
d <- mtcars %>%
group_by(cyl) %>%
summarise(lower = min(disp),
med = median(disp),
upper = max(disp))
p <- ggplot(d, aes(x = factor(cyl), y = med)) +
@jonocarroll
jonocarroll / coviddeaths.R
Last active May 7, 2020
USA daily deaths with and without NY, highlighting weekends
View coviddeaths.R
library(COVID19)
library(dplyr)
library(zoo) # rollmean
library(ggplot2)
library(ggeasy) # easy_add_legend_title
#### deaths ####
## gather USA data at state level and identify weekends
us <- covid19("USA", level = 2) %>%
@jonocarroll
jonocarroll / google_covid_mobility_plotter.R
Created Apr 16, 2020
Modification of google_covid_mobility_scraper to work with @nacnudus' data
View google_covid_mobility_plotter.R
# Fork of https://gist.github.com/johnburnmurdoch/1d23978fc8213ff656d9f629608dd1f5/revisions
# modified to work with https://github.com/nacnudus/google-location-coronavirus
# Install and load required packages
# install.packages("needs")
# library(needs)
# needs(tidyverse, magrittr, animation, pdftools, png, scales)
library(tidyverse)
library(magrittr)
library(animation)
@jonocarroll
jonocarroll / euler_d3.R
Created Feb 21, 2020
Interactive set selector
View euler_d3.R
library(htmltools)
library(d3r)
library(eulerr)
ui <- list(
attachDependencies(
tagList(),
d3_dep_v5()
),
titlePanel("Interactive Set Selector"),
@jonocarroll
jonocarroll / R_sort.R
Last active Dec 3, 2019
R vs Julia sorting
View R_sort.R
fastsort <- function(x, partial) {
y <- if (length(partial) <= 10L) {
partial <- .Internal(qsort(partial, FALSE))
.Internal(psort(x, partial))
}
else {
.Internal(qsort(x, FALSE))
}
y
}
@jonocarroll
jonocarroll / mip_julia.jl
Last active Oct 22, 2019
Graph coloring comparison between Julia and R
View mip_julia.jl
using JuMP, GLPK
function graph_coloring(;fast=true, benchmark=false)
m = Model(with_optimizer(GLPK.Optimizer, msg_lev = 3))
@variable(m, max_color, Int)
@variable(m, c[1:49,1:4], Bin)
@objective(m, Min, max_color)
View keybase.md

Keybase proof

I hereby claim:

  • I am jonocarroll on github.

  • I am jonocarroll (https://keybase.io/jonocarroll) on keybase.

  • I have a public key ASBY3mLwKW3JR6kHm4Pb0RN9EJIp6G3X8nD9SJMhylURGAo

@jonocarroll
jonocarroll / as.data.frame.matrix_diff.R
Last active Apr 20, 2019
Differences in as.data.frame.matrix between R 3.4.3 and R 3.5.3
View as.data.frame.matrix_diff.R
## create a copy of mtcars
m <- mtcars
## set every other value of cyl to NA
m$cyl <- m$cyl[c(TRUE, NA)]
## show a table of cyl vs am
table(m$cyl, m$am, useNA = "always")
# 0 1 <NA>
# 4 2 3 0
View threed_rotate_cube.R
library(threed)
library(dplyr)
library(gganimate)
library(ggplot2)
angles <- tail(seq(0, pi/2, length.out = 15), -1)
camera_to_world <- threed::look_at_matrix(eye = c(1.5, 1.75, 4), at = c(0, 0, 0))
objlist <- vector("list", length(angles))
@jonocarroll
jonocarroll / image_as_xaxis_latex.R
Created Oct 16, 2018
images as xaxis in R via LaTeX
View image_as_xaxis_latex.R
library(rvest)
## GDP per capita, top 11 countries
n_countries <- 11
url <- "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita"
html <- read_html(url)
gdppc <- html_table(html_nodes(html, "table")[3])[[1]][1:n_countries, ]
## clean up; remove non-ASCII and perform type conversions
gdppc$Country <- gsub("Â ", "", gdppc$Country)
You can’t perform that action at this time.