public
Created

Annotating select points on an X-Y plot using ggplot2

  • Download Gist
gistfile1.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#
# for details see
# http://bayesball.blogspot.ca/2013/06/annotating-select-points-on-x-y-plot.html
#
# load the ggplot2 and grid packages
library(ggplot2)
library(grid)
# read data (note csv files are renamed)
tbl1 = read.csv("FanGraphs_Leaderboard_h.csv")
tbl2 = read.csv("FanGraphs_Leaderboard_d.csv")
# create new table with data from both tbl1 and tbl2 by link on variable
# 'playerid'
outfield = data.frame(merge(tbl1, tbl2, by = "playerid"))
# clean up the variable names of the two Name fields
names(outfield)[2] = paste("Name")
names(outfield)[21] = paste("Name.y")
#
qplot(UZR.150, wRAA, data = outfield)
#
# create new MarinerNames field that contains only the name of Mariners
# players (plagarized from Winston Chang's R Graphics Cookbook Recipe 5.11)
outfield$MarinerNames = outfield$Name
idx = (outfield$Team.x == "Mariners")
outfield$MarinerNames[!idx] = NA
# create a new table, taking a subset that has only the Mariners players
Mariners = subset(outfield, Team.x == "Mariners")
# add the names of the UZR stars to outfield$Table2 sort the table by
# wRAA, then add the names of the top 4 wRAA stars
outfield$wRAAstars = outfield$Name
outfield = outfield[order(-outfield$wRAA), ]
outfield$wRAAstars[5:110] = NA
# sort the table by UZR.150, then copy the first 3 names
outfield$UZRstars = outfield$Name
outfield = outfield[order(-outfield$UZR.150), ]
outfield$UZRstars[4:110] = NA
#
#
# the full ggplot verion, creating an object called "WARcht"
WARcht = ggplot(outfield, aes(x=UZR.150, y=wRAA)) + #
geom_point(colour="gray60", size=2.0) + # set the colour and size of the points
theme_bw() + # and use the "background white" theme
ggtitle("Everyday Outfielders, 2013 [to 2013-06-15]") # and put a title on the plot
#
# start with WARcht, add geom_text() [for auto labels] and annotate() [for manual labels and arrows]
#
#
WARcht + # print the chart object
geom_text(aes(label=MarinerNames), size=4, fontface="bold", colour="navyblue",
vjust=0, hjust=-0.1) + # add the names of the Mariners players
geom_text(aes(label=wRAAstars), size=3, fontface="bold",
vjust=0, hjust=-0.1) + # add the names of the top wRAA players
annotate("text", label="Shane Victorino", x=40, y=3, size=3, fontface="bold.italic") + # manually place the label for Shane Victorino
annotate("segment", x=50, y=2, xend=51.7, yend=-0.4, size=0.5,
arrow=arrow(length=unit(.2, "cm"))) + # manually place the Victorino arrow
annotate("text", label="Craig Gentry", x=40, y=-7.0, size=3, fontface="bold.italic") +
annotate("segment", x=42, y=-6.6, xend=40.9, yend=-4.0, size=0.5,
arrow=arrow(length=unit(.2, "cm"))) +
annotate("text", label="A.J. Pollock", x=49, y=-2.5, size=3, fontface="bold.italic") +
geom_point(data=Mariners, aes(x=UZR.150, y=wRAA), colour="navyblue", size=4) # over-plot the points for the Mariners players
#
#

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.