-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.Rmd
294 lines (253 loc) · 8.2 KB
/
report.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
title: "Trending Youtube Dashboard Report"
output:
html_document:
theme:
bootswatch = "journal",
base_font = bslib::font_google("Assistant"))
sysfonts::font_add_google("Assistant")
thematic_shiny(font = "auto")
showtext::showtext_auto()
params:
daterange: NA
rm_outliers: NA
boxplotdist: NA
barplotcat: NA
bubbleCats: NA
num_tags: NA
vid_category: NA
representation_format: NA
---
```{r setup, include=FALSE}
# Loading in the data
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(shiny)
library(bslib)
library(shinyWidgets)
library(shinydashboard)
library(ggplot2)
library(forcats)
library(plotly)
library(thematic)
library(scales)
library(rlang)
library(stringr)
library(showtext)
library(sysfonts)
library(packcircles)
library(lubridate)
library(rsconnect)
library(rmarkdown)
data <- read.csv("data/processed/CA_youtube_trending_data_processed.csv")
# Filter data by date universally
data_by_date <- data |>
dplyr::filter(trending_date > params$daterange[1] & trending_date < params$daterange[2])
# Filter out outliers if toggled
if (params$rm_outliers == TRUE) {
data <- data_by_date |>
dplyr::filter(!!rlang::sym(params$boxplotdist) < quantile(!!rlang::sym(params$boxplotdist), 0.9))
boxplot_data <- data
} else {
boxplot_data <- data_by_date
}
boxplot_options <- c(
"Comments" = "comment_count",
"Dislikes" = "dislikes",
"Likes" = "likes",
"Views" = "view_count"
)
interval_choices <- c("Day of Week" = "publish_wday",
"Month of Year" = "publish_month",
"Time of Day" = "publish_hour")
barplot_colours <- setNames(
unique(data$categoryId),
c("#35618f", "#2dadb8", "#2a6a45", "#0df38f", "#93c680",
"#21a708", "#bce333", "#7e2b19", "#de592e", "#fcd107",
"#b08965", "#d4d4d4", "#5c51b1", "#cc99d9", "#a53bb7")
)
boxplot_colours <- setNames(
c("#35618f", "#2dadb8", "#2a6a45", "#0df38f", "#93c680",
"#21a708", "#bce333", "#7e2b19", "#de592e", "#fcd107",
"#b08965", "#d4d4d4", "#5c51b1", "#cc99d9", "#a53bb7"),
unique(data$categoryId)
)
```
```{r}
# Plots
# Video Counter
video_count_box <-
shinydashboard::valueBox(
span(icon("video"), length(unique(data_by_date$video_id))),
subtitle = "Total Video Count"
)
# video_count_box
# Channel Counter
channel_count_box <- shinydashboard::valueBox(span(icon("user"), length(unique(
data_by_date$channelId
))),
subtitle = "Total Channel Count")
# channel_count_box
```
## Distribution Boxplots
**Use your cursor to select an area to zoom or hover to gain additional information**
```{r}
paste("Exclude Outliers (>0.9 Quantile):", params$rm_outliers)
paste("Distribution Metric:", params$boxplotdist)
```
```{r}
# Category Boxplot
box_plot <- boxplot_data |>
dplyr::arrange(trending_date) |>
dplyr::distinct(video_id, .keep_all = TRUE) |> # keep most recent data point for accurate tracking (no aggregating the same video)
ggplot2::ggplot() +
ggplot2::geom_boxplot(
aes(
x = forcats::fct_reorder(categoryId, !!rlang::sym(params$boxplotdist)),
y = !!rlang::sym(params$boxplotdist),
fill = categoryId
)
) +
ggplot2::labs(
y = names(boxplot_options[which(boxplot_options == params$boxplotdist)]),
x = 'Category',
) +
ggplot2::scale_y_continuous(labels = scales::label_number(scale_cut = cut_short_scale()), breaks = scales::breaks_pretty(n = 5)) +
ggplot2::scale_fill_manual(values = boxplot_colours) +
ggplot2::guides(fill = FALSE) +
ggplot2::theme(axis.title.x = element_text(size = 14, face = "bold"),
axis.title.y = element_text(size = 14, face = "bold")) +
ggplot2::coord_flip()
# Display the plot
plotly::ggplotly(box_plot, tooltip = "text")
```
## Trending Videos by Channel
**Hover your cursor over the graph to gain additional information**
```{r}
paste("Category:", params$barplotcat)
```
```{r}
# Channel Barplot
bar_plot <- data_by_date |>
dplyr::filter(categoryId == params$barplotcat) |>
dplyr::group_by(channelId, channelTitle) |>
dplyr::summarise(video_count = length(unique(video_id))) |>
dplyr::arrange(dplyr::desc(video_count)) |>
dplyr::ungroup() |>
dplyr::slice(1:10) |>
ggplot2::ggplot(aes(
x = video_count,
y = reorder(channelTitle, video_count),
text = paste("Count: ", video_count)
)) +
ggplot2::geom_bar(stat = "identity",
fill = names(barplot_colours[which(barplot_colours == params$barplotcat)])) +
ggplot2::labs(x = 'Count of Videos',
y = 'Channel Name') +
ggplot2::scale_y_discrete(
labels = function(x) {
stringr::str_wrap(x, width = 20)
}
) +
ggplot2::theme(
axis.title.x = element_text(size = 14, face = "bold"),
axis.title.y = element_text(size = 14, face = "bold")
)
# Display the plot
plotly::ggplotly(bar_plot, tooltip = "text")
```
## Common Tags by Category
**Hover your cursor over the graph to gain additional information**
```{r}
paste("Number of Tags:", params$num_tags)
```
```{r}
# Bubble Tags Plot
bubble <-
filtered_tag_counts <- data_by_date |>
# Filter date and categories
dplyr::filter(categoryId %in% params$bubbleCats) |>
# Lowercase, count and sort remaining tags
dplyr::mutate(tags = tolower(tags)) |>
dplyr::pull(tags) |>
stringr::str_split(fixed("|")) |>
unlist() |>
table(dnn = c("tag")) |>
sort(decreasing = TRUE) |>
as.data.frame() |>
subset(tag != "[none]")
# Functions to "pack" the circles in a nice layout
packing <-
packcircles::circleProgressiveLayout(filtered_tag_counts$Freq[1:params$num_tags]) |>
mutate(radius = 0.95 * radius,
id = dplyr::row_number())
packing$counts <- filtered_tag_counts$Freq[1:params$num_tags]
bubbleplot_data <- packcircles::circleLayoutVertices(packing) |>
merge(y = select(packing, id, radius, counts), by = "id")
bubble_labels <- stringr::str_wrap(filtered_tag_counts$tag[1:params$num_tags], 10)
# Create the plot
bubble_plot <-
ggplot2::ggplot(bubbleplot_data, aes(x, y, text = paste("Rank: ", id))) +
ggplot2::geom_polygon(aes(group = id, fill = counts),
colour = "black",
show.legend = TRUE) +
ggplot2::geom_text(
data = packing,
aes(
x,
y,
text = paste("Tag: ", filtered_tag_counts$tag[1:params$num_tags], "\nNumber of Videos: ", counts)
),
label = bubble_labels,
size = 3,
color = "white"
) +
ggplot2::scale_fill_gradient(name = "Num. of\nVideos", high = "#FF0000", low = "#440000") +
ggplot2::theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()
)
# Display the plot
plotly::ggplotly(bubble_plot, tooltip = "text")
```
## Popular Publishing Times
```{r}
paste("Representation Format:", params$representation_format)
paste("Category:", params$vid_category)
```
```{r}
# Polar Coordinates
polar_coor <-
data_filtered <- data_by_date |>
# Filtering dataset by category
dplyr::filter(categoryId == params$vid_category) |>
# Creating new columns for date components
dplyr::mutate(publishedAt = lubridate::ymd_hms(publishedAt)) |>
dplyr::mutate(
publish_date = lubridate::date(publishedAt),
publish_month = lubridate::month(publishedAt, label = TRUE),
publish_wday = lubridate::wday(publishedAt, label = TRUE),
publish_hour = lubridate::hour(publishedAt)
) |>
dplyr::group_by(!!rlang::sym(params$representation_format)) |>
dplyr::summarise(video_count = length(unique(video_id)))
# Render plot
ggplot2::ggplot(data_filtered,
aes(x = .data[[params$representation_format]], y = video_count, fill = video_count)) +
ggplot2::geom_bar(stat = "identity") +
ggplot2::xlab(names(interval_choices[which(interval_choices == params$representation_format)])) +
ggplot2::scale_fill_distiller(palette = "YlGnBu",
direction = 1,
name = "Number of Videos") +
ggplot2::coord_polar() +
ggplot2::theme(
axis.title.x = element_text(size = 22, face = "bold"),
axis.text.x = element_text(size = 26),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
legend.text = element_text(size = 18),
legend.title = element_text(size = 20, face = "bold"),
legend.box.margin = margin(12, 12, 12, 12)
)
```