Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Created June 18, 2013 03:30
Show Gist options
  • Save MonkmanMH/5802497 to your computer and use it in GitHub Desktop.
Save MonkmanMH/5802497 to your computer and use it in GitHub Desktop.
Annotating select points on an X-Y plot using ggplot2
#
# 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
#
#
@ndjrt
Copy link

ndjrt commented Feb 22, 2018

Hi,
When I try to run this, it errors out on line 59 with the following error:

  • geom_point(data=Mariners, aes(x=UZR.150, y=wRAA), colour="navyblue", size=4) # over-plot the points for the Mariners players
    Error in FUN(X[[i]], ...) : object 'wRAA' not found

Not sure if I downloaded the data incorrectly either. Sees to read the info in without issue.

Fairly new to R so apologies if I am missing something obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment