---
title: 'Game of Thrones "Who killed Whom"'
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse) # data manipulation
library(visNetwork) # network
library(ggimage) # display icon instead of points in ggplot
library(ggwordcloud) # wordcloud for ggplot
load("valar.RData")
```
Network
=======================================================================
Column {data-width=150}
-----------------------------------------------------------------------
### Legend
```{r, fig.height=12, fig.width=3}
legendnodes <- legendnodes %>%
mutate(label = fct_recode(label, "Children of the\nForest" = "Children of the Forest"),
label = fct_drop(label))
legendnodes %>%
mutate(X = rep(1, times = 16),
Y = seq(32, 2, -2)) %>%
ggplot(aes(x = X, y = Y, fill = color)) +
geom_point(shape = 21, size = 8, color = "black") +
scale_fill_identity() +
geom_text(aes(label = label), nudge_x = 0.25, color = "black", hjust = 0, size = 6) +
geom_text(aes(x = 1, y = 34, label = "Houses / Groups"), color = "black", size = 6, hjust = 0.06) +
scale_x_continuous(expand = expand_scale(add = c(0.5, 2))) +
theme_void()
```
Column {data-width=850}
-----------------------------------------------------------------------
### Kikatuéki
```{r}
visNetwork(nodes = nodes, edges = edges, width = "100%",
# add title
main = "Game of Thrones - Kikatuéki",
submain = paste0("(Zoom and Hover over nodes and arrows to get more information)")) %>%
visEdges(color = list(color = "black"),
arrows = list(to = TRUE),
smooth = TRUE,
shadow = FALSE) %>%
visLayout(randomSeed = 42) %>%
# add a input selection (by house)
visOptions(selectedBy = "group") %>%
# enable navigation buttons, disable nodes and edges selection
visInteraction(navigationButtons = TRUE, selectable = FALSE) %>%
visPhysics(solver = "barnesHut",
barnesHut = list(avoidOverlap = 0.6))
```
More Graphs {data-orientation=rows}
=======================================================================
Row {data-height=120}
-----------------------------------------------------------------------
### Important Dead Characters
```{r}
num_dead <- n_distinct(got_death$name)
valueBox(value = num_dead,
icon = "fa-skull",
color = "crimson")
```
### Killers
```{r}
num_killers <- n_distinct(got_death$killed_by)
valueBox(value = num_killers,
caption = "Different Killers",
icon = "fa-crow",
color = "Moccasin")
```
### Deadliest
```{r}
deadly_season <- got_death %>% count(season_death) %>% top_n(1) %>% pull(season_death)
valueBox(value = paste("Season", deadly_season),
caption = "is the deadliest season",
icon = "fa-tv",
color = "DarkOrchid")
```
### Characters Killed More than 8 Other Persons
```{r}
more_eight <- got_death %>% count(killed_by) %>% filter(n >= 8) %>% n_distinct()
valueBox(value = more_eight,
icon = "fa-crown",
color = "Chocolate")
```
Column {data-width=400}
-----------------------------------------------------------------------
### Houses killed {.no-title}
```{r}
got_char %>%
left_join(got_death, by = "name") %>%
left_join(got_house, by = "house") %>%
filter(!is.na(killed_by), !is.na(house)) %>%
select(name, house) %>%
distinct() %>%
count(house) %>%
ggplot(aes(x = fct_reorder(house, n), y = n, fill = n)) +
geom_col(width = 0.75) +
scale_y_continuous(expand = expand_scale(add = c(0, 1.5))) +
scale_fill_gradient(low = "orange", high = "red") +
labs(title = "Game of Thrones TV Show\nMost decimated houses", subtitle = "(Important characters killed From Season 1 to Season 7)",
x = "", y = "Number of persons killed") +
coord_flip() +
theme(legend.position = "none"
, plot.title = element_text(face = "bold", size = 18, hjust = 0.5)
, plot.subtitle = element_text(hjust = 0.5)
, axis.ticks.y = element_blank()
, axis.text.y = element_text(face = "italic", size = 9)
, panel.grid.major.y = element_blank()
, panel.grid.minor.x = element_blank()
, plot.margin = margin(5,50,5,5))
```
### Top Killers {.no-title}
```{r}
# Prepare data
temp_data <- got_death %>%
count(killed_by) %>%
top_n(n = 15, wt = n) %>%
arrange(-n)
# icon ; from https://www.shutterstock.com/image-vector/throne-icon-398765014
# or
icon <- "https://image.winudf.com/v2/image/Y29tLnVud2VhcnlLZXRhcy5nb3RfaWNvbl8xNTAzNDE4NDk5XzAzNQ/icon.png?w=170&fakeurl=1&type=.png"
# inspired from # https://stackoverflow.com/questions/47363416/can-i-use-ggplot-to-replace-the-default-shape-with-custom-picture
# repeat the killer's name as many times as number of killings ; repeat icon
tibble(name = rep(temp_data$killed_by, temp_data$n),
img = icon) %>%
group_by(name) %>%
# y-coordinate of the icon
mutate(Y = row_number()) %>%
ungroup() %>%
ggplot(aes(x = fct_reorder(name, Y), y = Y)) +
geom_image(aes(image = img), size = 0.06) +
coord_flip() +
labs(title = "Game of Thrones TV Show\nTop 12 Killers",
subtitle = "(Important characters killed From Season 1 to Season 7)",
x = "", y = "") +
theme(legend.position = "none"
, plot.title = element_text(face = "bold", size = 18, hjust = 0.5)
, plot.subtitle = element_text(hjust = 0.5)
, axis.ticks = element_blank()
, axis.text.y = element_text(face = "italic", size = 11)
, axis.text.x = element_blank()
, panel.grid.major.y = element_blank()
, panel.grid.minor.x = element_blank())
```
### Character table {.no-title}
```{r}
# table character/killed_by
got_death %>%
select(Character = name, "Killed by" = killed_by) %>%
DT::datatable(rownames = FALSE, options = list(pageLength = 10))
```
Column {data-width=500}
-----------------------------------------------------------------------
### Episodes {.no-title}
```{r}
got_char %>%
left_join(got_death, by = "name") %>%
left_join(got_house, by = "house") %>%
filter(!is.na(killed_by), !is.na(season_death)) %>%
select(name, season_death, episode_death) %>%
distinct() %>%
count(episode_death) %>%
ggplot(aes(x = episode_death, y = n))+
# add a backgroung image
annotation_custom(grid::rasterGrob(image = valar
, width = unit(0.38, "npc")
, height = unit(0.3, "npc")
, x = 0.5
, y = 0.78)
) +
geom_col(width = 0.02) +
geom_smooth(aes(color = ""), method = "loess", se = FALSE, show.legend = TRUE) +
scale_x_continuous(breaks = 1:10, labels = paste("Epis", 1:10)) +
scale_y_continuous(expand = expand_scale(add = c(0.2, 3))) +
scale_color_manual(name = "Global Trend", values = "firebrick2") +
labs(title = "More important deaths appearing\nin Season Endings",
subtitle = "(Important characters killed From Season 1 to Season 7)",
x = "", y = "Number of deaths per episode",
caption = "Note: Season 7 ony has 7 episodes") +
theme(legend.position = c(0.14, 0.85)
, legend.direction = "horizontal"
, plot.title = element_text(face = "bold", size = 18, hjust = 0.5)
, plot.subtitle = element_text(hjust = 0.5)
, axis.ticks = element_blank()
, axis.text = element_text(face = "italic", size = 9)
, panel.grid.minor = element_blank()
, panel.background = element_blank()
, panel.grid.major.y = element_line(colour = "grey80", linetype = "dashed")
, plot.margin = margin(15,15,5,20)
, axis.title.y = element_text(vjust = 3)
, plot.caption = element_text(face = "italic")
, panel.border = element_rect(fill = NA))
```
### Locations {.no-title}
```{r}
# wordcloud
set.seed(42)
got_death %>%
count(location_death) %>%
arrange(-n) %>%
mutate(angle = runif(n = max(row_number()), min = -30, max = 30),
angle = ifelse(n == max(n), 0, angle)) %>%
ggplot(aes(label = location_death, size = n, color = n)) +
geom_text_wordcloud(aes(angle = angle), eccentricity = .35) +
labs(title = "Main deaths' Locations",
caption = "Hodor") +
scale_size_area(max_size = 15) +
theme_minimal() +
scale_color_gradient(low = "chocolate1", high = "firebrick2") +
theme(plot.title = element_text(face = "bold", size = 18, hjust = 0.5))
```