Why use GOT data? Because I was participating in #datosdemiercoles which is the spanish version of #tidytuesday. So the data is given and the purpouse is to learn new packages using that data and share with the community, you know this already, right?
Secondly the package I want to learn beside {ggforce} is {gganimate} so a very first idea was represent every character as a point and move according the actual affiliations. A kind of copy inspiration from FlowingData’s A Day in the Life of Americans1
Disclaimers
I know the animation is not then best way to visualiza data! Don’t judge me please! But is fun to play with.
I don’t follow the series so I don’t know if the results or animations makes any sense.
The data, the wrangling and the cleaning
The data come from this post where the shifting affiliations are visualized using an alluyvial diagram. It’s a nice post by Matthew Lunkes where he tell all the process to get the final chart:
In this case the data can be downloaded from this repository https://github.com/MattLunkes/GoT_Affiliations.
# A tibble: 488 × 11
Name Origin `Starting Affiliation` `End of S1` `End of S2` `End of S3`
<chr> <chr> <chr> <chr> <chr> <chr>
1 Tyrion Lan… House… King Robert Baratheon King Joffr… King Joffr… King Joffr…
2 Cersei Lan… House… King Robert Baratheon King Joffr… King Joffr… King Joffr…
3 Daenerys T… House… Viserys Targaryen Daenerys T… Daenerys T… Daenerys T…
4 Jon Snow House… King Robert Baratheon Night's Wa… Wildlings Night's Wa…
5 Sansa Stark House… King Robert Baratheon King Joffr… King Joffr… King Joffr…
6 Arya Stark House… King Robert Baratheon Other, Wes… Other, Wes… Other, Wes…
7 Jaime Lann… House… King Robert Baratheon King Joffr… King Joffr… King Joffr…
8 Jorah Morm… House… Viserys Targaryen Daenerys T… Daenerys T… Daenerys T…
9 Theon Grey… House… King Robert Baratheon Robb Stark… Balon Grey… King Joffr…
10 Samwell Ta… House… Night's Watch Night's Wa… Night's Wa… Night's Wa…
# ℹ 478 more rows
# ℹ 5 more variables: `End of S4` <chr>, `End of S5` <chr>, `End of S6` <chr>,
# `End of S7` <chr>, Episodes <dbl>
As we see, the data comes in a not tidy way so gather is our friend here.
Code
data_long <- data |> janitor::clean_names() |>rename(end_of_s0 = starting_affiliation) |>select(-episodes, -origin) |>gather(season, affiliation, -name) |>mutate(season =as.numeric(str_extract(season, "\\d+")),affiliation =case_when( affiliation =="King Robert Baratheon"~"Baratheon", affiliation =="Viserys Targaryen"~"Targaryen", affiliation =="King Joffrey Baratheon"~"Lannister", affiliation =="Daenerys Targaryen"~"Targaryen", affiliation =="Night's Watch"~"Night's Watch", affiliation =="Other, Westeros"~"Westeros", affiliation =="Wildlings"~"Wildlings", affiliation =="King Tommen Baratheon"~"Lannister", affiliation =="Petyr Baelish, Lord Protector of the Vale"~"The Vale", affiliation =="Other, Essos"~"Essos", affiliation =="Roose Bolton, Lord Paramount of the North"~"Bolton", affiliation =="Queen Cersei Lannister"~"Lannister", affiliation =="Jon Snow, King in the North"~"Stark",TRUE~ affiliation ) )# there are some repeated characters?data_long <- data_long |>semi_join(count(data, Name) |>filter(n ==1), by =c("name"="Name")) |># importante for the ggrepel partarrange(season, name, affiliation)data_long
At the beginning I think use a circular layout and see what happend but the result was far for beign interesting, and as we can see I was a failure in my first attempt using {gganimate}.
Well, so the next idea and step was to get closer the affilations related. How can be two affiliations be related? An answer can be the the amount of characters which move from one to another.
# A tibble: 119 × 3
from to n
<chr> <chr> <int>
1 Balon Greyjoy, King of the Iron Islands Balon Greyjoy, King of the Iro… 31
2 Balon Greyjoy, King of the Iron Islands Deceased 7
3 Balon Greyjoy, King of the Iron Islands Euron Greyjoy, King of the Iro… 4
4 Balon Greyjoy, King of the Iron Islands Lannister 1
5 Balon Greyjoy, King of the Iron Islands Targaryen 3
6 Baratheon Balon Greyjoy, King of the Iro… 9
7 Baratheon Brotherhood Without Banners 5
8 Baratheon Deceased 15
9 Baratheon Essos 1
10 Baratheon House Arryn (Neutral) 12
# ℹ 109 more rows
Now, with this data we can use the {igraph} package and the graph_from_data_frame function to get a graph from the previous data frame and then get a layout.
This is really an improvement from the the circular layout. The downside is the main affiliations are too close so the text is overlaping. A simple solution to this was generate an equidistant sequence for every set of coordinates, \(x\) and \(y\).
Happy with the effect of a simple fix for the overlaping text. And I think this change keep the spirit of the original graph’s shape.
Character positions
To get the character positions for every step/time/season we decided to put them in the corresponding affiliation making a circle around it and then adding a random noise
p <-ggplot() +geom_point(aes(x, y, color = affiliation), alpha =0.5, data = characters) +geom_text(aes(x, y, size = n, label = affiliation), alpha =0.5, data = affiliations) +scale_size_area() +scale_color_viridis_d() +theme(legend.position ="none")p
Nice! We are almost there.
Some details before the magic
To get a very style GOT theme we need first the font, you can download from this link https://fontmeme.com/fonts/game-of-thrones-font/.2 and use it with the {extrafont} package.
We’ll select some important characters to use with {ggrepel} package:
Code
main_characters <- data |>select(name = Name, Episodes) |>arrange(desc(Episodes)) |>head(5)knitr::kable(main_characters)
library(gganimate)p <- p +labs(subtitle ="Affiliation changes in season {trunc(frame_time)}") +transition_time(season) +shadow_wake(wake_length =0.005, alpha =TRUE, exclude_layer =1) +ease_aes("exponential-in-out")
For test purposes I recommend reduce de fps to 10, and duration as much you can according how many frames you are using so you can to check if the output animation is what you want quickly, then for the final output use at least 30 fps to get a smooth transition.