Skip to content

ggplot2: Panel Chart of Selected Player Statistics in Basketball

May 24, 2009

Basketballgeek is exploring the relationship of several game statistics at home and away games. He uses R to illustrate these relationships from the NBA 08-09 regular season. One of the graphs is below.

Instead of plotting all the 10 graphs one-by-one this post demonstrates how to achieve the same result and combine the graphs into one plot using the powerful facetting feature in ggplot2.


The first step after importing the supplied data file is to replace the codes of statistics with meaningful names, as these would be used as panel labels.

> library(ggplot2)
> df1 <- read.csv("2008.stats", header = T)
> levels(df1$stat) <- list(`FT%` = "ftp", `2FG%` = "2fgp",
     `3FG%` = "3fgp", `eFG%` = "efg", `OR%` = "orp",
     `DR%` = "drp", `TO/Poss` = "to_poss", `Fouled/Poss` = "fouled_poss",
     `Steal/Poss` = "steal_poss", `Foul/Poss` = "foul_poss")


Each small panel shows the data with the fitted linear models. The first plot has fixed x&y axis, whereas in the second plot each of the panels has varying axes limits, in my view providing a little more insight into the data.

Plot 1 with equal x & y axis limits

> ggplot(df1, aes(home, away)) + geom_point(shape = 1) +
     geom_smooth(method = "lm", se = FALSE,
         fullrange = T) + facet_wrap(~stat,
     ncol = 2) + labs(x = "Home", y = "Away") +

Plot 2 with varying x & y axis limits

> last_plot() + facet_wrap(~stat, ncol = 2, scales = "free") +
One Comment leave one →
  1. May 24, 2009 11:41 pm

    I just learned about ggplot the other day from Andrew Gelman’s blog, so I’m excited to see you show me how to do this with my data! Looks like I’ll be implementing it sooner rather than later.

    Thanks again for the demo.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: