This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.Rmd
156 lines (128 loc) · 4.56 KB
/
dashboard.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
---
title: "\\#Solar"
output:
flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(flexdashboard)
library(twitteR)
library(dplyr)
library(ggplot2)
library(lubridate)
library(network)
library(sna)
library(qdap)
library(tm)
source('twitterCreds.R')
setup_twitter_oauth(api_key, api_secret, access_token, token_secret)
tw <- searchTwitter('#Solar', n=1000, since='2016-05-01')
d <- twListToDF(tw)
```
Column {.tabset}
-----------------------------------------------------------------
### When do people tweet?
```{r}
d$created = with_tz(d$created, 'America/Denver')
ggplot(d, aes(created)) +
geom_density(aes(fill = isRetweet), alpha = .5) +
scale_fill_discrete(guide = 'none') +
xlab('All tweets')
```
### What platforms are people using?
```{r}
par(mar = c(3, 3, 3, 2))
d$statusSource = substr(d$statusSource,
regexpr('>', d$statusSource) + 1,
regexpr('</a>', d$statusSource) - 1)
d_tab <- sort(table(d$statusSource))
dotchart(d_tab, cex=0.5)
mtext('Number of tweets posted by platform')
```
### Emotional Valence
```{r}
# Split into retweets and original tweets
sp = split(d, d$isRetweet)
orig = sp[['FALSE']]
# Extract the retweets and pull the original author's screenname
rt = mutate(sp[['TRUE']], sender = substr(text, 5, regexpr(':', text) - 1))
pol = gsub('[[:punct:]]+\\s+', ' ', orig$text)
pol = iconv(gsub('\\n+', '', pol), to='ASCII', sub='')
pol = gsub('https?:\\/\\/[[:alnum:]]+\\.\\w{2,6}\\/[[:alnum:]]*',
'', pol)
pol = polarity(pol)
orig$emotionalValence = pol$all$polarity
# As reality check, what are the most and least positive tweets
# orig$text[which.max(orig$emotionalValence)]
orig %>%
ggplot(aes(created, emotionalValence)) +
geom_point() +
geom_smooth(span = .5)
```
### Do happier tweets get retweeted more?
```{r}
ggplot(orig, aes(x = emotionalValence, y = retweetCount)) +
geom_point(position = 'jitter') +
geom_smooth()
```
### Emotional Content
```{r}
polWord =
sapply(pol, function(p) {
words = c(positiveWords = paste(p$pos.words, collapse = ' '),
negativeWords = paste(p$neg.words, collapse = ' '))
gsub('-', '', words) # Get rid of nothing found's "-"
}) %>%
apply(1, paste, collapse = ' ') %>%
stripWhitespace() %>%
strsplit(' ')
polWord[[1]] <- gsub('c\\(\\"|\\"|\\)|,', '', polWord[[1]])
polWord[[2]] <- gsub('c\\(\\"|\\"|\\)|,', '', polWord[[2]])
polWordTables <- sapply(polWord, table)
for(i in 1:length(polWordTables)) {
polWordTables[[i]] <-
sort(polWordTables[[i]])[(length(polWordTables[[i]])-30):(length(polWordTables[[i]]))]
}
par(mfrow = c(1, 2))
invisible(
lapply(1:2, function(i) {
dotchart(sort(polWordTables[[i]]), cex = .5)
mtext(names(polWordTables)[i])
}))
```
### Emotionally associated non-emotional words
```{r}
pol = iconv(gsub('\\n+', '', pol), to='ASCII', sub='')
polSplit = split(orig, sign(orig$emotionalValence))
polText = sapply(polSplit, function(df) {
paste(tolower(iconv(df$text, to = 'ASCII', sub='')), collapse = ' ') %>%
gsub(' (http|@)[^[:blank:]]+', '', .) %>%
gsub('[[:punct:]]', '', .)
}) %>%
structure(names = c('negative', 'neutral', 'positive'))
# remove emotive words
polText['negative'] = removeWords(polText['negative'], names(polWordTables$negativeWords))
polText['positive'] = removeWords(polText['positive'], names(polWordTables$positiveWords))
# Make a corpus by valence and a wordcloud from it
corp = VCorpus(VectorSource(polText))
names(corp) <- c('negative', 'neutral', 'positive')
col3 = RColorBrewer::brewer.pal(3, 'Accent') # Define some pretty colors, mostly for later
wordcloud::comparison.cloud(as.matrix(TermDocumentMatrix(corp)),
max.words = 100, min.freq = 2, random.order=FALSE,
rot.per = 0, vfont = c("sans serif", "plain"))
```
### Who is retweeting whom?
```{r}
# Adjust retweets to create an edgelist for network
el = as.data.frame(cbind(sender = tolower(rt$sender),
receiver = tolower(rt$screenName)))
el = count(el, sender, receiver)
rtnet = network(el, matrix.type = 'edgelist', directed = TRUE,
ignore.eval = FALSE, names.eval = 'num')
# Get names of only those who were retweeted to keep labeling reasonable
vlabs = rtnet %v% 'vertex.names'
vlabs[degree(rtnet, cmode = 'outdegree') == 0] = NA
par(mar = c(0, 0, 3, 0))
plot(rtnet, label = vlabs, label.pos = 5, label.cex = .8,
vertex.cex = log(degree(rtnet)) + .5, vertex.col = col3[1],
edge.lwd = 'num', edge.col = 'gray70', main = '\\#Solar Retweet Network')
```