Skip to content

Instantly share code, notes, and snippets.

View py's full-sized avatar

Peter Yates py

View GitHub Profile
@py
py / create_xbar_chart.R
Created June 18, 2024 21:23
QCC's xbar chart recreated in ggplot2
# Load libraries -------
library(ggplot2)
library(dplyr)
# Function to create X-bar chart
create_xbar_chart <- function(
data,
x_var,
y_var,
title = ""
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 16 columns, instead of 9 in line 9.
fips,name,state,pop1900,pop1910,pop1920,pop1930,pop1940,pop1950,pop1960,pop1970,pop1980,pop1990,pop2000,pop2010,pop2020
01001,AL Autauga County,AL,17915,20038,18908,19694,20977,18186,18739,24460,32259,34222,43411,54116,58805
01003,AL Baldwin County,AL,13194,18178,20730,28289,32324,40997,49088,59382,78556,98280,138141,179958,231767
01005,AL Barbour County,AL,35152,32728,32067,32425,32722,28892,24700,22543,24756,25417,26320,24264,25223
01007,AL Bibb County,AL,18498,22791,23144,20780,20155,17987,14357,13812,15723,16576,19587,20691,22293
01009,AL Blount County,AL,23119,21456,25538,28020,29490,28975,25449,26853,36459,39248,50414,56833,59134
01011,AL Bullock County,AL,31944,30196,25333,20016,19810,16054,13462,11824,10596,11042,10203,9224,10357
01013,AL Butler County,AL,25761,29030,29531,30195,32447,29228,24560,22007,21680,21892,21134,20614,19051
01015,AL Calhoun County,AL,34874,39115,47822,55611,63319,79539,95878,103092,119761,116034,109821,115653,116441
01017,AL Chambers County,AL,32554,36056,41201,39313,42146,395
#------------------------------------------------------------------------------#
# Generate a file with county-level decennial census data 1900 to 2020
# Author: Peter Y (github: py)
# Date: 2022-07-01
# Inputs:
# - NBER population file 1900 to 1990 (https://data.nber.org/census/pop/cencounts.csv")
# - Census API calls for 2000 to present
# Outputs:
# An R tibble with a row for each US county and a column for each decade 1900 to 2020
# with that county's population. The NBER file does include rows for US total and each
# Adaptation of http://martinsbioblogg.wordpress.com/2014/03/03/using-r-correlation-heatmap-take-2/ with labels
data <- airquality[,1:4]
library(ggplot2)
library(reshape2)
qplot(x=Var1, y=Var2, data=melt(cor(data, use="p")), fill=value, geom="tile") +
scale_fill_gradient2(limits=c(-1, 1)) +
geom_text(aes(label=round(melt(cor(data, use="p"))$value,2)))
<?xml version="1.0" encoding="UTF-8" ?>
<!--
MINDMAPEXPORTFILTER md;markdown Markdown
v. 0.1
This code released under the GPL. : (http://www.gnu.org/copyleft/gpl.html)
Document : mm2markdown.xsl
@py
py / mm2markdown.xsl
Created November 21, 2013 01:38
Freeplane export xsl: Converts mindmap into markdown
<?xml version="1.0" encoding="UTF-8" ?>
<!--
MINDMAPEXPORTFILTER md;markdown Markdown
: This code released under the GPL. :
(http://www.gnu.org/copyleft/gpl.html)
Document : mm2markdown.xsl
Created on : 2013-11-20
#------------------------------------------------------------------------------#
# Bayesian update with new information
# Based on http://www.databozo.com/2013/09/15/Bayesian_updating_of_probability_distributions.html
#------------------------------------------------------------------------------#
# Load packages
library('ggplot2')
# Define functions
normalize <- function(pos) {
# Normalizes the possibilities so that sum = 1
@py
py / binomial_calcs.r
Created January 22, 2012 23:13
vos Savant binomial calcs
# Libraries
require("ggplot2")
# Define variables
n = 4
p = .25
k = c(1:4)
# Calculate binomial probabilities for X=[1:4]
probs <- dbinom(x=k, size=n, prob=p)