Reproducible ggghost example from https://matthewdharris.com/2016/08/12/ggplot2-step-by-step/
## Based on BUILDING A GGPLOT2 STEP BY STEP | |
## https://matthewdharris.com/2016/08/12/ggplot2-step-by-step/ | |
## 'reproducible' saving of data: | |
## this was to be copied by hand, hoping that Unicode chars didn't get caught up | |
## requires re-formatting to factors, etc... | |
year <- c(1760, 1790, 1797, 1850, 1860, 1889, 1900, 1910, 1950) | |
sites <- c("Isleta", "Acoma", "Laguna", "Zuni", "Sandia", "San Felipe", | |
"Santa Ana", "Zia", "Santo Domingo", "Jemez", "Cochiti", | |
"Tesuque", "Nambe", "San Ildefonso", "Pojoaque", "Santa Clara", | |
"San Juan", "Picuris", "Toas") | |
south <- c(seq_along(sites)) | |
east <- c(14, 18, 17, 19, 12, 11, 13, 15, 10, 16, 9, 4, 3, 7, 5, 8, 6, 2, 1) | |
pop <- c( | |
c(304, 410, 603, 751, 440, 1037, 1035, 956, 1051), | |
c(1052, 820, 757, 367, 523, 582, 492, 691, 1376), | |
c(600, 668, 802, 749, 929, 970, 1077, 1472, 1655), | |
c(664, 1935, 2716, 1294, 1300, 1547, 1525, 1667, 2564), | |
c(291, 304, 116, 241, 217, 150, 81, 73, 150), | |
c(458, 532, 282, 800, 360, 501, 515, 502, 721), | |
c(404, 356, 634, 339, 316, 264, 228, 219, 285), | |
c(568, 275, 262, 124, 115, 113, 115, 109, 145), | |
c(424, 650, 483, 666, 262, 930, 771, 817, 978), | |
c(373, 485, 272, 365, 650, 474, 452, 449, 789), | |
c(450, 720, 505, 254, 172, 300, 247, 237, 289), | |
c(232, 138, 155, 119, 97, 94, 80, 80, 145), | |
c(204, 155, 178, 107, 107, 80, 81, 88, 96), | |
c(484, 240, 251, 319, 166, 189, 137, 114, 152), | |
c(99, 53, 79, 48, 37, 18, 12, 16, 2), | |
c(257, 134, 193, 279, 179, 187, 222, 243, 511), | |
c(316, 260, 202, 568, 343, 373, 422, 388, 152), | |
c(328, 254, 251, 222, 143, 120, 95, 104, 99), | |
c(505, 518, 531, 361, 363, 324, 462, 517, 842) | |
) | |
dat <- data.frame(Year = rep(year, length(sites)), | |
Site = rep(sites, each = length(year)), | |
Population = pop, | |
South = rep(south, each = length(year)), | |
East = rep(east, each = length(year))) | |
dat$East_label <- rep(factor(sites, levels = (sites[order(east)])), each = length(year)) | |
## ############## ## | |
## ALTERNATIVELY: ## | |
## ############## ## | |
## ggghost (https://github.com/jonocarroll/ggghost) provides functionality to | |
## save the resulting ggplot2 code and data together using %g<% | |
library(ggplot2) | |
library(ggghost) ## devtools::install_github("jonocarroll/ggghost") | |
## final ggplot2 call from blog post: | |
## changed `pop` to `Population` and re-formatted to use `+` notation | |
p %g<% ggplot(dat, aes(x = as.factor(Year), y = log(Population), group = East)) | |
p <- p + geom_smooth(data = transform(dat, East_label = NULL), | |
method = "lm", formula = y ~ splines::bs(x, 3), | |
se = FALSE, color = "gray90", size = 0.5) | |
p <- p + geom_hline(yintercept = mean(log(Population)), linetype = 5, color = "gray35") | |
p <- p + facet_wrap( ~ East_label, nrow = 2) | |
p <- p + geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3), | |
se = FALSE, color = "red", fill = "gray70") | |
p <- p + theme_bw() | |
p <- p + labs(title="Population Change in New Mexico Pueblos, 1760 to 1950", | |
subtitle="Arranged from East to West", | |
caption="Data: Zubrow(1974)", | |
x = "Year") | |
p <- p + theme( | |
strip.background = element_rect(colour = "white", fill = "white"), | |
strip.text.x = element_text(colour = "black", size = 7, face = "bold", | |
family = "Trebuchet MS"), | |
panel.margin = unit(0, "lines"), | |
panel.border = element_rect(colour = "gray90"), | |
axis.text.x = element_text(angle = 90, size = 6, family = "Trebuchet MS"), | |
axis.text.y = element_text(size = 6, family = "Trebuchet MS"), | |
axis.title = element_text(size = 8, family = "Trebuchet MS"), | |
plot.caption = element_text(size = 8, hjust=0, margin=margin(t=5), | |
family = "Trebuchet MS"), | |
plot.title=element_text(family="TrebuchetMS-Bold"), | |
plot.subtitle=element_text(family="TrebuchetMS-Italic") | |
) | |
## save the now reproducible example | |
saveRDS(p, file = "./QuantArch.rds") | |
## a copy of this .rds is available here: | |
## https://s3-ap-southeast-2.amazonaws.com/jcarroll1/QuantArch.rds | |
### starting a new session, load code and data from the saved file | |
library(ggplot2) | |
library(ggghost) | |
p <- readRDS(file = "./QuantArch.rds") | |
## this and this alone re-builds the entire graph, | |
## recovering the data from the saved object | |
p | |
## inspect the calls that produce this | |
summary(p) | |
## recover `dat` back into the worspace | |
recover_data(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment