-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
278 lines (247 loc) · 13 KB
/
README.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
---
title: "The Emotions of Jane Austen"
output:
rmarkdown::github_document:
theme: paper
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# load packages
library(plyr)
library(stringr)
library(data.table)
library(ggplot2)
library(ggstream)
library(tidytext)
library(janeaustenr)
library(extrafont)
library(showtext)
library(dplyr)
library(gridExtra)
library(grid)
library(cowplot)
# load Google font for visualisations
font_add_google("Noto Serif", "Noto Serif")
showtext_auto()
# format the data from {janeaustenr} for analysis
tidy_books <- austen_books() %>%
group_by(book) %>%
mutate(
linenumber = row_number(),
chapter = cumsum(str_detect(text,
regex("^chapter [\\divxlc]",
ignore_case = TRUE)))) %>%
ungroup() %>%
unnest_tokens(word, text)
# import NRC Emotion Lexicon
nrc_all <- get_sentiments("nrc")
# list of unique emotions
sentiments <- unique(nrc_all$sentiment)
# identify words that appear in the emotion lexicon
all_books <- tidy_books %>%
filter() %>%
mutate(joy = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "joy"]),
trust = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "trust"]),
surprise = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "surprise"]),
anticipation = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "anticipation"]),
sadness = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "sadness"]),
fear = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "fear"]),
anger = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "anger"]),
disgust = as.numeric(word %in% nrc_all$word[nrc_all$sentiment == "disgust"]))
# reshape the data frame
all_books_long <- melt(setDT(all_books), id.vars = c("book","linenumber","chapter","word"), variable.name = "emotion")
# round lines to nearest 30 (approximately 1 page) to help remove noise
all_books_long_10 <- all_books_long %>% mutate(linenumber = round_any(linenumber, 30, f = ceiling))
# aggregate by "linenumber" groups
all_books_byline <- all_books_long_10 %>%
group_by(book, linenumber, emotion) %>%
summarise(value = sum(value)) %>%
filter(value > 0) %>%
group_by(book, linenumber) %>%
mutate(test = value / sum(value) * n())
# add ids for each book
all_books_byline <- all_books_byline %>% group_by(book) %>% mutate(id = row_number())
# add unique identifier
all_books_byline_dt <- all_books_byline %>% mutate(book_line = paste0(book, linenumber))
all_books_byline_dt <- as.data.table(all_books_byline_dt)
# identify the 2 most common emotions in each linenumber group
all_books_byline_dt <- all_books_byline_dt[all_books_byline_dt[, .I[value >= max( value[value!=max(value)] )], by=book_line]$V1]
# apply an equal value to these emotions
all_books_byline_dt$value <- 1
# apply calculation to adjust appearance of the streamgraph and remove extreme spikes
all_books_byline_dt <- all_books_byline_dt %>%
group_by(book, emotion) %>%
mutate(test = n() / value)
# create custom colour palette
pal <- c("joy" = "#6b9a3e",
"trust" = "#25654d",
"surprise" = "#efd041",
"anticipation" = "#34478b",
"sadness" = "#4175a5",
"fear" = "#ed9953",
"anger" = "#9b262c",
"disgust" = "#807094")
# create plot for Sense & Sensibility
sense_p <- all_books_byline_dt %>% filter(book == "Sense & Sensibility") %>%
ggplot(aes(x = id, y = test)) +
geom_stream(bw = 0.6, n_grid = 16561, aes(fill = emotion), alpha = 0.96) +
scale_fill_manual(values = pal) +
ggtitle("S E N S E & S E N S I B I L I T Y") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.text = element_text(size = 24),
legend.title = element_blank(),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.title = element_text(color = "#69543f", size = 18, hjust = 0.5),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#69543f"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family = "Noto Serif", face = "bold", color = "#69543f"))
# create plot for Emma
emma_p <- all_books_byline_dt %>% filter(book == "Emma") %>%
ggplot(aes(x = id, y = test)) +
geom_stream(bw = 0.6, n_grid = 16561, aes(fill = emotion), alpha = 0.96) +
scale_fill_manual(values = pal) +
ggtitle("E M M A") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.text = element_text(size = 24),
legend.key = element_blank(),
legend.title = element_blank(),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.title = element_text(color = "#69543f", size = 18, hjust = 0.5),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#69543f"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family = "Noto Serif", face = "bold", color = "#69543f"))
# create plot for Northanger Abbey
north_p <- all_books_byline_dt %>% filter(book == "Northanger Abbey") %>%
ggplot(aes(x = id, y = test)) +
geom_stream(bw = 0.6, n_grid = 16561, aes(fill = emotion), alpha = 0.96) +
scale_fill_manual(values = pal) +
ggtitle("N O R T H A N G E R A B B E Y") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.text = element_text(size = 24),
legend.key = element_blank(),
legend.title = element_blank(),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.title = element_text(color = "#69543f", size = 18, hjust = 0.5),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#69543f"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family = "Noto Serif", face = "bold", color = "#69543f"))
# create plot for Mansfield Park
mansfield_p <- all_books_byline_dt %>% filter(book == "Mansfield Park") %>%
ggplot(aes(x = id, y = test)) +
geom_stream(bw = 0.6, n_grid = 16561, aes(fill = emotion), alpha = 0.96) +
scale_fill_manual(values = pal) +
ggtitle("M A N S F I E L D P A R K") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.text = element_text(size = 24),
legend.title = element_blank(),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.title = element_text(color = "#69543f", size = 18, hjust = 0.5),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#69543f"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family = "Noto Serif", face = "bold", color = "#69543f"))
# create plot for Pride & Prejudice
pride_p <- all_books_byline_dt %>% filter(book == "Pride & Prejudice") %>%
ggplot(aes(x = id, y = test)) +
geom_stream(bw = 0.6, n_grid = 16561, aes(fill = emotion), alpha = 0.96) +
scale_fill_manual(values = pal) +
ggtitle("P R I D E & P R E J U D I C E") +
labs(caption = "Sentiment Analysis Data: NRC Word-Emotion Association Lexicon / saifmohammad.com | Visualisation: @filmicaesthetic") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.text = element_text(size = 18),
legend.title = element_blank(),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.title = element_text(color = "#69543f", size = 18, hjust = 0.5),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#69543f"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family = "Noto Serif", face = "bold", color = "#69543f"))
# build styled legend with a bar chart
leg <- data.frame(EMOTION = as.factor(c("joy", "trust", "surprise", "anticipation", "sadness", "fear", "anger", "disgust")), value = c(1, 1, 1, 1, 1, 1, 1, 1))
leg$EMOTION <- factor(leg$EMOTION, levels = leg$EMOTION)
# plot legend
hist <- ggplot(leg, aes(x = EMOTION, y = value, fill = EMOTION)) +
geom_bar(stat = "identity") +
geom_text(aes(y = 0.502, label = toupper(EMOTION)), angle = 90, family = "Noto Serif", color = c("#b07e13", "#f2ddb1", "#b07e13", "#f2ddb1", "#b07e13", "#b07e13", "#f2ddb1", "#b07e13"), size = 8) +
geom_text(aes(y = 0.498, label = toupper(EMOTION)), angle = 90, family = "Noto Serif", color = c("#403020", "#b07e13", "#403020", "#b07e13", "#403020", "#403020", "#b07e13", "#403020"), size = 8) +
geom_text(aes(y = 0.95, label = "JANE AUSTEN"), family = "Noto Serif", color = "#f2ddb1", size = 1.5) +
geom_text(aes(y = 0.95001, label = "JANE AUSTEN"), family = "Noto Serif", color = "#291d12", size = 1.5) +
geom_text(aes(y = 0.05, label = "SIMON & SCHUSTER"), family = "Noto Serif", color = "#f2ddb1", size = 1.1) +
geom_text(aes(y = 0.05001, label = "SIMON & SCHUSTER"), family = "Noto Serif", color = "#291d12", size = 1.1) +
scale_fill_manual(values = pal) +
ggtitle(toupper("The Emotions of Jane Austen"), subtitle = "A visual interpretation of the emotion of language used in Jane Austen novels.") +
theme(panel.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
plot.background = element_rect(fill = "#f2f0ed", color = "#f2f0ed"),
panel.grid.major = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
plot.margin = margin(r = 4, l = 4, unit = "cm"),
legend.position = "none",
legend.box.margin = margin(r = 0, l = 0),
panel.grid.minor = element_blank(),
plot.subtitle = element_text(hjust = 0.5, size= 18, margin = margin(t = 5, b = 5, unit = "pt")),
plot.title = element_text(angle = 0, family = "Noto Serif", color = "#69543f", size = 24, face="bold", hjust = 0.5, margin = margin(t = 12, unit = "pt")),
axis.text = element_blank(),
strip.background = element_blank(),
strip.text = element_text(color = "#80664d"),
axis.title = element_blank(),
axis.ticks = element_blank(),
text=element_text(family="Noto Serif", face = "bold", color = "#69543f"))
# combine all the plots into one
plot <- plot_grid(hist, sense_p, emma_p, north_p, mansfield_p, pride_p, nrow = 6, rel_heights = c(1.2, 1, 1, 1, 1, 1))
```
This personal project explores the literature of Jane Austen novels, provided by Julia Silge's <a href="https://github.com/juliasilge/janeaustenr">janeaustenr</a> package in R, using the <a href="https://saifmohammad.com/WebPages/NRC-Emotion-Lexicon.htm">NRC Word-Emotion Association Lexicon</a> by Saif Mohammad. The lexicon is used to identify words associated with a range of 8 emotions: joy, trust, surprise, anticipation, sadness, fear, anger, and disgust.
The project aims to capture a visual interpretation of the emotional journey in each novel with a single visualisation.
``` {r main_plot, echo = FALSE, warning = FALSE, message = FALSE, fig.width = 13, fig.height = 20}
plot
```
## Files
'the_emotions_of_jane_austen.R' : <a href="https://github.com/filmicaesthetic/JaneAustenStreamgraphs/blob/main/scripts/the_emotions_of_jane_austen.R">view code</a>
## Process
* Load the NRC Word-Emotion Association Lexicon and Jane Austen novels datasets and prepare for further analysis.
* Count occurrences of words with an emotional association for each line of each book.
* Group lines approximately into pages (30 lines) to help reduce noise in the final visualisation.
* Identify the two emotions with strongest association for each group.
* Plot the results for each novel in a streamgraph.
* Create grid of all plots and bookshelf-style legend.