2021-05-04ggplot2绘制雷达图

原文链接

数据获取和准备

devtools::install_github("JaseZiv/worldfootballR")#安装包
library(worldfootballR)  #用于网页信息爬取
library(tidyverse)       #ggplot, dplyr等
library(forcats)         #ggplot排序
library(glue)            #比paste()更简单
df <- fb_player_scouting_report(https://fbref.com/en/players/282679b4/Mateusz-Klich)
head(df)
 player_name    season         Statistic Per.90 Percentile      
1 Mateusz Klich 2020-2021 Non-Penalty Goals   0.04         44 
2 Mateusz Klich 2020-2021              npxG   0.06         52 
3 Mateusz Klich 2020-2021       Shots Total   1.19         69 
4 Mateusz Klich 2020-2021           Assists   0.21         92 
5 Mateusz Klich 2020-2021                xA   0.15         89 
6 Mateusz Klich 2020-2021           npxG+xA   0.21         81 

df <- df %>% 
      mutate(stat=case_when(Statistic == "Non-Penalty Goals"|
                            Statistic == "npxG"|
                            Statistic == "Shots Total"|
                            Statistic == "Assists"|
                            Statistic == "xA"|
                            Statistic == "npxG+xA"|
                            Statistic == "Shot-Creating Actions" ~ "Attacking",
                            Statistic == "Passes Attempted"|
                            Statistic == "Pass Completion %"|
                            Statistic == "Progressive Passes"|
                            Statistic == "Progressive Carries"|
                            Statistic == "Dribbles Completed"|
                            Statistic == "Touches (Att Pen)"|
                            Statistic == "Progressive Passes Rec" ~ "Possession",
                                 TRUE ~ "Defending"))
df_selected <- df[-c(6,14,15,16,18),]
df_selected <- df[c(1:5,7:13,17,19,20),]

绘图

ggplot(df_selected,aes(fct_reorder(Statistic,stat),Percentile)) +                       #选择绘图列并按照分类排序
  geom_bar(aes(y=100,fill=stat),stat="identity",width=1,colour="white",                 #先绘制整个披萨图
  alpha=0.5) +                                                                          #改变透明度
  geom_bar(stat="identity",width=1,aes(fill=stat),colour="white") +                     #插入数值
  coord_polar() +                                                                       #变成圆形
  geom_label(aes(label=Per.90,fill=stat),size=2,color="white",show.legend = FALSE)+     #为数值加标签,把'label=Per.90' 变成'label=Percentile' 以展示百分比 scale_fill_manual(values=c("Possession" = "#D70232",                                   #选择披萨图的填充颜色
                             "Attacking" = "#1A78CF",
                             "Defending" = "#FF9300")) +                                                              
  scale_y_continuous(limits = c(-10,100))+                                              #在中间添加白色圆圈  
  labs(fill="",                                                                         #去掉图例标题
       caption = "Data from StatsBomb via FBref",                                        #标题
       title=df_selected$player_name[1])+                                               #标题为运动员的名字 
  theme_minimal() +                                                                     #主题进行调整 
  theme(legend.position = "top",
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust=0.5),
        plot.caption = element_text(hjust=0.5,size=6),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) 
image.png

看上去还行,就是标签不大好。当然可以手动改,比较麻烦。用以下代码在R中改。

temp <- (360/(length(df_selected$Player))/2)                             #在标签间算出角度的差异并除以2
myAng <- seq(-temp, -360+temp, length.out = length(df_selected$Player))  #为标签算好角
ang<-ifelse(myAng < -90, myAng+180, myAng)                                    #为了可读性,在某些位置旋转标签
ang<-ifelse(ang < -90, ang+180, ang)      

有些标签很长,可以让一个单词站一行

df_selected$Statistic <- gsub(" ","\n",df_selected$Statistic)
ggplot(df_selected,aes(fct_reorder(Statistic,stat),Percentile)) +                       
    geom_bar(aes(y=100,fill=stat),stat="identity",width=1,colour="white",                 
             alpha=0.5) +                                                                         
    geom_bar(stat="identity",width=1,aes(fill=stat),colour="white") +                  
    coord_polar() +                                                                      
    geom_label(aes(label=Per90,fill=stat),size=2,color="white",show.legend = FALSE)+     
    scale_fill_manual(values=c("Possession" = "#D70232",                                  
                               "Attacking" = "#1A78CF",
                               "Defending" = "#FF9300")) +                                                              
    scale_y_continuous(limits = c(-10,100))+                                             
    labs(fill="",                                                                       
         caption = "Data from StatsBomb via FBref",                                       
         title=df_selected$Player[1])+                                           
    theme_minimal() +                                                                   
    theme(legend.position = "top",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 6, angle = ang),
          plot.title = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0.5,size=6),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank()) 
image.png

看上去好多了,现在可以按照你的意图随意更改图的细节了。

ggplot(df_selected,aes(fct_reorder(Statistic,stat),Percentile)) +                       
    geom_bar(aes(y=100),fill="#131313",stat="identity",width=1,colour="#797979",                 
             alpha=0.5,show.legend = FALSE) +         
    
    geom_bar(stat="identity",width=1,aes(fill=stat),colour="#F3FEFC",alpha=1) +                     
    coord_polar(clip = "off") +                                                                      
    geom_hline(yintercept=25, colour="#565656",linetype="longdash",alpha=0.5)+
    geom_hline(yintercept=50, colour="#565656",linetype="longdash",alpha=0.5)+
    geom_hline(yintercept=75, colour="#565656",linetype="longdash",alpha=0.5)+ 
    scale_fill_manual(values=c("Possession" = "#1ADA89",                                   
                               "Attacking" = "#0F70BF",
                               "Defending" = "#EC313A")) +                                                        
    geom_label(aes(label=Percentile,fill=stat),size=2,color="white",show.legend = FALSE)+ 
    scale_y_continuous(limits = c(-20,100))+                                              
    labs(fill="",   
         caption = "Data from StatsBomb via FBref\nStyle copied from The Athletic/@worville",     
         title=glue("{df_selected$Player[1]} | Leeds United"),
         subtitle = glue::glue("{df_selected$season} | Compared to midfielders Top 5 competitions | stats per 90"))+                                                
    theme_minimal() +                                                                     
    theme(plot.background = element_rect(fill = "#131313",color = "#131313"),
          panel.background = element_rect(fill = "#131313",color = "#131313"),
          legend.position = "bottom",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 6,colour = "#FFFFFF"),
          plot.subtitle = element_text(hjust=0.5,size=8),
          plot.caption = element_text(hjust=0.5,size=6),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          plot.margin = margin(5,4,2,4)) 
image.png
ggplot(df_selected,aes(fct_reorder(Statistic,stat),Percentile)) +                      
    geom_bar(aes(y=100),fill="#FAFBFD",stat="identity",width=1,colour="black",                 
             alpha=0.5) +                                                                          
    geom_bar(stat="identity",width=0.95,aes(fill=stat),colour=NA) +                    
    coord_polar(clip = "off") +                                                                       
    
    geom_hline(yintercept=25, colour="#CFD0D2",alpha=1,size=0.1)+
    geom_hline(yintercept=50, colour="#CFD0D2",alpha=1,size=0.1)+
    geom_hline(yintercept=75, colour="#CFD0D2",alpha=1,size=0.1)+ 
    geom_text(aes(label=Per90,fill=stat),size=2,color="black",show.legend = FALSE)+  
    scale_fill_manual(values=c("Possession" = "#F47294",                                   
                               "Attacking" = "#E7D96E",
                               "Defending" = "#8FBFEF")) +                                                              
    scale_y_continuous(limits = c(-10,110))+                                             
    labs(fill="",   
         caption = "Data from StatsBomb via FBref\nStyle copied from @FootballSlices",     
         title=glue("{df_selected$Player[1]} | Leeds United"),
         subtitle = glue::glue("{df_selected$season} | Compared to midfielders Top 5 competitions | stats per 90"))+                                               
    theme_minimal() +                                                                  
    theme(plot.background = element_rect(fill = "#FAFBFD",color = "#FAFBFD"),
          panel.background = element_rect(fill = "#FAFBFD",color = "#FAFBFD"),
          legend.position = "top",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 6),
          plot.subtitle = element_text(hjust=0.5,size=8),
          plot.caption = element_text(hjust=0.5,size=6),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          plot.margin = margin(5,2,2,2)) 
image.png
ggplot(df_selected,aes(fct_reorder(Statistic,stat),Percentile)) +                      
    geom_bar(aes(y=100),fill="#F2F4F5",stat="identity",width=1,colour="white",                
             alpha=1,linetype="dashed") +                                                                          
    geom_bar(stat="identity",width=1,fill="#D20222",colour="white") +   
    geom_hline(yintercept=25, colour="white",linetype="longdash",alpha=0.5)+
    geom_hline(yintercept=50, colour="white",linetype="longdash",alpha=0.5)+
    geom_hline(yintercept=75, colour="white",linetype="longdash",alpha=0.5)+ 
    geom_hline(yintercept=100, colour="white",alpha=0.5)+ 
    coord_polar() +                                                                     
    geom_label(aes(label=Per90),fill="#D20222",size=2,color="white",show.legend = FALSE)+     
    scale_fill_manual(values=c("Possession" = "#D70232",                                  
                               "Attacking" = "#1A78CF",
                               "Defending" = "#FF9300")) +                                                              
    scale_y_continuous(limits = c(-10,100))+                                              
    labs(fill="",   
         caption = "Data from StatsBomb via FBref",     
         #remove legend title
         title=glue("{df_selected$Player[1]} | Manchester United"),
         subtitle = glue::glue("{df_selected$season} | Compared to midfielders Top 5 competitions | stats per 90"))+                                               
    
    theme_minimal() +                                                                     
    theme(plot.background = element_rect(fill = "#F2F4F5",color = "#F2F4F5"),
          panel.background = element_rect(fill = "#F2F4F5",color = "#F2F4F5"),
          legend.position = "top",
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 6, angle = ang),
          plot.subtitle = element_text(hjust=0.5,size=8),
          plot.caption = element_text(hjust=0.5,size=6),
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          plot.margin = margin(5,2,2,2)) 
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容