Skip to content

Instantly share code, notes, and snippets.

@mrecos
Created March 16, 2016 10:09
Show Gist options
  • Select an option

  • Save mrecos/d9c5453382bb3b16f106 to your computer and use it in GitHub Desktop.

Select an option

Save mrecos/d9c5453382bb3b16f106 to your computer and use it in GitHub Desktop.
Used Zubrow (1974) data on population change over time in Pueblos of New Mexico to illustrate new features in ggplot2: subtitles and caption. More details on these new features can be found here: https://gist.github.com/hrbrmstr/283850725519e502e70c
library("ggplot2") # Must be dev version, use: devtools::install_github("hadley/ggplot2")
library("gridExtra")
library("extrafont") # Need to follow steps here: http://zevross.com/blog/2014/07/30/tired-of-using-helvetica-in-your-r-graphics-heres-how-to-use-the-fonts-you-like-2/
# create data frame
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))
## Create ggplot
p <- ggplot(dat, aes(x = 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(pop)), linetype = 5, color = "gray35")
p <- p + geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3),
se = FALSE, color = "red", fill = "gray70")
p <- p + facet_wrap( ~ East_label, nrow = 2)
p <- p + theme_bw()
p <- p + labs(title="Population Change in New Mexico Pueblos",
subtitle="Arranged from East to West",
caption="Data: Zubrow(1974)")
p <- p + scale_x_continuous(breaks = c(1760, 1797, 1850, 1900, 1950))
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")
)
plot(p)
# save ggplot
ggsave(filename = "population.png", width = 8, height = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment