Skip to content

Instantly share code, notes, and snippets.

@cavedave
Created November 15, 2019 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cavedave/691b58c3595523ea3f8733621583febc to your computer and use it in GitHub Desktop.
Save cavedave/691b58c3595523ea3f8733621583febc to your computer and use it in GitHub Desktop.
Martin-Quinn scores for justices, 1937-2018. The r package ggplot2 code is very slightly modified version of https://rud.is/b/2016/06/28/making-time-rivers-in-r/ by https://twitter.com/hrbrmstr data from https://mqscores.lsa.umich.edu/measures.php This made the reddit front page in a slightl different version https://www.reddit.com/r/dataisbeaut…
```{r}
library(dplyr)
library(readr)
library(ggplot2) # devtools::install_github("hadley/ggplot2")
#library(hrbrmisc) # devtools::install_github("hrbrmstr/hrbrmisc")
library(grid)
library(scales)
library(grid)
library(scales)
```
```{r}
justices <- read_csv('justices.csv')
```
```{r}
head(justices)
```
Say whether the judge is nominated by democrats or Republicans
```{r}
justices<-justices %>%
mutate(col=ifelse(justiceName %in% c("AFortas","AJGoldberg","FMurphy","BRWhite","FFrankfurter","FMVinson","HHBurton","JFByrnes","JCMcReynolds","LDBrandeis","PButler","RHJackson","TMarshall","SFReed","SGBreyer","SMinton","HLBlack", "EKagan", "RBGinsburg", "SSotomayor","WBRutledge","WODouglas"),
"Democrat", "Republican")) -> recent
```
Get names of each justice. Ill do this automatically and move a few. Original did it manually and was cleaner
```{r}
tiny<-justices %>%
group_by(justiceName) %>%
filter(row_number()==1)
tiny
```
Now write the name of each justice just above where the start
```{r}
tiny<-select (tiny,justiceName, term, post_med,justice)
tiny = rename(tiny, label=justiceName, x=term, y=post_med)
tiny2 = mutate(tiny, x=x-.5)
tiny2
```
Change judges from just above where they start where they cause confusion
```{r}
tiny2<-mutate(tiny2, y = ifelse(label == "SAAlito", 1.65, y))
tiny2<-mutate(tiny2, y = ifelse(label == "SGBreyer", -.6, y))
#FMurphy 1938.5 -1.613
tiny2<-mutate(tiny2, y = ifelse(label == "FMurphy", -1.8, y))
#FMVinson 1945.5 0.410
tiny2<-mutate(tiny2, y = ifelse(label == "FMVinson", .3, y))
```
```{r}
gg <- ggplot(recent)#recent
gg <- gg + geom_hline(yintercept=0, alpha=0.5)
#gg <- gg + geom_label(data=data.frame(x=c(0.1, -0.1),
# label=c("More →\naffirming", "← More\nreversing"),#label=c("More #→\nconservative", "← More\nliberal"),
# hjust=c(0, 1)), aes(y=x, x=1932, hjust=hjust, label=label),
# family="Arial Narrow", fontface="bold", size=4, label.size=0, vjust=1)
gg <- gg + geom_text(x=1940,y=4,
label="↑ More\naffirming")
gg <- gg + geom_text(x=1940,y=-4,
label="↓ More\nreversing")
#gg <- gg + geom_ribbon(aes(ymin=post_mn-post_sd, ymax=post_mn+post_sd, x=term,
# group=justice, fill=col, color=col), size=0.1, alpha=0.3)
gg <- gg + geom_line(aes(x=term, y=post_med, color=col, group=justice), size=0.4)
#gg <- gg + geom_text(data=just_labs, aes(x=x, y=y, label=label),
# family="Arial Narrow", size=2.0)
gg <- gg + geom_text(data=tiny2, aes(x=x, y=y, label=label),
family="Arial Narrow", size=2.0)
#gg <- gg + scale_x_reverse(expand=c(0,0), limits=c(2019, 1932),#1982
# breaks=c(2018, seq(2018, 1940, -10), 1937, 1932))#,#
# labels=c(2018, seq(2018, 1990, -10), "1937\nTERM\n↓", ""))
## you can flip cords if you prefer
##gg <- gg + coord_flip()
gg <- gg + labs(x=NULL, y=NULL,
title="Martin-Quinn scores for justices, 1937-2018",
subtitle="Line is the M-Q median. Roughly how likely a judge is to change the view of a lower court",
caption="Data source: https://mqscores.lsa.umich.edu/measures.php")
gg <- gg + theme(plot.subtitle=element_text(margin=margin(b=15)))
gg <- gg + theme(legend.title=element_text(face="bold"))
gg <- gg + theme(legend.position=c(0.05, 0.6))
gg <- gg + theme(plot.margin=margin(20,20,20,20))
#gg <- gg + scale_y_continuous(expand=c(0,0), labels=c(-10,-4, "0\nM-Q Score", 4, 10))
gg <- gg + scale_color_manual(name=NULL, values=c(Democrat="#2166ac", Republican="#b2182b"), guide=FALSE)
gg <- gg + scale_fill_manual(name="Nominated by a", values=c(Democrat="#2166ac", Republican="#b2182b"))
gg <- gg + theme_bw()
ggsave('courtAll8.png')
gg
```
@cavedave
Copy link
Author

courtAll8

@cavedave
Copy link
Author

tbkdkwgxgmy31 (1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment