将不同长度的简单图表合并到相同的图中

时间:2017-04-04 11:21:14

标签: r plot overlay overlap

所以我从一开始就要开始这么抱歉,如果它有点冗长。我的可运行代码就是......

structure(list(X1 = 0:29, seasoncode = c("E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015", "E2015", 
"E2015", "E2015", "E2015", "E2015", "E2015", "E2015"), gamecode = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), pbp_id = 1:30, 
period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), game_minute = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), game_clock = structure(c(36000, 
32400, 32400, 32400, 32400, 32220, 32220, 32160, 32160, 31980, 
30960, 30780, 30360, 30240, 29100, 28980, 28500, 28500, 27720, 
27720, 27600, 27540, 27360, 27240, 26940, 26820, 26580, 26580, 
26100, 25980), class = c("hms", "difftime"), units = "secs"), 
team_id = c(NA, "MAD", "MAD", "KHI", "KHI", "KHI", "KHI", 
"KHI", "MAD", "KHI", "KHI", "KHI", "MAD", "KHI", "KHI", "KHI", 
"MAD", "KHI", "MAD", "MAD", "KHI", "KHI", "KHI", "KHI", "KHI", 
"MAD", "KHI", "MAD", "MAD", "MAD"), team_name = c(NA, "Real Madrid", 
"Real Madrid", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Khimki Moscow Region", 
"Real Madrid", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Real Madrid", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Real Madrid", 
"Khimki Moscow Region", "Real Madrid", "Real Madrid", "Khimki Moscow Region", 
"Khimki Moscow Region", "Khimki Moscow Region", "Khimki Moscow Region", 
"Khimki Moscow Region", "Real Madrid", "Khimki Moscow Region", 
"Real Madrid", "Real Madrid", "Real Madrid"), player_id = c(NA, 
"P005927", "P005927", "P005800", "P001438", "P005800", "P001438", 
NA, "PAAX", "PBFX", "PBFX", "P001438", "PTGB", "P001438", 
"P005800", "P001438", "PBMT", "PBGV", "PJCC", "PJCC", "P005800", 
"P003367", "P003367", "P005800", "P003367", "P005927", "P005800", 
"PAAX", "PBMT", "PTGB"), player_name = c(NA, "AYON, GUSTAVO", 
"AYON, GUSTAVO", "HONEYCUTT, TYLER", "AUGUSTINE, JAMES", 
"HONEYCUTT, TYLER", "AUGUSTINE, JAMES", NA, "REYES, FELIPE", 
"MONIA, SERGEY", "MONIA, SERGEY", "AUGUSTINE, JAMES", "LLULL, SERGIO", 
"AUGUSTINE, JAMES", "HONEYCUTT, TYLER", "AUGUSTINE, JAMES", 
"FERNANDEZ, RUDY", "VYALTSEV, EGOR", "MACIULIS, JONAS", "MACIULIS, JONAS", 
"HONEYCUTT, TYLER", "RICE, TYRESE", "RICE, TYRESE", "HONEYCUTT, TYLER", 
"RICE, TYRESE", "AYON, GUSTAVO", "HONEYCUTT, TYLER", "REYES, FELIPE", 
"FERNANDEZ, RUDY", "LLULL, SERGIO"), player_jersey_number = c(NA, 
14L, 14L, 33L, 5L, 33L, 5L, NA, 9L, 12L, 12L, 5L, 23L, 5L, 
33L, 5L, 5L, 9L, 8L, 8L, 33L, 0L, 0L, 33L, 0L, 14L, 33L, 
9L, 5L, 23L), event_desc_id = c("BP", "2FGAB", "AG", "FV", 
"D", "3FGA", "O", "TO", "2FGA", "D", "3FGM", "AS", "TO", 
"ST", "3FGM", "AS", "RV", "CM", "2FGAB", "AG", "FV", "D", 
"3FGA", "O", "2FGA", "D", "CM", "RV", "3FGM", "AS"), event_desc = c("Begin Period", 
"Missed Two Pointer (0/1 -  0 pt)", "Shot Rejected (1)", 
"Block (1)", "Def Rebound (1)", "Missed Three Pointer (0/1 -  0 pt)", 
"Off Rebound (1)", "Turnover (1)", "Missed Two Pointer (0/1 -  0 pt)", 
"Def Rebound (1)", "Three Pointer (1/1 -  3 pt)", "Assist (1)", 
"Turnover (1)", "Steal (1)", "Three Pointer (1/2 -  3 pt)", 
"Assist (2)", "Foul Drawn (1)", "Foul (1)", "Missed Two Pointer (0/1 -  0 pt)", 
"Shot Rejected (1)", "Block (2)", "Def Rebound (1)", "Missed Three Pointer (0/1 -  0 pt)", 
"Off Rebound (1)", "Missed Two Pointer (0/1 -  0 pt)", "Def Rebound (1)", 
"Foul (1)", "Foul Drawn (1)", "Three Pointer (1/1 -  3 pt)", 
"Assist (1)"), score_home = c(NA, 0L, NA, NA, NA, 0L, NA, 
NA, 0L, NA, 3L, NA, NA, NA, 6L, NA, NA, NA, 6L, NA, NA, NA, 
6L, NA, 6L, NA, NA, NA, 6L, NA), score_visitor = c(NA, 0L, 
NA, NA, NA, 0L, NA, NA, 0L, NA, 0L, NA, NA, NA, 0L, NA, NA, 
NA, 0L, NA, NA, NA, 0L, NA, 0L, NA, NA, NA, 3L, NA), coord_x = c(NA, 
0L, NA, NA, NA, -501L, NA, NA, 269L, NA, -163L, NA, NA, NA, 
-690L, NA, NA, NA, 18L, NA, NA, NA, -539L, NA, 94L, NA, NA, 
NA, 702L, NA), coord_y = c(NA, 181L, NA, NA, NA, 602L, NA, 
NA, 188L, NA, 771L, NA, NA, NA, 319L, NA, NA, NA, 232L, NA, 
NA, NA, 526L, NA, 37L, NA, NA, NA, 370L, NA), `Points off OREB` = c(NA, 
NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
TWO.FGA = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L), TWO.FGM = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), THREE.FGA = c(NA, NA, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), THREE.FGM = c(NA, 
NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
Missed.Attemts = c(NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L), Made.Points = c(NA, NA, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X1", 
"seasoncode", "gamecode", "pbp_id", "period", "game_minute", 
"game_clock", "team_id", "team_name", "player_id", "player_name", 
"player_jersey_number", "event_desc_id", "event_desc", "score_home", 
"score_visitor", "coord_x", "coord_y", "Points off OREB", "TWO.FGA", 
"TWO.FGM", "THREE.FGA", "THREE.FGM", "Missed.Attemts", "Made.Points"
), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))

最终看起来像这样......

gamecode    pbp_id  period  team_id team_name   player_id   player_name event_desc_id   event_desc  coord_x coord_y Points off OREB TWO.FGA TWO.FGM THREE.FGA   THREE.FGM   Missed.Attemts  Made.Points

1              7       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5    O  Off Rebound (1)                     0   0   0   0   0   0   0
1              8       1        KHI Khimki Moscow Region                TO  Turnover (1)                                            0   0   0   0   0   0   0
1              9       1        MAD Real Madrid PAAX    REYES, FELIPE   9   2FGA    Missed Two Pointer (0/1 -  0 pt)                0   0   269 188 0   0   0   0   0   0   0
1             10       1        KHI Khimki Moscow Region    PBFX    MONIA, SERGEY   12  D   Def Rebound (1)                         0   0   0   0   0   0   0
1             11       1        KHI Khimki Moscow Region    PBFX    MONIA, SERGEY   12  3FGM    Three Pointer (1/1 -  3 pt)         3   0   -163    771 0   0   0   0   0   0   0
1             12       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   AS  Assist (1)                          0   0   0   0   0   0   0
1             13       1        MAD Real Madrid PTGB    LLULL, SERGIO   23  TO  Turnover (1)                                        0   0   0   0   0   0   0
1             14       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   ST  Steal (1)                       0   0   0   0   0   0   0
1             15       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  3FGM    Three Pointer (1/2 -  3 pt) 6   0   -690    319 0   0   0   0   0   0   0
1             16       1        KHI Khimki Moscow Region    P001438 AUGUSTINE, JAMES    5   AS  Assist (2)                      0   0   0   0   0   0   0   
1             17       1        MAD Real Madrid PBMT    FERNANDEZ, RUDY 5   RV  Foul Drawn (1)                                      0   0   0   0   0   0   0   
1             18       1        KHI Khimki Moscow Region    PBGV    VYALTSEV, EGOR  9   CM  Foul (1)                            0   0   0   0   0   0   0
1             19       1        MAD Real Madrid PJCC    MACIULIS, JONAS 8   2FGAB   Missed Two Pointer (0/1 -  0 pt)                6   0   18  232 0   0   0   0   0   0   0
1             20       1        MAD Real Madrid PJCC    MACIULIS, JONAS 8   AG  Shot Rejected (1)                                   0   0   0   0   0   0   0
1             21       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  FV  Block (2)                       0   0   0   0   0   0   0
1             22       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   D   Def Rebound (1)                     0   0   0   0   0   0   0
1             23       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   3FGA    Missed Three Pointer (0/1 -  0 pt)  6   0   -539    526 0   0   0   0   0   0   0
1             24       1        KHI Khimki Moscow Region    P005800 HONEYCUTT, TYLER    33  O   Off Rebound (1)                 0   0   0   0   0   0   0
1             25       1        KHI Khimki Moscow Region    P003367 RICE, TYRESE    0   2FGA    Missed Two Pointer (0/1 -  0 pt)    6   0   94  37  0   1   0   0   0   1   0

格式化有点混乱。它有大约10万行,包含一堆团队,名字,游戏和一切。并非所有列都具有值。例如,没有" x_coord"助攻,失误,抢断......另外需要注意的是,从第34栏开始,关注OREB"直到最后一个是我在excel中制作的列,只有1或0,取决于该特定行的真或假。

最终目标。我想用来自" coord_x"的数据制作数据图或热图或任何视觉效果。和" coord_y"列值仅适用于2和3的FG%以及3次进攻篮板,所有篮球场都在后台。我想这个情节是一个篮球场,绿点是制造和红色未命中。或者看酷的热图。

我觉得我很亲近。我有3点制作绿点的情节,错过3点的红点和篮球场

#########################
MADE 3 SHOT MAP ATTEMPT
#########################
Team3Made<- subset(Practice_FGAM, gamecode >= 1 & THREE.FGM == 1)

x3M <- Team3Made$coord_x
y3M <- Team3Made$coord_y

p <- ggplot(data = Team3Made, aes(x3M, y3M))
p + geom_point(color='green')
Made3 <- p + geom_point(color='green')





###########################
MISSED 3 SHOT MAP ATTEMPT
###########################
Team3ATT<- subset(Practice_FGAM, gamecode >= 1 & THREE.FGA == 1)

x3A <- Team3ATT$coord_x
y3A <- Team3ATT$coord_y

m <- ggplot(data = Team3ATT, aes(x3A, y3A))
m + geom_point(color='red')
Missed3 <- m + geom_point(color='red')


############################
Basketball Court that's NOT TO SCALE
############################

library(grid)
library(jpeg)
library(RCurl)


courtImg.URL <- "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"
court <- rasterGrob(readJPEG(getURLContent(https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg)),
                width=unit(1,"npc"), height=unit(1,"npc"))

ggplot(Team3Made, aes(x3M, y3M)) + 
 annotation_custom(court, -250, 250, -50, 420) +
 xlim(-250, 250) +
 ylim(-50, 420) +
 geom_point(data = Team3Made, colour = "red", na.rm = TRUE)

我理想的结束代码无效,导致Error: Don't know how to add o to a plot

############################
Ideal End Code (ggplot = court, Missed3 = red dots, Made3 = green dots)
############################
ggplot(Team3Made, aes(x3M, y3M)) + 
 annotation_custom(court, -250, 250, -50, 420) +
 xlim(-250, 250) +
 ylim(-50, 420) +
 geom_point(data = Team3Made, colour = "red", na.rm = TRUE) + 
 plot(Missed3) +
 plot(Made3)

我会发布图片,但我是新的,它不会让我。想象一下,在一条想象中的3点线后面有一堆红点,另一条有绿点的情节,以及一个看起来就像一个篮球和篮板在底部的篮球半场的轮廓的情节。

在我继续做2指针之前,我想尝试将Made3,Missed3和球场叠加到一个视觉上。我已经阅读并尝试了所有内容,并且觉得我错过了最简单的步骤。所以问题就变成了实现这一目标的最佳方法。我不在乎是否要立即扩展,因为我可以稍后调整(尽管我不会拒绝任何帮助)

非常感谢你,只是想说这是一个非常棒的社区

0 个答案:

没有答案