Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
@trinker
trinker / multigeoms.R
Created April 2, 2018 16:29
Multiple layers over time w/ geom_box months
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(tidyverse, numform, viridis, ggrepel, lexicon)
p_load_current_gh('trinker/plotflow')
set.seed(13)
dat <- data_frame(
revenue = rnorm(5000, 50000, 5000),
date = sample(seq(as.Date('1999/01/01'), as.Date('1999/12/31'), by="day"), 5000, TRUE),
site = sample(paste("Site", 1:5), 5000, TRUE),
sales_person = sample(f_title(sample(lexicon::common_names, 15)), 5e3, TRUE)
@trinker
trinker / aes_geom_explore.R
Last active March 28, 2022 09:01
ggplot2: aesthetics and geoms exploration
In this post I have a few goals:
1. Become (re-)familiar with available geoms
2. Become (re-)familiar with aesthetic mappings in geoms (stroke who knew?)
3. Answer these questions:
<ul>
<li>How often do various geoms appear and how often do they have required aesthetics?</li>
<li>How often do various aesthetics appear and how often are they required?</li>
<li>What geoms are most similar based on mappings?</li>
</ul>
@trinker
trinker / verbatim_dynamic_regex.R
Created March 21, 2018 03:36
regex: dynamix verbatim
# test data
before <- "A."
after <- ".Z"
x <- c("A.xyz.Z", "ABxyzYZ")
pattern <- sprintf('(?<=\\Q%s\\E).*?(?=\\Q%s\\E)', before, after)
which gives:
> gregexpr(pattern, x, perl = TRUE) > 0
[1] TRUE FALSE
@trinker
trinker / bootstrap_linear_mod
Created March 21, 2018 02:48
Shape of confidence interval for predicted values in linear regression
# based on https://stats.stackexchange.com/q/85560/7482
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, ggplot2)
boot_dat <- lapply(1:1000, function(i){
inds <- sample.int(nrow(mtcars), replace = TRUE)
lm(mpg ~ hp, data = mtcars[inds, c('mpg', 'hp')])
}) %>%
lapply(function(x) setNames(as.data.frame(t(c(x$coeff))), c('intercept', 'slope'))) %>%
bind_rows() %>%
@trinker
trinker / gist:cef272b4b65462f1f6b808a164805a8b
Created March 12, 2018 02:11
Interesting text mining rticles
Main stuff below
@trinker
trinker / likert_odd.R
Created February 16, 2018 05:51
Likert ggplot2 Odd Number of Responses
###############################
## Plotting Likert Type Data ##
###############################
##------------------------------------------------------------------------
## Note: Plotting horizontal stacked bar plots in ggplot2 with Likert type
## data is a non-trivial task. Stacking is not well defined for mixed
## negative/positive values on a bar. This requires splitting the data
## set into two different parts (positive/negative), plotting each side
## separately, and filling the colors manually. This script adds complexity
## for neutral scales.
@trinker
trinker / likert_even.R
Created February 16, 2018 05:50
Likert ggplot2 Even Number of Responses
###############################
## Plotting Likert Type Data ##
###############################
##------------------------------------------------------------------------
## Note: Plotting horizontal stacked bar plots in ggplot2 with Likert type
## data is a non-trivial task. Stacking is not well defined for mixed
## negative/positive values on a bar. This requires splitting the data
## set into two different parts (positive/negative), plotting each side
## separately, and filling the colors manually.
##------------------------------------------------------------------------
@trinker
trinker / transcript_reading.R
Last active January 27, 2018 20:32
Reading trascripts with textreadr & textshape packages
## From: https://stackoverflow.com/q/48479535/1000343
## First part is just to make an external text file like the OP provided.
x <- c("Month march ", "MARK ", "good in mathematic ", "JOE", "he need help in language ",
"SUZANA ", "he is good in mathematic", "MARY ", "she has ti work hard this month ",
"", "", "Month April ", "MARK ", "good in language ", "JOE",
"he need help in mathematics ", "SUZANA ", "he is good in history ",
"MARY ", "she need help ")
@trinker
trinker / scrape_google_ngram
Last active December 30, 2017 20:40
Scrape Google Ngram
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, data.table, stringi, R.utils)
## browseURL('http://storage.googleapis.com/books/ngrams/books/datasetsv2.html')
if (!dir.exists('google_ngram')) dir.create('google_ngram')
#letter <- 'b'
get_google_ngram_data <- function(letter, ...){
loc <- sprintf('http://storage.googleapis.com/books/ngrams/books/googlebooks-eng-all-1gram-20120701-%s.gz', letter) %>%
@trinker
trinker / make_link.R
Created December 19, 2017 15:12
Make markdown links of text for href inside the markdown doc