Skip to content

ggplot2: Small Multiples Plot of Football Results

March 24, 2009

In the previous post I used R to create a football league table using the raw data available on the Internet. As my main goal is to quickly get an overview of developments in the leaguetable, I experimented a bit with the graphic display of the available data using ggplot2.

Using the same dataframes created in the previous post I was first interested in comparing the number of wins, losses, draws by team.
qplot (data=dfm, team, facets=pts~.)


This is almost what I want, except that the team names are unreadable and the list is not sorted in a meaningful way. Time to fix these small flaws by abbreviating the team names and sorting teams by the current total points:
qplot (data=dfm, team, facets=pts~.) +
limits=levels(with(dfm, reorder(team, -pts, sum))))


It would be also interesting to see how the teams’ total points have developed during the season.
(p <- qplot(data=dfm1, date, cumpts, geom="line", colour=team) +
labs(x="",y="Total Points", colour="") )

Whilst this plot has a lot of information, it is too crowded and difficult to follow. One option to zoom in to a period of your choice is via coord_cartesian:
p + coord_cartesian(xlim = c(as.numeric(as.Date("2009-01-01")), as.numeric(as.Date("2009-02-28"))))
Alternatively, the plot can be easily modified to use the dataset that includes only results from the period of interest:
p %+% dfm1[dfm1$date > as.Date("2009-01-01"),]
Or, to zoom in on the table positions of the first 5 teams during a specified time period
n <- 5
dfm1$team <- with(dfm1, reorder(team, -pts, sum))
nteams <- levels(dfm1$team)[1:n]
p2 <- qplot(data=dfm3, date, position, geom="line", colour=team, size="1") + labs(x="",y="Position", colour="") + scale_y_reverse()
p2 %+% dfm3[dfm3$date > as.Date("2008-01-01") & dfm3$team %in% nteams,]

The plots above present the changes in the league table. In addition a game-by-game team performance would be of interest. Next, a bar plot will be drawn, with the bar colour representing the match result:
(p2 <- ggplot(dfm) +
geom_bar(aes(x=date, y=0.1, fill=factor(pts), width=1), stat="identity") +
facet_grid (team ~ .) )


The above result can also be achieved using geom_rect instead of geom_bar:
(p2 <- ggplot(dfm) +
geom_rect(aes(xmin = date-0.5, xmax = date + 0.5, ymin = 0, ymax = 1, fill=factor(pts))) +
facet_grid (team ~ .) )

A few adjustments to the default formatting of the above plot are in order. First, the default colours of the bars are changed:Win – Green, Loss – Red, Draw – Blue. Also, grey background is removed and facet labels rotated.
p2 + scale_fill_manual(values = c("3"="green","1" = "blue","0"="red"))+
scale_y_continuous(breaks=NA) +
theme_bw() +
opts (
strip.text.y = theme_text(angle = 360),
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
axis.text.y = theme_blank(),
strip.background = theme_blank(),
panel.border = theme_rect(colour = NA),
legend.position = "none"


No comments yet

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: