-
Notifications
You must be signed in to change notification settings - Fork 12
/
createPitchETM.R
172 lines (142 loc) · 7.73 KB
/
createPitchETM.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#Based on the createPitch function written by FC_Rstats https://github.com/FCrSTATS
#Modified by Eliot McKinley ([email protected]) to use yards as the unit of measurement,
#enable plotting of a half pitch, and modifications to allow faceting
createPitch <- function(xmax=115, ymax=80, grass_colour="white", line_colour="gray", background_colour="white", goal_colour="gray", data=NULL, halfPitch=FALSE){
GoalWidth <- 8
penspot <- 12
boxedgeW <- 44
boxedgeL <- 18
box6yardW <- 20
box6yardL <- 6
corner_d=3
centreCirle_d <- 20
# The 18 Yard Box
TheBoxWidth <- c(((ymax / 2) + (boxedgeW / 2)),((ymax / 2) - (boxedgeW / 2)))
TheBoxHeight <- c(boxedgeL,xmax-boxedgeL)
GoalPosts <- c(((ymax / 2) + (GoalWidth / 2)),((ymax / 2) - (GoalWidth / 2)))
# The 6 Yard Box
box6yardWidth <- c(((ymax / 2) + (box6yardW / 2)),((ymax / 2) - (box6yardW / 2)))
box6yardHeight <- c(box6yardL,xmax-box6yardL)
## define the circle function
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
#### create leftD arc ####
Dleft <- circleFun(c((penspot),(ymax/2)),centreCirle_d,npoints = 1000)
## remove part that is in the box
Dleft <- Dleft[which(Dleft$x >= (boxedgeL)),]
## create rightD arc ####
Dright <- circleFun(c((xmax-(penspot)),(ymax/2)),centreCirle_d,npoints = 1000)
## remove part that is in the box
Dright <- Dright[which(Dright$x <= (xmax-(boxedgeL))),]
#### create center circle ####
center_circle <- circleFun(c((xmax/2),(ymax/2)),centreCirle_d,npoints = 2000)
if (halfPitch==FALSE){
xmin=0
ymin=0
## create corner flag radius ####
TopLeftCorner <- circleFun(c(xmin,ymax),corner_d,npoints = 1000)
TopLeftCorner <- TopLeftCorner[which(TopLeftCorner$x > (xmin)),]
TopLeftCorner <- TopLeftCorner[which(TopLeftCorner$y < (ymax)),]
TopRightCorner <- circleFun(c(xmax,ymax),corner_d,npoints = 1000)
TopRightCorner <- TopRightCorner[which(TopRightCorner$x < (xmax)),]
TopRightCorner <- TopRightCorner[which(TopRightCorner$y < (ymax)),]
BottomLeftCorner <- circleFun(c(xmin,ymin),corner_d,npoints = 1000)
BottomLeftCorner <- BottomLeftCorner[which(BottomLeftCorner$x > (xmin)),]
BottomLeftCorner <- BottomLeftCorner[which(BottomLeftCorner$y > (ymin)),]
BottomRightCorner <- circleFun(c(xmax,ymin),corner_d,npoints = 1000)
BottomRightCorner <- BottomRightCorner[which(BottomRightCorner$x < (xmax)),]
BottomRightCorner <- BottomRightCorner[which(BottomRightCorner$y > (ymin)),]
ggplot(data=data) + #xlim(c(ymin,ymax)) + ylim(c(xmin,xmax)) +
# add the theme
#theme_blankPitch() +
# add the base rectangle of the pitch
geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = grass_colour, colour = line_colour)+
# add the 18 yard box Left
geom_rect(aes(xmin=0, xmax=TheBoxHeight[1], ymin=TheBoxWidth[1], ymax=TheBoxWidth[2]), fill = grass_colour, colour = line_colour) +
# add the 18 yard box Right
geom_rect(aes(xmin=TheBoxHeight[2], xmax=xmax, ymin=TheBoxWidth[1], ymax=TheBoxWidth[2]), fill = grass_colour, colour = line_colour) +
# add the six yard box Left
geom_rect(aes(xmin=0, xmax=box6yardHeight[1], ymin=box6yardWidth[1], ymax=box6yardWidth[2]), fill = grass_colour, colour = line_colour) +
# add the six yard box Right
geom_rect(aes(xmin=box6yardHeight[2], xmax=xmax, ymin=box6yardWidth[1], ymax=box6yardWidth[2]), fill = grass_colour, colour = line_colour) +
# Add half way line
geom_segment(aes(x = xmax/2, y = ymin, xend = xmax/2, yend = ymax),colour = line_colour) +
# add left D
geom_path(data=Dleft, aes(x=x,y=y), colour = line_colour) +
# add Right D
geom_path(data=Dright, aes(x=x,y=y), colour = line_colour) +
# add centre circle
geom_path(data=center_circle, aes(x=x,y=y), colour = line_colour) +
# add penalty spot left
geom_point(aes(x = penspot , y = ymax/2), colour = line_colour) +
# add penalty spot right
geom_point(aes(x = (xmax-(penspot)) , y = ymax/2), colour = line_colour) +
# add centre spot
geom_point(aes(x = (xmax/2) , y = ymax/2), colour = line_colour) +
# add Corner Flag corners
geom_path(data=TopLeftCorner, aes(x=x,y=y), colour = line_colour) +
geom_path(data=TopRightCorner, aes(x=x,y=y), colour = line_colour) +
geom_path(data=BottomLeftCorner, aes(x=x,y=y), colour = line_colour) +
geom_path(data=BottomRightCorner, aes(x=x,y=y), colour = line_colour) +
geom_segment(aes(x = xmin-0.2, y = GoalPosts[1], xend = xmin-0.2, yend = GoalPosts[2]),colour = goal_colour, size = 1) +
# add the goal right
geom_segment(aes(x = xmax+0.2, y = GoalPosts[1], xend = xmax+0.2, yend = GoalPosts[2]),colour = goal_colour, size = 1) +
coord_fixed() +
theme(rect = element_blank(),#, #remove additional ggplot2 features: lines, axis, etc...
line = element_blank(),
#legend.position = "none",
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank())
}
else{
xmin=(xmax/2)
ymin=0
center_circle = center_circle[which(center_circle$x>=xmin),]
## create corner flag radius ####
BottomRightCorner <- circleFun(c(xmax,ymin),corner_d,npoints = 1000)
BottomRightCorner <- BottomRightCorner[which(BottomRightCorner$x < (xmax)),]
BottomRightCorner <- BottomRightCorner[which(BottomRightCorner$y > (ymin)),]
TopRightCorner <- circleFun(c(xmax,ymax),corner_d,npoints = 1000)
TopRightCorner <- TopRightCorner[which(TopRightCorner$x < (xmax)),]
TopRightCorner <- TopRightCorner[which(TopRightCorner$y < (ymax)),]
ggplot(data=data) + #xlim(c(ymin,ymax)) + ylim(c(xmin,xmax)) +
# add the theme
#theme_blankPitch() +
# add the base rectangle of the pitch
geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = grass_colour, colour = line_colour)+
# add the 18 yard box offensive
geom_rect(aes(xmin=TheBoxHeight[2], xmax=xmax, ymin=TheBoxWidth[1], ymax=TheBoxWidth[2]), fill = grass_colour, colour = line_colour)+
# add the six yard box offensive
geom_rect(aes(xmin=box6yardHeight[2], xmax=xmax, ymin=box6yardWidth[1], ymax=box6yardWidth[2]), fill = grass_colour, colour = line_colour)+
# add the arc circle
geom_path(data=Dright, aes(x=x,y=y), colour = line_colour)+
#add center arc
geom_path(data=center_circle, aes(x=x,y=y), colour = line_colour)+
# add penalty spot
geom_point(aes(x = (xmax-(penspot)) , y = ymax/2), colour = line_colour) +
# add centre spot
geom_point(aes(x = (xmax/2) , y = ymax/2), colour = line_colour) +
#geom_point(aes(x = CentreSpot , y = penSpotOff), colour = line_colour) +
# add Corner Flag corners
geom_path(data=BottomRightCorner, aes(x=x,y=y), colour = line_colour) +
geom_path(data=TopRightCorner, aes(x=x,y=y), colour = line_colour) +
# add the goal right
geom_segment(aes(x = xmax+0.2, y = GoalPosts[1], xend = xmax+0.2, yend = GoalPosts[2]),colour = goal_colour, size = 1) +
# add the goal offensive
coord_fixed() +
theme(rect = element_blank(), #remove additional ggplot2 features: lines, axis, etc...
line = element_blank(),
#legend.position = "none",
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank())
}
}