-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisuzalizations.R
175 lines (148 loc) · 5.29 KB
/
Visuzalizations.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
#Exploring with 3D Data
#Plotting every home run in 2017, 2018 using plot_ly
p <- plot_ly(x = c(0,HR_All$x), y = c(0,HR_All$y), z = c(0,HR_All$launch_angle),
type = "scatter3d",
marker = list(size = 4),
mode = "markers",
hoverinfo = "text",
text = ~paste("</br>Launch Direction:",c("Base Point",HR_All$launch_direction),
"</br> Launch Angle: ",c("",HR_All$launch_angle),
"</br> Exit Velocity: ", c("",HR_All$launch_speed),
"</br> Player: ", c("",paste(HR_All$player_name))))
layout <- list(
autosize = TRUE,
hovermode = "closest",
scene = list(
xaxis = list(
showline = FALSE,
title = "y - component",
type = "linear"
),
yaxis = list(
title = "Deviation from Dead Center",
type = "linear"
),
zaxis = list(
title = "Launch Angle",
type = "linear"
)
),
title = "Home Run Visualization",
xaxis = list(title = "y - component"),
yaxis = list(title = "Deviation from Dead Center"))
p <- layout(p, title = layout$title, scene = layout$scene,
xaxis = layout$xaxis, yaxis = layout$yaxis, zaxis = layout$zaxis)
p
plot_ly(
x = c(0,HR_All$launch_direction), y = c(0,HR_All$launch_speed), z = c(0,HR_All$launch_angle),
type = "scatter3d",
marker = list(size = 4),
mode = "markers",
hoverinfo = "text",
text = ~paste("</br>Launch Direction:",c("Base Point",HR_All$launch_direction),
"</br> Launch Angle: ",c("",HR_All$launch_angle),
"</br> Exit Velocity: ", c("",HR_All$launch_speed),
"</br> Player: ", c("",paste(HR_All$player_name))))
#Summary of HRs
HR_All %>%
select(PARK_ID) %>%
group_by(PARK_ID) %>%
summarise(N = n()) %>%
arrange(desc(N))-> HR_Summary_By_Park
ggplot(HR_All, aes(PARK_ID)) + geom_bar()
#Create a dataframe for each ballpark
for(i in 1:nrow(stadiums)){
HR_All %>%
filter(PARK_ID == stadiums[i,1]) -> x
x %>% inner_join(stadiums, by = c("PARK_ID" = "PARK_ID")) -> x
assign(toString(stadiums[i,2]),x)
}
###Function to Plot Home Runs for a Specific Ballpark for Specific EV or less###
plot3d_hr <- function(bp,ev = 100){
HR_All %>%
filter(PARK_ID == bp[1,1]) %>%
filter(launch_speed <= ev) -> foo
p <- plot_ly(x = c(0,foo$x), y = c(0,foo$y), z = c(0,foo$launch_angle),
type = "scatter3d",
marker = list(size = 4),
mode = "markers",
hoverinfo = "text",
text = ~paste("</br>Launch Direction:",c("Base Point",foo$launch_direction),
"</br> Launch Angle: ",c("",foo$launch_angle),
"</br> Exit Velocity: ", c("",foo$launch_speed),
"</br> Player: ", c("",paste(foo$player_name)))
)
layout <- list(
autosize = TRUE,
hovermode = "closest",
scene = list(
xaxis = list(
showline = FALSE,
title = "y - component",
type = "linear"
),
yaxis = list(
title = "Deviation from Dead Center",
type = "linear"
),
zaxis = list(
title = "Launch Angle",
type = "linear"
)
),
title = paste("Home Runs at", bp[1,2], "with exit velocity <= ", ev, "mph") ,
xaxis = list(title = "y - component"),
yaxis = list(title = "Deviation from Dead Center"))
p <- layout(p, title = layout$title, scene = layout$scene,
xaxis = layout$xaxis, yaxis = layout$yaxis, zaxis = layout$zaxis)
p
}
plot2d_hr <- function(bp,ev){
HR_All %>%
filter(PARK_ID == bp[1,1]) %>%
filter(launch_speed <= ev) -> foo
p <- plot_ly(x = -1*foo$launch_direction, y = foo$launch_angle,
type = "scatter",
marker = list(size = 10),
mode = "markers",
hoverinfo = "text",
text = ~paste("</br>Launch Direction:",foo$launch_direction,
"</br> Launch Angle: ",foo$launch_angle,
"</br> Exit Velocity: ",foo$launch_speed,
"</br> Player: ", paste(foo$player_name))
)
layout <- list(
autosize = TRUE,
hovermode = "closest",
scene = list(
xaxis = list(
showline = FALSE,
title = "Launch Direction",
type = "linear"
),
yaxis = list(
title = "Launch Direction",
type = "linear"
)
),
title = paste("Home Runs at", bp[1,2], "with exit velocity <= ", ev, "mph") ,
xaxis = list(title = "Launch Direction"),
yaxis = list(title = "Launch Angle"))
p <- layout(p, title = layout$title, scene = layout$scene,
xaxis = layout$xaxis, yaxis = layout$yaxis)
p
}
###Testing Plots
foo <- BOS07
plot_ly(x = c(0,foo$x), y = c(0,foo$y), z = c(0,foo$launch_angle),
type = "scatter3d",
marker = list(
size = 4
),
mode = "markers",
hoverinfo = "text",
text = ~paste("</br>Launch Direction:",c("Base Point",foo$launch_direction),
"</br> Launch Angle: ",c("",foo$launch_angle),
"</br> Exit Velocity: ", c("",foo$launch_speed)))
p <- ggplot(foo, aes(launch_direction,launch_angle)) + geom_point() + stat_smooth()
ggplotly(p)