Skip to content

Instantly share code, notes, and snippets.

View erichare's full-sized avatar
🏠
Working from home

Eric Hare erichare

🏠
Working from home
View GitHub Profile
@erichare
erichare / set_color_theme.R
Created July 25, 2018 13:11
Get a ggplot2 color scheme based on the RGB pixel values of an image/logo
# Sets theme according to color of logo
set_color_theme <- function(logo_loc, low_sat = 0.042, high_sat = 0.866, medium_sat = 0.28,
low_val = 0.99, high_val = 0.38, medium_val = 0.65) {
# Read image/logo as a list
img1 <- list(readPNG(file.path(logo_loc)))
# Find the average pixel value
rgb <- rowMeans(sapply(img1, function(im) apply(im, 3, mean) * 255))
# Convert to HSV - hue, saturation, value
img_hsv <- rgb2hsv(rgb[1:3])[,1]
@erichare
erichare / iris_tour.R
Created February 26, 2019 19:40
An iris tour using gganimate
library(tidyverse)
library(gganimate)
library(cowplot)
iris_tall <- iris %>%
cbind(iris %>%
rename_all(function(.) paste0(., "_2")) %>%
select(-Species_2)) %>%
gather(key = Variable, value = Value, c(1:(ncol(iris) - 1))) %>%
gather(key = Variable2, value = Value2, c(2:ncol(iris))) %>%
@erichare
erichare / geom_bartext.R
Last active March 11, 2019 17:28
An example of using geom_bartext to plot a bar chart with labels
library(tidyverse)
library(cowplot)
library(bartext) # devtools::install_github("Omni-Analytics-Group/bartext")
mpg_manu <- mpg %>%
distinct(manufacturer, model) %>%
mutate_all(tools::toTitleCase) %>%
mutate(model = gsub(" 4wd| 2wd| Pickup| Wagon|Toyota ", "", model))
p1 <- ggplot(mpg_manu, aes(x = manufacturer)) +
@erichare
erichare / pdftools_tables.R
Last active November 5, 2019 02:41
Extract Tables from PDF with PDFTools 2.0
library(pdftools)
library(tidyverse)
parse_tables <- function(url, remove_last = TRUE) {
my_data <- pdf_data(url)
lapply(my_data, function(my_data2) {
header_row <- my_data2 %>%
filter(y == min(y))
@erichare
erichare / droste.R
Created June 17, 2019 17:47
Drawing a Droste Effect Polygon with ggplot2 3.2.0
library(tidyverse)
polygon_values <- tibble(
id = 1:2,
value = c(-2, 2)
)
polygon_positions <- tibble(
id = c(1, 1, 1, 2, 2, 2, 2),
x = c(1, 2, 3, 5, 6, 7, 6),
library(magick)
library(tidyverse)
im <- image_read("line.jpg")
im_proc <- im %>%
image_channel("saturation") %>%
image_threshold("white", "30%") %>%
image_negate()
@erichare
erichare / fable.R
Created July 17, 2019 13:26
Sample code to forecast Air Passengers using the Fable package
library(tidyverse)
library(fable)
fable_series <- AirPassengers %>%
as_tsibble() %>%
model(
ets = ETS(box_cox(value, 0.3)),
arima = ARIMA(value),
snaive = SNAIVE(value)
) %>%
@erichare
erichare / clean.R
Created August 26, 2019 18:42
clean.R
library(tidyverse)
library(clean)
unclean_tbl <- tibble(
Logical = c("Yes", "No", "Depends", "Unknown"),
Factor = c("bachelor's", "Bachelor's Degree", "Master's Degree", "PhD"),
Currency = c("$20", "$ 25", "26.02", "$27.0"),
Numeric = c("~15", "about 10", "20", "15")
)
@erichare
erichare / scales.R
Last active November 25, 2019 15:18
Some new features in the scales 1.1 release
library(ggplot2)
ggplot(data = diamonds, aes(x = carat, y = price, colour = clarity)) +
geom_point() +
scale_y_continuous(labels = scales::dollar,
breaks = scales::breaks_width(2000)) +
scale_x_continuous(breaks = scales::breaks_width(.5))
ggplot(data = diamonds, aes(x = carat, y = price, colour = clarity)) +
geom_point() +
@erichare
erichare / investment_curve.R
Created December 8, 2020 15:07
Digitizing the investment curve
library(tidyverse)
library(ggrepel)
ln <- read_csv("Investment_Bubble_Cycle_Line.csv") %>%
mutate(Y = Y + 50 + 28)
cv <- read_csv("Investment_Bubble_Cycle.csv") %>%
mutate(Y = Y + 50)
mapping = tibble(
X = cv$X[c(20, 50, 70, 75, 105, 125, 138, 150, 175, 165, 185, 208, 225, 255, 280)],