/raptors_animation

(2020) Using gganimate to bounce emoji basketballs over the course of a game.

Primary LanguageR

Zhi  Visualizing Baskets 

Scraping & animating a basketball game using {rvest} & {gganimate}

Data: all you will need is play-by-play data sourced from basketball-reference.com.

Notebook: the graph is created using RMarkdown. See the code here.

In practice: One of my popular posts on reddit with 1500+ upvotes. This was Version 1.0.

Details

The image above is outdated. Look at my latest post here: https://www.reddit.com/r/torontoraptors/comments/fh0prr/droppin_baskets_raptors_vs_utah_20200309/

I post these animations to NBA subreddits, like r/TorontoRaptors

Feel free to check the issues to see potential improvements I'm working on.

Walkthrough

I use {ggplot2} to create the base plot. Emojis were added with {ggimage} which simply replace the points in a geom_point call. I create the sequence of events using {running_score} + {time} for {gganimate} to follow. The animation will drop basketballs on the winning team's player whenever they get a basket. Three pointers are highlighted with flames. The final frame is added to display point totals from each player and remains still for 5 seconds.

The data is play-by-play (pbp) data from basketball-reference.com. I use rvest to scrape the pbp table. I extract player names and filter for scoring plays only. I also use conditional logic so it knows which team wins and it prepares the title, logo, and animation accordingly.

The notebook will then scrape teamcolorcodes.com to find the team's hex colour code and add it to the plot title using {ggtext}.

Finally, it applies transparency to the team logo in the plot background with {png}.

See notebook for all the code used.

Please leave a star if you like it.

Requirements

library(tidyverse) # for the basics
library(gganimate) # for animation and gifs
library(ggimage) # for inserting the basketball emojis
library(rvest) # for scraping data from websites
library(lubridate) # for date manipulation
library(png) # for image manipulation
library(grid) # for custom plot manipulation
library(ggtext) # for colouring the plot title
library(extrafont) # for nicer fonts

See more

See more animations in the /animations folder.

Twitter 💬   |   LinkedIn 👔   |   GitHub :octocat:   |   Reddit 🔗