PAHR Sentiment Network

In my previous post on sentiment analysis I used a dataframe to plot the trajectory of sentiment across the novel Picnic at Hanging Rock. In this post I will use the same dataframe of non-unique, non-stop, greater than 3 character words (red line from an earlier post) to create a network of associated words. Words can be grouped by sentence, paragraph, or chapter. I have already removed stop words and punctuation, so I will use my previous grouping of every 15 words in the order they appear in the novel. Looking at my dataframe rows 10 to 20:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> d2[10:20,]
chpt word sentiment lexicon group
10 1 silent positive bing 1
11 1 ridiculous negative bing 1
12 1 supported positive bing 1
13 1 bust negative bing 1
14 1 tortured negative bing 1
15 1 hopeless negative bing 1
16 1 misfit negative bing 2
17 1 clumsy negative bing 2
18 1 gold positive bing 2
19 1 suitable positive bing 2
20 1 insignificant negative bing 2
>

You can see the column “group” has grouped every 15 words. First I create a table of word cooccurences using the pair_count function, then I use ggraph to create the network graph. The number of cooccurences are reflected in edge opacity and width. At the time of this writing, ggraph was still in beta and had to be downloaded from github and built locally. The igraph package provides the graph_from_data_frame function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

library(igraph)
library(ggraph)

word_cooccurences <- d2 %>%
pair_count(group, word, sort = TRUE)

set.seed(1900)
word_cooccurences %>%
filter(n >=4) %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(aes(edge_alpha = n, edge_width = n)) +
geom_node_point(color = "darkslategray4", size = 5) +
geom_node_text(aes(label = name), vjust = 1.8) +
ggtitle(expression(paste("Word Network ",
italic("Picnic at Hanging Rock")))) +
theme_void()

Lets regroup every 25 words:

1
2
3

d2$group <- sort( c(rep(1:120, 25),rep(121,19)))

And now include only words with 5 occurences or more:

1
2

..... filter(n >=5) %>% ....
Share