ggplot2畫散點圖展示恩比德面對不同的防守者的百回合得分

在一個籃球相關的公衆號的文章裏發現了一張圖片

第一感覺應該是是R語言的ggplot2包做出來的,這麼好的學習素材不重複一下豈不是可惜了,遂以關鍵詞“Joel Embiids Points Per 100 Possessions When Guarded By”搜索找到了原文https://www.reddit.com/r/nba/comments/bjuiy4/oc_joel_embiids_points_per_100_possessions/

有的評論提到原始數據來自這個鏈接 https://stats.nba.com/player/203954/matchups/?sort=POSS&dir=1&Season=2018-19&SeasonType=Regular%20Season&DateFrom=04%2F29%2F2019&DateTo=04%2F30%2F2019

但是自己還沒有研究出來如何在這個網站上找到特定的球員面對不同的防守者的得分的相關數據。

找到了原始代碼

df %>% 
  ggplot(aes(x = DEF_PLAYER_NAME, y = pts.per.100, size = total.poss, fill = pts.per.100)) + 
  geom_point(alpha = .75, shape = 21, color = 'black') + 
  coord_flip() +
  theme_custom() +
  labs(size = "Total Possessions", 
       title = "Joel Embiid's Points Per 100 Possessions When Guarded By ___", 
       subtitle = "Among players that guarded Embiid at least 50 possesions in 2018-19 (Regular Season + Playoffs)", 
       y = "Points per 100 possessions", 
       x = "") +
  scale_y_continuous(limits = c(15, 55), breaks = seq(15, 55, 5)) +
  scale_size_continuous(range = c(2, 7)) +
  theme(legend.position=c(0.15, 0.835), legend.background = element_rect(fill="floralwhite")) +
  scale_fill_gradient2(guide=FALSE, low = ("#0571b0"), mid = "white",
                         high = ("#ca0020"), midpoint = 38.6) + 
  geom_hline(yintercept = 38.6, linetype = 2, color = 'gray55') +
  theme(plot.title = element_text(face = 'bold', size = 11, hjust = 0.5))  +
  theme(plot.subtitle = element_text(face = 'italic', size = 9, hjust = 0.5)) + 
  annotate(geom = 'label', x= 3, y = 45, label = "Season average\n38.6 pts per 100", family = 'Gill Sans MT', fontface = 'bold', fill = 'floralwhite') +
  theme(plot.margin=unit(c(.75,.25,.5,0),"cm"))

那就不用原始數據了,根據圖片和代碼自己構造一份數據

根據以上代碼可以看到作圖的數據總共有三列

  • x是防守者的姓名
  • y是恩比德面對不同的對手百回合得分
  • 還有一列是恩比德面對不同的對手總共的回合數,用來控制點的大小
  • 恩比德面對不同的對手百回合得分 用來映射點的顏色

根據圖片可以看到總共有29個防守者,得分範圍在每百回合15到55之間,總共的回合數是50到200之間。

首先是構造數據
def_player_name<-paste0("Player","_",1:29)
pts.per.100<-seq(15,55,by=0.5)
total.poss<-seq(50,200,by=1)
df<-data.frame(def_player_name=sample(def_player_name,29,replace = F),
               pts.per.100=sample(pts.per.100,29,replace = F),
               total.poss=sample(total.poss,29,replace = F))
