Skip to content

Instantly share code, notes, and snippets.

@bschneidr
bschneidr / Update ggplot2 print method to wrap labels to device width.R
Last active April 26, 2019 18:45
Updates ggplot2 print method to wrap labels in plot to match device width
library(ggplot2)
# This is a custom printing function that will update wrapping in plot labels
# to match the width of the current device. (in RStudio, this is the 'Plots' pane)
wrap_title_to_device <- function(ggplot_obj, dev_scaler = 12) {
# Retrieve width of current device in inches
dev_width <- dev.size("in")[1]
@bschneidr
bschneidr / Wrap ggplot labels when printing.R
Created April 26, 2019 18:24
Example of how to make ggplot2 labels wrap to fit device size when printing the plot
library(ggplot2)
# This function will udpate the ggplot2 print method used in the session
wrap_labels_in_print_method <- function (dev_scaler = 12) {
if (!"ggplot2" %in% .packages()) {
stop("The ggplot2 package must be installed and loaded.")
}
# This is a custom printing function that will update wrapping in plot labels
# to match the width of the current device. (in RStudio, this is the 'Plots' pane)
@bschneidr
bschneidr / extract_plots_from_list.R
Created April 17, 2019 21:06
Extract all of the ggplot2 plots from a list (including sub-lists)
#' @title Extract ggplot2 plots from a list
#' @description Takes a list (potentially containing sublists) and extracts all of the ggplot2 'plot-type' objects from that list into a simple list of 'plot-type' objects.
#' @param x A list object, potentially containing sublists.
#' @return Returns a 'flat', single-level list of all the ggplot2 'plot-type' objects from within `x`, reaching recursively into sub-lists as needed. If there are no 'plot-type' objects, returns an empty list.
#' @note Whether an object is a ggplot2 'plot-type' object is defined here as an object with classes 'gg', 'gTree', or 'gtable'.
#' @export
#' @examples
#'
#' library(ggplot2)
#'
library(magrittr)
library(dplyr)
library(tidyr)
library(stringr)
library(readr)
# Import the race/hispanic-origin ACS data downloaded through American Fact-Finder ----
b03002_data <- read_csv(file = "Data/ACS/ZCTA/2013-2017/Raw/B03002/ACS_17_5YR_B03002_with_ann.csv",
skip = 1L,
@bschneidr
bschneidr / G-Statistic_Likelihood_Ratio_Test.R
Created February 18, 2019 21:38
Implement a 'G-statistic' Likelihood Ratio Test, similar to the Chi-Squared Independence Test.
g_statistic_lr_test <- function(x) {
chisq_results <- chisq.test(x)
expected_counts <- chisq_results[['expected']]
observed_counts <- chisq_results[['observed']]
G_Statistic <- 2 * sum(observed_counts * log(observed_counts/expected_counts),
na.rm = TRUE)
@bschneidr
bschneidr / Visualize_bad_text_with_viewer.R
Last active April 17, 2019 18:46
Visualize bad text with RStudio viewer
example_strings <- c("Heres the sentince", "Here's anothr")
highlight_bad_text <- function(strings, match = NA) {
bad_text <- hunspell::hunspell(text = as.character(strings),
format = "text",
dict = hunspell::dictionary("en_us"),
ignore = hunspell::en_stats)
names(bad_text) <- names(strings)
@bschneidr
bschneidr / Riddler Classic - July 14, 2017.R
Last active July 21, 2017 16:54
Solution to July 14, 2017 Riddler Classic
# Set the parameters which are beyond the player's control
prob_win <- 0.6
alpha <- 1000000L
beta <- 10000L
# List the possible choices available to the player
minimum_games <- 1L:50L
# List the series lengths which result from each available choice
# and, for each choice, list the corresponding probability of winning the series