2021-05-04ggplot2绘制雷达图

作者: iColors | 来源:发表于2021-05-06 11:46 被阅读0次

    原文链接

    数据获取和准备

    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

    相关文章

      网友评论

        本文标题:2021-05-04ggplot2绘制雷达图

        本文链接:https://www.haomeiwen.com/subject/upnhdltx.html