Skip to content

Instantly share code, notes, and snippets.

@JoGall
Last active January 29, 2018 13:29
Show Gist options
  • Save JoGall/239e556f1dad19f8a43b6234c4e57b71 to your computer and use it in GitHub Desktop.
Save JoGall/239e556f1dad19f8a43b6234c4e57b71 to your computer and use it in GitHub Desktop.
Draw a netball court using ggplot
library(ggplot2)
library(ggforce) # gives us `geom_circle` and `geom_arc` functions
# pitch dimensions
courtLength <- 30.5
courtWidth <- 15.25
courtRunoff <- 3.05
# aesthetic parameters
pitch_col <- "grey90"
line_col <- "black"
lwd <- 1.5
# plot!
ggplot() +
# draw pitch including runoff
geom_rect(aes(xmin = 0 - courtRunoff, xmax = courtLength + courtRunoff, ymin = 0 - courtRunoff, ymax = courtWidth + courtRunoff), col = "grey80", fill = "white", lwd = lwd) +
# draw pitch lines
geom_rect(aes(xmin = 0, xmax = courtLength, ymin = 0, ymax = courtWidth), fill = pitch_col, col = line_col, lwd = lwd) +
# draw third lines
geom_segment(aes(x = courtLength / 3, xend = courtLength / 3, y = 0, yend = courtWidth), col = line_col, lwd = lwd) +
geom_segment(aes(x = courtLength / 3 * 2, xend = courtLength / 3 * 2, y = 0, yend = courtWidth), col = line_col, lwd = lwd) +
# draw centre circle
ggforce::geom_circle(aes(x0 = courtLength / 2, y0 = courtWidth / 2, r = 0.45), col = line_col, lwd = lwd) +
# draw goal areas
ggforce::geom_arc(aes(x0 = 0, y0 = courtWidth / 2, r = 4.9, start = 0, end = pi), col = line_col, lwd = lwd) +
ggforce::geom_arc(aes(x0 = courtLength, y0 = courtWidth / 2, r = 4.9, start = pi, end = 2 * pi), col = line_col, lwd = lwd) +
coord_fixed() +
# blank theme
theme(rect = element_blank(),
text = element_blank(),
line = element_blank(),
axis.text = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment