Skip to content

Instantly share code, notes, and snippets.

@zackw
Created April 1, 2017 18:22
Show Gist options
  • Save zackw/59812ffb3cce3c3acb434fb8ee68c9d1 to your computer and use it in GitHub Desktop.
Save zackw/59812ffb3cce3c3acb434fb8ee68c9d1 to your computer and use it in GitHub Desktop.
# Get nwquantbyage.csv from https://dqydj.com/wp-content/uploads/2015/03/nwquantbyage.csv
library(reshape2)
library(ggplot2)
library(scales)
library(viridis)
biexp2_trans <- function(lim = 100, decade.size = lim)
{
trans <- function(x){
ifelse(abs(x) <= lim,
x,
sign(x) * (lim + decade.size * (suppressWarnings(log(abs(x), 10)) - log(lim, 10))))
}
inv <- function(x) {
ifelse(abs(x) <= lim,
x,
sign(x) * 10^(((abs(x)-lim)/decade.size) + log(lim,10)))
}
breaks = pretty_breaks()
trans_new(paste0("biexp-",format(lim)), trans, inv, breaks)
}
nqa <- read.csv("nwquantbyage.csv")
nqa$percentile <- as.numeric(sub("%", "", as.character(nqa$AGE)))
nqa$AGE <- NULL
nqal <- melt(nqa, id.vars=c("percentile"))
colnames(nqal) <- c("percentile", "age bracket", "net worth")
levels(nqal$`age bracket`) <- c("18-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65+")
ggplot(nqal, aes(x=percentile, y=`net worth`, colour=`age bracket`)) +
geom_line() + scale_colour_viridis(discrete=TRUE) +
scale_y_continuous(trans=biexp2_trans(lim=100, decade.size=100),
breaks=c(-200000, -2000, 0, 2000, 200000, 20000000),
labels=comma)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment