-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCriminalInjustice.Rmd
105 lines (91 loc) · 2.74 KB
/
CriminalInjustice.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "Change in Pre-trial Inmate Population"
output: html_notebook
---
```{r}
# loading the necessary packages
library(tidyverse)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
```
```{r}
# loading the data that was preprocessed in python
df_inmates <- read.csv("~/Downloads/counts.csv")
# saving the date column in date format
df_inmates$date <- as.Date(df_inmates$date)
df_inmates
```
```{r}
branded_colors <- list(
"blue" = "#00798c",
"red" = "#d1495b",
"yellow" = "#edae49",
"green" = "#66a182",
"navy" = "#2e4057",
"grey" = "#8d96a3"
)
branded_pal <- function(
primary = "blue",
other = "grey",
direction = 1
) {
stopifnot(primary %in% names(branded_colors))
function(n) {
if (n > 6) warning("Branded Color Palette only has 6 colors.")
if (n == 2) {
other <- if (!other %in% names(branded_colors)) {
other
} else {
branded_colors[other]
}
color_list <- c(other, branded_colors[primary])
} else {
color_list <- branded_colors[1:n]
}
color_list <- unname(unlist(color_list))
if (direction >= 0) color_list else rev(color_list)
}
}
scale_colour_branded <- function(
primary = "blue",
other = "grey",
direction = 1,
...
) {
ggplot2::discrete_scale(
"colour", "branded",
branded_pal(primary, other, direction),
...
)
}
scale_color_branded <- scale_colour_branded
inmates <- ggplot(df_inmates, aes(x = date, y = id, group = race, color = factor(race))) +
# specify the type of chart
geom_line() +
# choose a custom color scheme
scale_colour_branded(other="yellow") +
#scale_fill_manual(values=c("#CC6666", "#9999CC", "black"))+
# make the chart animated
transition_reveal(date) +
# set the plot and axis titles, set text size and positions
theme(legend.position = "top",
axis.text.x = element_text(face = "bold", size = 14),
axis.text.y = element_text(face = "bold", size = 14),
plot.title = element_text(size=24),
plot.subtitle = element_text(face = "bold", size=18),
legend.text=element_text(size=14),
legend.title = element_text(size=16),
legend.key.size = unit(2,"cm"),
axis.title = element_text(face = "bold", size = 18)
) +
labs(title = "Change in Pre-trial Inmate Population",
x = "Year",
y = "Number of Pre-trial Inmates",
colour = 'Race') +
scale_x_date(date_labels = "%Y-%m", date_breaks = "6 months")
# set custom size
animate(inmates, duration = 12, fps = 20, width = 900, height = 600, renderer = gifski_renderer())
# save the animation
anim_save("inmates.gif")
```