make_graphics.R 5.47 KB
Newer Older
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
DEFAULT_MAX_PLAYER <- 11
DEFAULT_PITCH_LENGTH <- 105.0
DEFAULT_PITCH_WIDTH <- 70.0
DEFAULT_PITCH_MARGIN <- 5.0
DEFAULT_CENTER_CIRCLE_R <- 9.15
DEFAULT_PENALTY_AREA_LENGTH <- 16.5
DEFAULT_PENALTY_AREA_WIDTH <- 40.32
DEFAULT_PENALTY_CIRCLE_R <- 9.15
DEFAULT_PENALTY_SPOT_DIST <- 11.0
DEFAULT_GOAL_AREA_LENGTH <- 5.5
DEFAULT_GOAL_AREA_WIDTH <- 18.32
DEFAULT_GOAL_DEPTH <- 2.44
# DEFAULT_CORNER_ARC_R <- 1.0
half_p_l <- DEFAULT_PITCH_LENGTH/2
half_p_w <- DEFAULT_PITCH_WIDTH/2
penalty_x <- half_p_l - DEFAULT_PENALTY_AREA_LENGTH
penalty_y <- DEFAULT_PENALTY_AREA_WIDTH/2
goal_x <- half_p_l - DEFAULT_GOAL_AREA_LENGTH
goal_y <- DEFAULT_GOAL_AREA_WIDTH/2



make_field <- function(input_graph){
    p <- input_graph +
    
26
27
    geom_hline(yintercept = 0, linetype = "solid")+
    geom_vline(xintercept = 0, linetype = "solid")+
28
29
30
31
32
    geom_linerange(aes(x=half_p_l, y=NULL, ymin=-half_p_w, ymax=half_p_w))+
    geom_linerange(aes(x=-half_p_l, y=NULL, ymin=-half_p_w, ymax=half_p_w))+
    geom_linerange(aes(x=NULL, y=half_p_w, xmin=-half_p_l, xmax=half_p_l))+
    geom_linerange(aes(x=NULL, y=-half_p_w, xmin=-half_p_l, xmax=half_p_l))+

33
    geom_point(size=0.1) +
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    geom_linerange(aes(x=penalty_x, y=NULL, ymin=-penalty_y, ymax=penalty_y))+
    geom_linerange(aes(x=-penalty_x, y=NULL, ymin=-penalty_y, ymax=penalty_y))+
    geom_linerange(aes(x=NULL, y=penalty_y, xmin=penalty_x, xmax=half_p_l))+
    geom_linerange(aes(x=NULL, y=-penalty_y, xmin=penalty_x, xmax=half_p_l))+
    geom_linerange(aes(x=NULL, y=penalty_y, xmax=-penalty_x, xmin=-half_p_l))+
    geom_linerange(aes(x=NULL, y=-penalty_y, xmax=-penalty_x, xmin=-half_p_l))+

    geom_linerange(aes(x=goal_x, y=NULL, ymin=-goal_y, ymax=goal_y))+
    geom_linerange(aes(x=-goal_x, y=NULL, ymin=-goal_y, ymax=goal_y))+
    geom_linerange(aes(x=NULL, y=goal_y, xmax=goal_x, xmin=half_p_l))+
    geom_linerange(aes(x=NULL, y=-goal_y, xmax=goal_x, xmin=half_p_l))+
    geom_linerange(aes(x=NULL, y=goal_y, xmin=-goal_x, xmax=-half_p_l))+
    geom_linerange(aes(x=NULL, y=-goal_y, xmin=-goal_x, xmax=-half_p_l))+

    ggforce::geom_circle(aes(x0 = 0, y0 = 0, r = DEFAULT_CENTER_CIRCLE_R),inherit.aes = FALSE)
    return(p)
}

make_heatmap <- function(p_data){
53
    p <- ggplot(data = p_data, aes(x = x, y = y )) +
54
    ggplot2::coord_fixed(xlim=c(-half_p_l,half_p_l),ylim =c(-half_p_w,half_p_w))+ 
55
56
57
58
    theme(text = element_text(size = 24)) +
    geom_density_2d_filled(alpha = 0.7) +
    labs(x = "x軸", y = "y軸",fill = "プレー割合") + 
    theme_minimal(base_size = 6)
59
60

    p <- p |> make_field()
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

    return(p)
}
make_heatmap_ball <- function(p_data){
    p <- ggplot(data = p_data, aes(x = ball_x, y = ball_y )) +
    ggplot2::coord_fixed(xlim=c(-half_p_l,half_p_l),ylim =c(-half_p_w,half_p_w))+ 
    theme(text = element_text(size = 24)) +
    geom_density_2d_filled(alpha = 0.7) +
    labs(x = "x軸", y = "y軸",fill = "プレー割合") + 
    theme_minimal(base_size = 6)

    p <- p |> make_field()

    return(p)
}
make_kick_point <- function(p_data,input_graph){
    p <- input_graph +
    geom_point(data = p_data,colour = "red",size = 0.5,aes(x = as.numeric(ball_x), y = as.numeric(ball_y)))

    return(p)
}

make_pass_point <- function(p_data,input_graph){
    p <- input_graph +
    geom_point(data = p_data,colour = "bule",size = 0.5,aes(x = as.numeric(ball_x), y = as.numeric(ball_y)))

    return(p)
}

make_dribble_point <- function(p_data,input_graph){
    p <- input_graph +
    geom_point(data = p_data,colour = "green",size = 0.5,aes(x = as.numeric(ball_x), y = as.numeric(ball_y)))

94
95
96
    return(p)
}

97
98
99
make_tackle_point <- function(p_data,input_graph){
    p <- input_graph +
    geom_point(data = p_data,colour = "",size = 0.5,aes(x = as.numeric(ball_x), y = as.numeric(ball_y)))
100

101
102
    return(p)
}
103

104
105
106
107
108
109
110
111
112
make_ball_area <- function(rcg){
    ball_path <- rcg |>
        dplyr::group_nest(step, ball_x, ball_y) |>
        dplyr::mutate(move_dist_x = ball_x - dplyr::lag(ball_x),
                        move_dist_y = ball_y - dplyr::lag(ball_y),
                        move_dist   = sqrt(move_dist_x^2 + move_dist_y^2),
                        move_dist   = dplyr::if_else(is.na(move_dist), 0, move_dist)
        ) |>
        dplyr::filter(move_dist != 0 & move_dist < 40)
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
    output <- ball_path |>
        plotly::plot_ly(showlegend = FALSE) |>
        plotly::add_markers(
            data       = ball_path,
            x          = ~ball_x,
            y          = ~ball_y,
            z          = ~move_dist,
            marker     = list(color        = ~move_dist,
                            size         = 3,
                            colorscale   = "Viridis",
                            opacity      = 0.8,
                            showscale  = FALSE)
        ) |>
        plotly::add_paths(
            data       = ball_path |>
            dplyr::select(step, ball_x, ball_y, move_dist) |>
            dplyr::mutate(base = 0) |>
            tidyr::pivot_longer(c(move_dist, base)) |>
            dplyr::group_by(step),
            x          = ~ball_x,
            y          = ~ball_y,
            z          = ~value,
            color      = ~value
        ) |>
        plotly::hide_colorbar() |>
        plotly::layout(
            scene = list(
                xaxis  = list(title = "ボールのX座標"),
                yaxis  = list(title = "ボールのY座標"),
                zaxis  = list(title = "ボールの飛距離"),
                camera = list(eye    = list(x = 0.8, y = -1.8, z =  0.8),
                                center = list(x = 0.0, y =  0.0, z = -0.2))
            )
        )
148

149
}