Skip to content

Instantly share code, notes, and snippets.

@arbelt
Forked from hrbrmstr/orig.png
Last active March 22, 2016 16:14
Show Gist options
  • Save arbelt/f163edcb88480c212734 to your computer and use it in GitHub Desktop.
Save arbelt/f163edcb88480c212734 to your computer and use it in GitHub Desktop.
Supreme Annotations - moar splainin here: http://rud.is/b/2016/03/16/supreme-annotations/ - NOTE: this requires the github version of ggplot2
# CAVEATS:
#
# 1. This requires nonstandard fonts. Showtext is the best way to handle this portably, BUT
# it doesn't work within the RStudio viewer pane. Exported files (and knitr) are fine, also
# interactive sessions without RStudio (e.g., using XQuartz).
#
# When working in RStudio, I generally don't change the font and preview an "ugly" version
# of the chart. Just make sure all the elements are set when exporting.
#
# To set up showtext:
library(showtext)
font.add.google("Open Sans Condensed", "Open Sans Condensed", regular.wt = 300)
showtext.auto()
# In knitr, set the fig.showtext CHUNK PARAMETER to TRUE. Globally (for a file), you can put
# this in a chunk at the top:
library(knitr)
opts_chunk$set(fig.showtext = TRUE)
# or just set it for specific chunks
# 2. geom_label with label.size = 0 doesn't work (for me, at least) on the default pdf
# device. You'll need to use CairoPDF to get the boxes to disappear. This may or may
# not be a little complicated to get working completely.
#
# **THEME**
# This is just a way to package most aspects of the above theme to make it easier to use more
# generally.
#
# Note that you will typically need to manually tweak the x and y axes, as well as the legend
# on a per-plot basis.
#
# For the axes, you usually want to get rid of the automatic space at the edges (this is why
# bar plots are by default lifted above the axis). This is done with the expand parameter:
#
# scale_x_continuous(expand = c(0,0), ...)
#
# But this will probably also require you to set the limits of the axis manually to prevent cutoff,
# etc.
#
# First, set the global theme used for all plots:
theme_set(theme_minimal(base_family = "Open Sans Condensed"))
# Define the set of theme modifications. Then for any ggplot object this can be added
# to apply the theme, e.g.,
#
# p <- ggplot(df) + ...
# p <- p + theme_mods
#
theme_mods <- theme(
legend.position = c(0,1),
legend.justification = c(0,1),
legend.text = element_text(size = 8, face = "bold"),
legend.key.size = unit(3, "mm"),
plot.margin = unit(rep(0.5,4), "cm"),
panel.grid = element_line(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(color = "#2b2b2b", linetype = "dotted", size = 0.15),
axis.text.y = element_text(margin = margin(r = 0)),
axis.ticks.x = element_line(size = 0.15, color = "#2b2b2b"),
axis.ticks.y = element_blank(),
axis.ticks.length = unit(5, "pt"),
axis.line = element_line(size = 0.15),
axis.line.x = element_line(size = 0.15, color = "#2b2b2b"),
plot.title = element_text(face = "bold"),
plot.caption = element_text(size = 8, hjust = 0, margin = margin(t = 15)))
library(ggplot2)
dat <- read.csv("supreme_court_vacancies.csv",
col.names=c("year", "wait"))
# We only want every other tick labeled
xlabs <- seq(1780, 2020, by=10)
xlabs[seq(2, 24, by=2)] <- " "
# Pretty long caption
caption <- "Note: Vacancies are counted as the number of days between a justice's death, retirement or resignation and the successor justice's swearing in (or commissioning in the case of a recess appointment) as a member of the court.Sources: U.S. Senate, 'Supreme Court Nominations, present-1789'; Supreme Court, 'Members of the Supreme Court of the United States'; Pew Research Center calculations"
caption <- paste0(strwrap(caption, 160), sep="", collapse="\n")
# you could probably just use caption <- label_wrap_gen(160)(caption) instead
annot <- read.table(text=
"year|wait|just|text
1848|860|0|Robert Cooper Grier was sworn in Aug 10, 1846,<br>841 days after the death of Henry Baldwin
1969|440|1|Henry Blackmun was sworn<br>in June 9, 1970, 391 days<br>after Abe Fortas resigned.
1990|290|0|Anthony Kennedy<br>was sworn in Feb.<br>18, 1988, 237<br>days after Lewis<br>Powell retired.",
sep="|", header=TRUE, stringsAsFactors=FALSE)
annot$text <- gsub("<br>", "\n", annot$text)
gg <- ggplot()
gg <- gg + geom_point(data=dat, aes(x=year, y=wait))
gg <- gg + geom_label(aes(x=1780, y=900, label="days"),
family="OpenSans-CondensedLight",
size=3.5, hjust=0, label.size=0, color="#2b2b2b")
gg <- gg + geom_label(data=annot, aes(x=year, y=wait, label=text, hjust=just),
family="OpenSans-CondensedLight", lineheight=0.95,
size=3, label.size=0, color="#2b2b2b")
gg <- gg + scale_x_continuous(expand=c(0,0),
breaks=seq(1780, 2020, by=10),
labels=xlabs, limits=c(1780,2020))
gg <- gg + scale_y_continuous(expand=c(0,10),
breaks=seq(100, 900, by=100),
limits=c(0, 1000))
gg <- gg + labs(x=NULL, y=NULL,
title="Lengthy Supreme Court vacancies are rare now, but weren't always",
subtitle="Supreme Court vacancies, by duration",
caption=caption)
gg <- gg + theme_minimal(base_family="OpenSans-CondensedLight")
gg <- gg + theme(panel.grid=element_line())
gg <- gg + theme(panel.grid.major.y=element_line(color="#2b2b2b", linetype="dotted", size=0.15))
gg <- gg + theme(panel.grid.major.x=element_blank())
gg <- gg + theme(panel.grid.minor.x=element_blank())
gg <- gg + theme(panel.grid.minor.y=element_blank())
gg <- gg + theme(axis.line=element_line())
gg <- gg + theme(axis.line.x=element_line(color="#2b2b2b", size=0.15))
gg <- gg + theme(axis.ticks=element_line())
gg <- gg + theme(axis.ticks.x=element_line(color="#2b2b2b", size=0.15))
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.ticks.length=unit(5, "pt"))
gg <- gg + theme(plot.margin=unit(rep(0.5, 4), "cm"))
gg <- gg + theme(axis.text.y=element_text(margin=margin(r=-5)))
gg <- gg + theme(plot.title=element_text(family="OpenSans-CondensedBold", margin=margin(b=15)))
gg <- gg + theme(plot.subtitle=element_text(family="OpenSans-CondensedLightItalic"))
gg <- gg + theme(plot.caption=element_text(size=8, hjust=0, margin=margin(t=15)))
gg
year wait
1845 841
1792 152
1793 43
1795 103
1795 70
1795 4
1799 37
1800 141
1801 180
1803 98
1806 62
1811 504
1811 154
1823 166
1825 133
1829 55
1830 500
1835 264
1835 164
1836 483
1842 314
1845 440
1845 6
1851 17
1852 260
1857 111
1862 780
1862 584
1862 293
1864 66
1869 43
1872 43
1873 301
1878 275
1880 111
1880 19
1882 162
1882 66
1887 244
1887 191
1890 293
1891 76
1892 39
1892 266
1894 244
1896 152
1897 57
1902 6
1903 84
1906 207
1909 199
1909 72
1910 166
1910 45
1910 17
1911 154
1914 92
1916 156
1916 121
1920 55
1922 53
1922 10
1924 53
1929 14
1930 80
1931 62
1936 76
1938 58
1938 10
1939 207
1939 76
1941 158
1941 0
1942 137
1942 14
1944 58
1946 64
1948 35
1953 25
1955 170
1956 0
1956 23
1958 0
1962 37
1962 21
1964 68
1968 0
1968 111
1970 391
1972 115
1972 103
1976 41
1981 82
1986 2
1987 237
1990 19
1990 82
1992 43
1994 0
2005 0
2005 25
2009 41
2010 41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment