-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path2021_w40_NBERpapers.Rmd
178 lines (149 loc) · 4.48 KB
/
2021_w40_NBERpapers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
---
title: "2021_w40_NBERpapers"
author: "Albert Rapp"
date: "28 9 2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
papers <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-28/papers.csv')
authors <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-28/authors.csv')
programs <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-28/programs.csv')
paper_authors <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-28/paper_authors.csv')
paper_programs <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-09-28/paper_programs.csv')
```
Let's bring these tables together by joining them.
```{r}
dat <- paper_programs %>%
left_join(programs, by = "program") %>%
left_join(paper_authors, by = "paper") %>%
left_join(authors, by = "author") %>%
left_join(papers, by = "paper") %>%
mutate(catalogue_group = str_sub(paper, 1, 1)) %>%
mutate(
catalogue_group = case_when(
catalogue_group == "h" ~ "Historical",
catalogue_group == "t" ~ "Technical",
catalogue_group == "w" ~ "General"
)
)
```
Let's try to manually do a micro vs. macro split violin plot over time.
First, take a look at the manual counts to see if that looks possible.
```{r}
counts <- dat %>%
filter(str_detect(program_category, "Micro") |
str_detect(program_category, "Macro")) %>%
count(program_category, year) %>%
mutate(
n = if_else(str_detect(program_category, "Micro"), n, -n)
)
count_plot <- counts %>%
ggplot(aes(x = n, y = factor(year), fill = program_category)) +
geom_col() +
scale_y_discrete(breaks = seq(1975, 2021, 5)) +
coord_cartesian(xlim = c(-9000, 9000))
count_plot
```
This looks like it might be suitable for a decent plot.
Thus, compute the densities.
```{r}
estimate_density <- function(dat, variable) {
dens <- dat %>%
filter(program_category == variable) %>%
pull(year) %>%
abs() %>%
density()
tibble(
year = dens$x,
count = dens$y,
program_category = variable
) %>%
mutate(count = if_else(program_category != "Micro", -count, count))
}
dens <- dat %>%
estimate_density("Micro") %>%
bind_rows(dat %>% estimate_density("Macro/International"))
uncropped <- dens %>%
ggplot(aes(count, year, fill = program_category)) +
geom_polygon()
uncropped
```
Now crop to range
```{r}
year_range <- dat$year %>% range()
add_entries <- function(dens, variable) {
dens %>%
filter(
between(year, year_range[1], year_range[2]),
program_category == variable
) %>%
bind_rows(
tibble(
year = year_range[1],
count = 0,
program_category = variable
), .) %>%
bind_rows(
tibble(year = year_range[2], count = 0, program_category = variable)
)
}
cropped_plot <- dens %>%
add_entries("Micro") %>%
bind_rows(dens %>% add_entries("Macro/International")) %>%
ggplot(aes(count, year, fill = program_category)) +
geom_polygon() +
coord_cartesian(xlim = c(-0.09, 0.09))
cropped_plot
```
Adjust scales and labels
```{r}
count_plot_adj <- count_plot +
scale_fill_brewer(palette = "Set1") +
scale_y_discrete(
breaks = seq(1980, 2020, 10)
) +
labs(
x = element_blank(),
y = "Year",
fill = "Program"
)
croped_plot_adj <- cropped_plot +
scale_fill_brewer(palette = "Set1") +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = seq(1980, 2020, 10), minor_breaks = NULL) +
labs(
x = element_blank(),
y = "Year",
fill = "Program"
)
```
```{r}
library(patchwork)
arranged <- count_plot_adj + croped_plot_adj +
plot_annotation(
title = "Macro vs. Micro",
subtitle = "Comparing the Amount of Publications distributed by the NBER",
caption = "A TidyTuesday contribution by @rappa753"
) +
plot_layout(guides = "collect", heights = 1) &
theme_light() +
theme(
legend.position = "top",
text = element_text(size = 12, face = "bold")
)
arranged
```
Save plot
```{r}
ggsave(
"2021_w40_NBERpapers.png",
arranged,
width = 30,
height = 30 * 9 / 16,
units = "cm"
)
```