-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathplot.R
More file actions
195 lines (188 loc) · 7.39 KB
/
plot.R
File metadata and controls
195 lines (188 loc) · 7.39 KB
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
rm(list=ls(all=TRUE))
library(Hmisc) # list.tree
library(rlist) # list.map
library(rjson)
library(ggplot2)
calc.percentage <- function (data) {
length = length(data)
names = names(data[[1]])
ret = list()
for (name in names) {
d = list.apply(data, function (x) {
if (is.null(unlist(x[name], use.names=FALSE))) {
return(0)
} else {
return(x[name])
}
})
ret[name] = (sum(unlist(d)))/length
}
return(ret)
}
calc.sentiment <- function (data) {
polarity = list.map(data, return(polarity))
polarity.result = calc.percentage(polarity)
return(list(polarity=polarity.result))
}
calc.count <- function (data) {
retweets = sum(unlist(list.map(data, return(retweets))))
favorites = sum(unlist(list.map(data, return(favorites))))
return(list(tweets = length(data), retweets = retweets, favorites = favorites))
}
gen.dataset <- function (file.jsons) {
ret = list()
for (file.json in file.jsons) {
## calcData = list(date=file.name,
## polarity=calc.sentiment(json, 'polarity'),
## intensity=calc.sentiment(json, 'intensity'))
calcData = list.merge(list(data=file.json$date), calc.sentiment(file.json$data), calc.count(file.json$data))
ret = list.append(ret, calcData)
}
return(ret)
}
load.json <- function (file.names) {
ret = list()
for (file.name in file.names) {
json = load.jsonFile(file.name)
tmp = list(date=file.name, data=json)
ret = list.append(ret, tmp)
}
return(ret)
}
file.list = system("ls ./results | sed 's/\\.json//g'", intern = TRUE)
plot.sentiment <- function (dataset) {
names = unlist(list.map(dataset, return(date)))
names = c(1:length(dataset))
pos = unlist(list.map(dataset, return(polarity$pos)))
neg = unlist(list.map(dataset, return(polarity$neg)))
neut = unlist(list.map(dataset, return(polarity$neut)))
plot(x=names, y=0.5, ylim=c(0:1), type="l", col="blue")
lines(pos, type="l", col="blue")
lines(neg, type="l", col="red")
lines(neut, type="l", col="gray")
}
ggplot.sentiment <- function (dataset) {
# 감성그래프
names = as.Date(unlist(list.map(dataset, return(data))))
pos = unlist(list.map(dataset, return(polarity$pos)))
neg = unlist(list.map(dataset, return(polarity$neg)))
neut = unlist(list.map(dataset, return(polarity$neut)))
df = data.frame(
x = names,
middle = pos*0+0.5,
pos = pos,
pos.label = 'Positive',
neg = neg,
neg.label = 'Negative',
neut = neut,
neut.label = 'Neutral'
)
color.pos = "blue"
color.neg = "red"
color.neut = "green"
size.dot = 0
gp = ggplot(df, aes(x, middle)) ## + geom_line()
gp + ylim(0, 0.5) +
geom_point(aes(x, pos), color = color.pos, size = size.dot) + geom_line(aes(x, pos), color = color.pos) +
geom_point(aes(x, neg), color = color.neg, size = size.dot) + geom_line(aes(x, neg), color = color.neg) +
geom_point(aes(x, neut), color = color.neut, size = size.dot) + geom_line(aes(x, neut), color = color.neut) +
scale_x_date(date_labels = "%m-%d", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 270, hjust = 1))
## theme(axis.text.x = element_blank(),
## axis.ticks = element_blank())
}
ggplot.tweets <- function (dataset) {
# 트윗 수 그래프
names = as.Date(unlist(list.map(dataset, return(data))))
tw = unlist(list.map(dataset, return(tweets)))
rt = unlist(list.map(dataset, return(retweets)))
## fv = unlist(list.map(dataset, return(favorites)))
df = data.frame(
x = names,
pos = tw,
pos.label = 'Positive',
neg = rt,
neg.label = 'Negative'
## neut = fv,
## neut.label = 'Neutral',
)
color.pos = "blue"
color.neg = "red"
color.neut = "green"
size.dot = 0
gp = ggplot(df) ## + geom_line()
gp + geom_point(aes(x, pos), color = color.pos, size = size.dot) + geom_line(aes(x, pos), color = color.pos) +
geom_point(aes(x, neg), color = color.neg, size = size.dot) + geom_line(aes(x, neg), color = color.neg) +
## geom_point(aes(x, neut), color = color.neut, size = size.dot) + geom_line(aes(x, neut), color = color.neut) +
scale_x_date(date_labels = "%m-%d", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 270, hjust = 1))
## theme(axis.text.x = element_blank(),
## axis.ticks = element_blank())
}
ggplot.issue <- function (dataset) {
names = as.Date(unlist(list.map(dataset, return(data))))
tw = unlist(list.map(dataset, return(tweets)))
rt = unlist(list.map(dataset, return(retweets)))
## fv = unlist(list.map(dataset, return(favorites)))
pos = unlist(list.map(dataset, return(polarity$pos)))
neg = unlist(list.map(dataset, return(polarity$neg)))
neut = unlist(list.map(dataset, return(polarity$neut)))
is = (tw+rt)*(abs(pos-neg)+0.00001)
diff = is-c(0, is[1:length(is)-1])
df = data.frame(
x = names,
pos = is,
pos.label = 'Positive',
neg = tw,
neg.label = 'Negative',
neut = neut,
neut.label = 'Neutral',
diff = diff
)
color.pos = "blue"
color.neg = "red"
color.neut = "green"
size.dot = 0
gp = ggplot(df) ## + geom_line()
gp + geom_point(aes(x, is), color = color.pos, size = size.dot) + geom_line(aes(x, is), color = color.pos) +
geom_bar(stat = "identity", aes(x, diff), color = color.neg) +
## geom_point(aes(x, neg), color = color.neg, size = size.dot) + geom_line(aes(x, neg), color = color.neg) +
## geom_point(aes(x, neut), color = color.neut, size = size.dot) + geom_line(aes(x, neut), color = color.neut) +
scale_x_date(date_labels = "%m-%d", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 270, hjust = 1))
## theme(axis.text.x = element_blank(),
## axis.ticks = element_blank())
}
ggplot.slope <- function (dataset) {
# 트윗 수 변화량 그래프
names = as.Date(unlist(list.map(dataset, return(data))))
pos = unlist(list.map(dataset, return(polarity$pos)))
neg = unlist(list.map(dataset, return(polarity$neg)))
neut = unlist(list.map(dataset, return(polarity$neut)))
t = unlist(list.map(dataset, return(tweets)))
rt = unlist(list.map(dataset, return(retweets)))
fv = unlist(list.map(dataset, return(favorites)))
re = (t+rt+fv)*(abs(pos-neg)+0.0001)
df = data.frame(
x = names,
pos = t,
pos.label = 'Positive',
neg = rt,
neg.label = 'Negative',
neut = fv,
neut.label = 'Neutral'
)
color.pos = "blue"
color.neg = "red"
color.neut = "green"
size.dot = 0
gp = ggplot(df) ## + geom_line()
gp + geom_point(aes(x, pos), color = color.pos, size = size.dot) + geom_line(aes(x, pos), color = color.pos) +
geom_point(aes(x, neg), color = color.neg, size = size.dot) + geom_line(aes(x, neg), color = color.neg) +
geom_point(aes(x, neut), color = color.neut, size = size.dot) + geom_line(aes(x, neut), color = color.neut) +
scale_x_date(date_labels = "%m-%d", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 270, hjust = 1))
## theme(axis.text.x = element_blank(),
## axis.ticks = element_blank())
}
ggplot.sentiment(dataset)