dim(df)
head(df)
基本的散點圖
library(ggplot2)
ggplot(df,aes(x=def_player_name,y=pts.per.100,
              size=total.poss,fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')
接下來就是對這幅圖進行美化

美化的內容包括

  • 旋轉90度
    用到的代碼是
ggplot(df,aes(x=def_player_name,y=pts.per.100,
              size=total.poss,fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()
  • 按照得分大小排序
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()

注意這裏是如何實現的,升序和降序是經常用到的

  • 改變主題
    原始代碼用到的是 theme_custom()這個函數。我暫時還不知道是哪個包裏的,或者是他自己寫的。這裏我們就不管了,換成theme_bw()這個函數
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()
  • 接下來是改變點的填充顏色和大小
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()+
  scale_fill_gradient2(guide=FALSE, 
                       low = ("#0571b0"), 
                       mid = "white",
                       high = ("#ca0020"), 
                       midpoint = 38.6)+
  scale_size_continuous(range = c(2, 7))
  • 更改y軸的顯示刻度,添加輔助線
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()+
  scale_fill_gradient2(guide=FALSE, 
                       low = ("#0571b0"), 
                       mid = "white",
                       high = ("#ca0020"), 
                       midpoint = 38.6)+
  scale_size_continuous(range = c(2, 20))+
  scale_y_continuous(limits = c(15, 55), breaks = seq(15, 55, 5)) +
  geom_hline(yintercept = 38.6, linetype = 2, color = 'gray55') 
  • 更改座標軸的標籤,添加標題
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()+
  scale_fill_gradient2(guide=FALSE, 
                       low = ("#0571b0"), 
                       mid = "white",
                       high = ("#ca0020"), 
                       midpoint = 38.6)+
  scale_size_continuous(range = c(2, 20))+
  scale_y_continuous(limits = c(15, 55), breaks = seq(15, 55, 5)) +
  geom_hline(yintercept = 38.6, linetype = 2, color = 'gray55')+
  labs(size = "Total Possessions", 
       title = "Joel Embiid's Points Per 100 Possessions When Guarded By ___", 
       subtitle = "Among players that guarded Embiid at least 50 possesions in 2018-19 (Regular Season + Playoffs)", 
       y = "Points per 100 possessions", 
       x = "") 
  • 添加一個註釋的標籤
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()+
  scale_fill_gradient2(guide=FALSE, 
                       low = ("#0571b0"), 
                       mid = "white",
                       high = ("#ca0020"), 
                       midpoint = 38.6)+
  scale_size_continuous(range = c(2, 20))+
  scale_y_continuous(limits = c(15, 55), breaks = seq(15, 55, 5)) +
  geom_hline(yintercept = 38.6, linetype = 2, color = 'gray55')+
  labs(size = "Total Possessions", 
       title = "Joel Embiid's Points Per 100 Possessions When Guarded By ___", 
       subtitle = "Among players that guarded Embiid at least 50 possesions in 2018-19 (Regular Season + Playoffs)", 
       y = "Points per 100 possessions", 
       x = "") +
  annotate(geom = 'label', x= 3, y = 45, 
           label = "Season average\n38.6 pts per 100", 
           family = 'Times New Roman', 
           fontface = 'bold', 
           fill = 'floralwhite') 
  • 最後就是對主題的一些細節設置了
ggplot(df,aes(x=reorder(def_player_name,pts.per.100),
              y=pts.per.100,
              size=total.poss,
              fill=pts.per.100))+
  geom_point(alpha=0.75,shape=21,color='black')+
  coord_flip()+
  theme_bw()+
  scale_fill_gradient2(guide=FALSE, 
                       low = ("#0571b0"), 
                       mid = "white",
                       high = ("#ca0020"), 
                       midpoint = 38.6)+
  scale_size_continuous(range = c(2, 20))+
  scale_y_continuous(limits = c(15, 55), breaks = seq(15, 55, 5)) +
  geom_hline(yintercept = 38.6, linetype = 2, color = 'gray55')+
  labs(size = "Total Possessions", 
       title = "Joel Embiid's Points Per 100 Possessions When Guarded By ___", 
       subtitle = "Among players that guarded Embiid at least 50 possesions in 2018-19 (Regular Season + Playoffs)", 
       y = "Points per 100 possessions", 
       x = "") +
  annotate(geom = 'label', x= 3, y = 45, 
           label = "Season average\n38.6 pts per 100", 
           family = 'Times New Roman', 
           fontface = 'bold', 
           fill = 'floralwhite') +
  theme(legend.position=c(0.15, 0.835), 
        legend.background = element_rect(fill="floralwhite"),
        plot.title = element_text(face = 'bold', size = 11, hjust = 0.5),
        plot.subtitle = element_text(face = 'italic', size = 9, hjust = 0.5),
        plot.margin=unit(c(.75,.25,.5,0),"cm"))

好了,今天的內容就到這裏了。

歡迎大家關注我的公衆號

小明的數據分析筆記本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章