/
make_plots.R
executable file
·374 lines (305 loc) · 11.6 KB
/
make_plots.R
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env Rscript
# make_plots.R
# Henry J Schmale
# Log Analysis Plotting Script
required.packages <- c(
"rjson", "ggplot2", "dplyr", "lubridate", "reshape2", "scales", "tidyr"
)
new.packages <- required.packages[!(required.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) {
install.packages(new.packages, repos='http://cran.us.r-project.org')
}
suppressPackageStartupMessages(suppressWarnings({
library(rjson)
library(ggplot2)
library(dplyr)
library(lubridate)
library(reshape2)
library(scales)
library(tidyr)
}))
#############################################
# Some Knobs to twist and turn
#############################################
NUM_MOST_RECENT_POSTS <- 6
LAST_N_DAYS <- 30
bisect_date <- as.Date(today(), format="%Y-%m-%d") - LAST_N_DAYS
SEVEN_DAYS_AGO <- as.Date(today(), format="%Y-%m-%d") - 7
#############################################
# Begin primary code execution
#############################################
#options(stringsAsFactors = FALSE)
if (! interactive()) {
options(device = 'pdf')
pdf(width = 11, height = 8.5)
}
# Load the post tags file
tags <- rjson::fromJSON(file = 'posttags.json')
livePosts <- names(tags)
# For the Future to develop a better category stuff
buildPostToTagMapping <- function(posttags_json=tags) {
posts <- c()
tag_vec <- c()
for (postname in names(tags)) {
for (tag in tags[[postname]]) {
posts <- append(posts, postname)
tag_vec <- append(tag_vec, tag)
}
}
data.frame(
path = posts,
tag = tag_vec
)
}
# Load the Hit Counts
hitCounts <- read.csv(file = 'articleViews.csv', header = FALSE)
names(hitCounts) <- c('path', 'date', 'hits')
hitCounts$date <- as.Date(hitCounts$date)
# deadPostHit <- hitCounts[!hitCounts$path %in% livePosts,]
# Select only those posts which currently are live on my site.
livePostHit <- hitCounts[hitCounts$path %in% livePosts,]
livePostHit$pubDate=as.Date(substr(livePostHit$path,2,11))
livePostHit$yearPub <- year(livePostHit$pubDate)
#################################################
# Total Number Of Hits
#################################################
postTotalHitsAllTime <- livePostHit %>%
group_by(path) %>%
summarise(hits=sum(hits), .groups = 'drop')
ggplot(postTotalHitsAllTime, aes(y = path, x = hits, label = hits)) +
geom_bar(stat = 'identity', fill="lightblue") +
geom_text(size = 3) +
ggtitle("Total Views of Posts") +
scale_x_sqrt()
# Gets the most viewed posts of all time
getMostViewedAllTimePosts <- function(n=NUM_MOST_RECENT_POSTS) {
postTotalHitsAllTime %>% arrange(-hits) %>% top_n(n) %>% select(path)
}
#buildPostToTagMapping() %>%
# ggplot(aes(y=tag)) +
# geom_bar() +
# ggtitle("Number of Posts Under Tags")
#################################################
# Post Hits in the Last N Days (LAST_N_DAYS)
#################################################
livePostHit %>%
filter(date >= bisect_date) %>%
group_by(path) %>%
summarise(hits = sum(hits), .groups = 'drop') %>%
filter(hits > 1) %>%
ggplot(aes(y = path, x = hits, label = hits)) +
geom_bar(stat="identity") +
geom_text(size = 3, hjust = -1) +
ggtitle(paste("Post Hits in the Past", LAST_N_DAYS, "Days as of ", today())) +
scale_x_log10()
livePostHit %>%
filter(date >= SEVEN_DAYS_AGO) %>%
group_by(path) %>%
summarise(hits = sum(hits), .groups = 'drop') %>%
filter(hits > 1) %>%
ggplot(aes(y = path, x = hits, label=hits)) +
geom_bar(stat="identity") +
geom_text(size = 3, hjust = -1) +
theme(legend.position="bottom") +
ggtitle(paste("Post Hits in the Past", 7, "Days as of", today()))
overAllHistInLastNDays <- function(nDays, rollDays=3) {
from_count <- nDays + rollDays
from_date <- as.Date(today(), format="%Y-%m%-%d") - from_count
actual_from_date <- as.Date(today(), format="%Y-%m-%d") - nDays
hitsPerDay <- livePostHit %>%
filter(date >= from_date) %>%
group_by(date) %>%
summarise(hits = sum(hits), .groups='drop')
median_roll <- numeric(length(hitsPerDay$date) - rollDays)
for(i in seq_along(median_roll)){
median_roll[i] <- mean(hitsPerDay$hits[i:(i+rollDays)])
}
#median_roll = c(numeric(rollDays), median_roll)
hitsPerDay <- hitsPerDay %>%
filter(date >= actual_from_date) %>%
group_by(date) %>%
summarise(hits = sum(hits), .groups='drop')
hitsPerDay$median_roll <- median_roll
totalHits <- sum(hitsPerDay$hits)
ggplot(hitsPerDay, aes(x = date, y = hits, label=hits)) +
geom_bar(stat='identity') +
geom_line(aes(x=date, y=median_roll), color="red") +
geom_text(size=3, vjust=-1) +
theme(axis.text = element_text(angle=75, hjust = 1)) +
ggtitle(paste("Hit Counts in the Past", nDays, "Days as of ", today(), " (total = ", totalHits, ")"))
}
overAllHistInLastNDays(7)
overAllHistInLastNDays(30)
overAllHistInLastNDays(60)
livePostHit %>%
filter(date >= bisect_date) %>%
group_by(path, date) %>%
summarize(hits = sum(hits), .groups = 'drop') %>%
ggplot(aes(x = path, y = date, fill = hits)) +
geom_tile() +
coord_flip() +
geom_text(aes(label = hits)) +
ggtitle("When posts were hit in the last 30 days") +
scale_fill_gradient(low='blue', high='red', trans='sqrt')
#################################################
# Daily Hits
#################################################
daily_hits <- livePostHit %>%
group_by(date) %>%
summarise(daily=sum(hits), .groups = 'drop') %>%
arrange(date) %>%
mutate(total=cumsum(daily),
year=year(date),
yearday=strftime(date, "%j"),
quarter=floor_date(date, "month")
)
daily_hits$yearday <- as.numeric(daily_hits$yearday)
daily_hits$year <- as.factor(daily_hits$year)
ggplot(daily_hits, aes(x = yearday, y = daily, fill=year)) +
geom_bar(stat='identity') +
facet_grid(year ~ .) +
ggtitle("Daily Post Hit Counts") +
scale_y_sqrt()
ggsave("facet_year_days.png")
daily_hits %>%
group_by(year) %>%
arrange(date) %>%
mutate(cs = cumsum(daily)) %>%
ggplot(aes(x = yearday, y = cs, color=year)) +
geom_line() +
ggtitle('Cummulative Daily Post Hits Year Over Year')
ggplot(daily_hits, aes(x = date, y = daily, group = quarter)) +
geom_boxplot() +
scale_y_sqrt() +
ggtitle("Box and Whisker of Total Daily Views Grouped By Quarter (sqrt scale)")
ggsave("bw.png")
#################################################
# Monthly Hits
#################################################
hits_per_month <- livePostHit %>%
group_by(month = floor_date(date, 'month')) %>%
summarise(hits = sum(hits))
ggplot(hits_per_month, aes(x = month, y = hits, label = hits)) +
geom_bar(stat = 'identity') +
geom_text(size = 3, vjust = -1) +
ggtitle("Post Hits Per Month")
#################################################
# Grouped By Quarter, since it's getting really
# quite big my
#################################################
summarizedPostHit <- livePostHit %>%
group_by(path, dates=floor_date(date, "quarter")) %>%
summarize(hits=sum(hits), pubDate=as.Date(substr(path,2,11)), .groups = 'drop')
ggplot(summarizedPostHit, aes(x = path, y = dates, fill = hits)) +
geom_tile() +
coord_flip() +
geom_text(aes(label = hits)) +
ggtitle("Post Hits Over Time Grouped By Quarter") +
scale_fill_continuous(low='blue', high='red', trans='log10')
#################################################
# Grouped By Day of Week
# Ex. Sat, Sun .... Friday
#################################################
weekdayPostHit <- livePostHit %>%
group_by(path, weekday = wday(date, label = TRUE)) %>%
summarize(hits=sum(hits))
ggplot(weekdayPostHit, aes(x = path, y = weekday, fill = hits)) +
geom_tile() +
coord_flip() +
geom_text(aes(label = hits)) +
ggtitle("Post Hits Grouped By Day Of Week - My Blog") +
scale_fill_continuous(low='blue', high='red', trans='log10')
#################################################
# Grouped By Day of Month
#################################################
monthdayPostHit <- livePostHit %>%
group_by(path, monthday = mday(date)) %>%
summarize(hits=sum(hits))
ggplot(monthdayPostHit, aes(x = path, y = monthday, fill = hits)) +
geom_tile() +
coord_flip() +
geom_text(aes(label = hits)) +
ggtitle("Post Hits Grouped By Day Of Month - My Blog") +
scale_fill_continuous(low='blue', high='red', trans='log10')
#################################################
# Post view activity normalized for time since
# publication. The pubDate is known by the path
# name.
#################################################
normHitsSincePub <- livePostHit %>%
group_by(path) %>%
arrange(date) %>%
mutate(
hitsSincePub = cumsum(hits),
pubDate = as.Date(substr(path, 2, 11)),
daysSincePub = as.integer(date - pubDate)
)
normHitsSincePub %>%
slice_max(order_by = hitsSincePub, n=1) %>%
ggplot(aes(x=daysSincePub, y=hitsSincePub)) +
scale_x_log10() +
geom_point() +
ggtitle(paste("Days Since Pub vs Total Hits at Current Views as of", today()))
getMostRecentPosts <- function(n=NUM_MOST_RECENT_POSTS) {
k <- data.frame(
post_names = livePosts,
pubDate = substr(livePosts, 2, 11)
)
k <- k %>% arrange(desc(pubDate)) %>% top_n(n)
k$post_names
}
N_MOST_RECENT_POSTS <- getMostRecentPosts()
most_recent_posts_data <- normHitsSincePub %>%
filter(path %in% N_MOST_RECENT_POSTS, daysSincePub < 366)
normHitsStdDev <- normHitsSincePub %>%
complete(date = seq.Date(min(date), max(date), by="day")) %>%
fill(hitsSincePub, pubDate) %>%
mutate(daysSincePub = as.integer(date - pubDate)) %>%
group_by(daysSincePub) %>%
summarise(
ymin = min(hitsSincePub),
ymax = max(hitsSincePub),
hsp_mean = mean(hitsSincePub),
hsp_stdev = mad(hitsSincePub, center = mean(hitsSincePub)),
cnt = n()
)
normHitsStdDev %>%
filter(daysSincePub < 31) %>%
inner_join(most_recent_posts_data, by="daysSincePub") %>%
ggplot(aes(x = daysSincePub, y = hitsSincePub)) +
geom_line(aes(color=path)) +
geom_point(aes(shape=path, color=path)) +
geom_ribbon(aes(ymin=hsp_mean - hsp_stdev, ymax=hsp_mean + hsp_stdev), alpha=0.1) +
ggtitle(paste(NUM_MOST_RECENT_POSTS, "Most Recent Posts and Performance in First 30 Days of Publication")) +
theme(legend.position="bottom") +
guides(colour = guide_legend(nrow = 3)) +
coord_cartesian(xlim = c(0,31))
normHitsStdDev %>%
filter(daysSincePub < 366) %>%
left_join(most_recent_posts_data, by="daysSincePub") %>%
ggplot(aes(x = daysSincePub, y = hitsSincePub)) +
geom_line(aes(color=path)) +
geom_point(aes(shape=path, color=path)) +
geom_ribbon(aes(ymin=hsp_mean - hsp_stdev, ymax=hsp_mean + hsp_stdev), alpha=0.1) +
ggtitle(paste(NUM_MOST_RECENT_POSTS, "Most Recent Posts and Performance in First Year of Publication")) +
theme(legend.position="bottom") +
guides(colour = guide_legend(nrow = 2)) +
coord_cartesian(xlim=c(0,366))
# At the end of the first year of publication. What were the most viewed posts? Display them against the average views.
normHitsSincePub %>%
filter(daysSincePub < 366) %>%
group_by(path) %>%
summarise(max_hits=max(hitsSincePub)) %>%
arrange(-max_hits) %>%
top_n(NUM_MOST_RECENT_POSTS) %>%
inner_join(normHitsSincePub) %>%
filter(daysSincePub < 366) %>%
inner_join(normHitsStdDev) %>%
ggplot(aes(x = daysSincePub, y = hitsSincePub)) +
geom_line(aes(color=path)) +
geom_point(aes(shape=path, color=path)) +
geom_ribbon(aes(ymin=hsp_mean - hsp_stdev, ymax=hsp_mean + hsp_stdev), alpha=0.1) +
ggtitle(paste("Hits of ", NUM_MOST_RECENT_POSTS, " most viewed posts in their first year")) +
theme(legend.position="bottom") +
guides(colour = guide_legend(nrow = 3)) +
coord_cartesian(xlim=c(0,366))