Module 03: FIFA 19 Statistics

Detailed attributes for every player registered in the latest edition of the FIFA 19 database.

Data Source: https://www.kaggle.com/karangadiya/fifa19/downloads/fifa19.zip/4

Research Question(s)

  • What is the mean and relative frequency distribution of ‘overall’ and ‘agility’ ratings for players from Uruguay and Argentina?
  • Is there a difference in agility between players from Uruguay and Argentina?

Initial Data Inspection

#FIFA '19 dataset
#https://www.kaggle.com/karangadiya/fifa19/downloads/fifa19.zip/4

#import the data from a .csv file
data <- as_tibble(read.csv("fifa19.csv", header=TRUE))

#check header for info
colnames(data)
##  [1] "X"                        "ID"                      
##  [3] "Name"                     "Age"                     
##  [5] "Photo"                    "Nationality"             
##  [7] "Flag"                     "Overall"                 
##  [9] "Potential"                "Club"                    
## [11] "Club.Logo"                "Value"                   
## [13] "Wage"                     "Special"                 
## [15] "Preferred.Foot"           "International.Reputation"
## [17] "Weak.Foot"                "Skill.Moves"             
## [19] "Work.Rate"                "Body.Type"               
## [21] "Real.Face"                "Position"                
## [23] "Jersey.Number"            "Joined"                  
## [25] "Loaned.From"              "Contract.Valid.Until"    
## [27] "Height"                   "Weight"                  
## [29] "LS"                       "ST"                      
## [31] "RS"                       "LW"                      
## [33] "LF"                       "CF"                      
## [35] "RF"                       "RW"                      
## [37] "LAM"                      "CAM"                     
## [39] "RAM"                      "LM"                      
## [41] "LCM"                      "CM"                      
## [43] "RCM"                      "RM"                      
## [45] "LWB"                      "LDM"                     
## [47] "CDM"                      "RDM"                     
## [49] "RWB"                      "LB"                      
## [51] "LCB"                      "CB"                      
## [53] "RCB"                      "RB"                      
## [55] "Crossing"                 "Finishing"               
## [57] "HeadingAccuracy"          "ShortPassing"            
## [59] "Volleys"                  "Dribbling"               
## [61] "Curve"                    "FKAccuracy"              
## [63] "LongPassing"              "BallControl"             
## [65] "Acceleration"             "SprintSpeed"             
## [67] "Agility"                  "Reactions"               
## [69] "Balance"                  "ShotPower"               
## [71] "Jumping"                  "Stamina"                 
## [73] "Strength"                 "LongShots"               
## [75] "Aggression"               "Interceptions"           
## [77] "Positioning"              "Vision"                  
## [79] "Penalties"                "Composure"               
## [81] "Marking"                  "StandingTackle"          
## [83] "SlidingTackle"            "GKDiving"                
## [85] "GKHandling"               "GKKicking"               
## [87] "GKPositioning"            "GKReflexes"              
## [89] "Release.Clause"
head(data)
dim(data) #18207 rows x 89 columns
## [1] 18207    89
#investigate data types
str(data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    18207 obs. of  89 variables:
##  $ X                       : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ ID                      : int  158023 20801 190871 193080 192985 183277 177003 176580 155862 200389 ...
##  $ Name                    : Factor w/ 17194 levels "A. Ábalos","A. Abang",..: 9676 3192 12552 4169 8661 4458 9684 9892 15466 7822 ...
##  $ Age                     : int  31 33 26 27 27 27 32 31 32 25 ...
##  $ Photo                   : Factor w/ 18207 levels "https://cdn.sofifa.org/players/4/19/100803.png",..: 567 6032 3132 3468 3453 1986 1447 1391 484 4443 ...
##  $ Nationality             : Factor w/ 164 levels "Afghanistan",..: 7 124 21 141 14 14 36 159 141 138 ...
##  $ Flag                    : Factor w/ 164 levels "https://cdn.sofifa.org/flags/1.png",..: 123 108 125 115 138 138 2 132 115 114 ...
##  $ Overall                 : int  94 94 92 91 91 91 91 91 91 90 ...
##  $ Potential               : int  94 94 93 93 92 91 91 91 91 93 ...
##  $ Club                    : Factor w/ 652 levels ""," SSV Jahn Regensburg",..: 215 330 437 377 376 138 474 215 474 62 ...
##  $ Club.Logo               : Factor w/ 679 levels "https://cdn.sofifa.org/flags/103.png",..: 491 553 638 90 30 577 493 491 493 490 ...
##  $ Value                   : Factor w/ 217 levels "€0","€1.1M","€1.2M",..: 17 196 19 191 13 214 183 202 155 184 ...
##  $ Wage                    : Factor w/ 144 levels "€0","€100K","€105K",..: 95 75 56 50 67 65 78 82 71 138 ...
##  $ Special                 : int  2202 2228 2143 1471 2281 2142 2280 2346 2201 1331 ...
##  $ Preferred.Foot          : Factor w/ 3 levels "","Left","Right": 2 3 3 3 3 3 3 3 3 3 ...
##  $ International.Reputation: int  5 5 5 4 4 4 4 5 4 3 ...
##  $ Weak.Foot               : int  4 4 5 3 5 4 4 4 3 3 ...
##  $ Skill.Moves             : int  4 5 5 1 4 4 4 3 3 1 ...
##  $ Work.Rate               : Factor w/ 10 levels "","High/ High",..: 10 3 4 10 2 4 2 4 4 10 ...
##  $ Body.Type               : Factor w/ 11 levels "","Akinfenwa",..: 6 3 7 5 8 8 5 8 8 8 ...
##  $ Real.Face               : Factor w/ 3 levels "","No","Yes": 3 3 3 3 3 3 3 3 3 3 ...
##  $ Position                : Factor w/ 28 levels "","CAM","CB",..: 23 28 16 7 21 13 21 25 20 7 ...
##  $ Jersey.Number           : int  10 7 10 1 7 10 10 9 15 1 ...
##  $ Joined                  : Factor w/ 1737 levels "","Apr 1, 2008",..: 776 796 249 783 255 784 105 801 98 836 ...
##  $ Loaned.From             : Factor w/ 342 levels "","1. FC Köln",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Contract.Valid.Until    : Factor w/ 37 levels "","2018","2019",..: 5 6 6 4 7 4 4 5 4 5 ...
##  $ Height                  : Factor w/ 22 levels "","5'1","5'10",..: 10 15 12 17 4 11 11 13 13 15 ...
##  $ Weight                  : Factor w/ 58 levels "","110lbs","115lbs",..: 23 34 19 27 21 25 17 37 33 38 ...
##  $ LS                      : Factor w/ 94 levels "","31+2","32+2",..: 93 94 88 1 85 87 75 92 67 1 ...
##  $ ST                      : Factor w/ 94 levels "","31+2","32+2",..: 93 94 88 1 85 87 75 92 67 1 ...
##  $ RS                      : Factor w/ 94 levels "","31+2","32+2",..: 93 94 88 1 85 87 75 92 67 1 ...
##  $ LW                      : Factor w/ 106 levels "","25+2","27+2",..: 106 105 105 1 104 105 101 103 71 1 ...
##  $ LF                      : Factor w/ 103 levels "","27+2","29+2",..: 103 102 101 1 98 100 95 99 68 1 ...
##  $ CF                      : Factor w/ 103 levels "","27+2","29+2",..: 103 102 101 1 98 100 95 99 68 1 ...
##  $ RF                      : Factor w/ 103 levels "","27+2","29+2",..: 103 102 101 1 98 100 95 99 68 1 ...
##  $ RW                      : Factor w/ 106 levels "","25+2","27+2",..: 106 105 105 1 104 105 101 103 71 1 ...
##  $ LAM                     : Factor w/ 102 levels "","27+2","28+2",..: 102 100 101 1 100 101 99 97 69 1 ...
##  $ CAM                     : Factor w/ 102 levels "","27+2","28+2",..: 102 100 101 1 100 101 99 97 69 1 ...
##  $ RAM                     : Factor w/ 102 levels "","27+2","28+2",..: 102 100 101 1 100 101 99 97 69 1 ...
##  $ LM                      : Factor w/ 101 levels "","27+2","28+2",..: 101 99 99 1 99 100 98 96 70 1 ...
##  $ LCM                     : Factor w/ 93 levels "","30+2","31+2",..: 88 83 83 1 92 85 93 79 70 1 ...
##  $ CM                      : Factor w/ 93 levels "","30+2","31+2",..: 88 83 83 1 92 85 93 79 70 1 ...
##  $ RCM                     : Factor w/ 93 levels "","30+2","31+2",..: 88 83 83 1 92 85 93 79 70 1 ...
##  $ RM                      : Factor w/ 101 levels "","27+2","28+2",..: 101 99 99 1 99 100 98 96 70 1 ...
##  $ LWB                     : Factor w/ 96 levels "","30+2","31+2",..: 55 58 58 1 83 60 93 67 91 1 ...
##  $ LDM                     : Factor w/ 100 levels "","28+2","29+2",..: 50 51 49 1 84 55 92 66 97 1 ...
##  $ CDM                     : Factor w/ 100 levels "","28+2","29+2",..: 50 51 49 1 84 55 92 66 97 1 ...
##  $ RDM                     : Factor w/ 100 levels "","28+2","29+2",..: 50 51 49 1 84 55 92 66 97 1 ...
##  $ RWB                     : Factor w/ 96 levels "","30+2","31+2",..: 55 58 58 1 83 60 93 67 91 1 ...
##  $ LB                      : Factor w/ 99 levels "","29+2","30+2",..: 49 54 52 1 79 52 91 65 99 1 ...
##  $ LCB                     : Factor w/ 109 levels "","25+2","27+2",..: 30 44 31 1 71 35 81 65 109 1 ...
##  $ CB                      : Factor w/ 109 levels "","25+2","27+2",..: 30 44 31 1 71 35 81 65 109 1 ...
##  $ RCB                     : Factor w/ 109 levels "","25+2","27+2",..: 30 44 31 1 71 35 81 65 109 1 ...
##  $ RB                      : Factor w/ 99 levels "","29+2","30+2",..: 49 54 52 1 79 52 91 65 99 1 ...
##  $ Crossing                : int  84 84 79 17 93 81 86 77 66 13 ...
##  $ Finishing               : int  95 94 87 13 82 84 72 93 60 11 ...
##  $ HeadingAccuracy         : int  70 89 62 21 55 61 55 77 91 15 ...
##  $ ShortPassing            : int  90 81 84 50 92 89 93 82 78 29 ...
##  $ Volleys                 : int  86 87 84 13 82 80 76 88 66 13 ...
##  $ Dribbling               : int  97 88 96 18 86 95 90 87 63 12 ...
##  $ Curve                   : int  93 81 88 21 85 83 85 86 74 13 ...
##  $ FKAccuracy              : int  94 76 87 19 83 79 78 84 72 14 ...
##  $ LongPassing             : int  87 77 78 51 91 83 88 64 77 26 ...
##  $ BallControl             : int  96 94 95 42 91 94 93 90 84 16 ...
##  $ Acceleration            : int  91 89 94 57 78 94 80 86 76 43 ...
##  $ SprintSpeed             : int  86 91 90 58 76 88 72 75 75 60 ...
##  $ Agility                 : int  91 87 96 60 79 95 93 82 78 67 ...
##  $ Reactions               : int  95 96 94 90 91 90 90 92 85 86 ...
##  $ Balance                 : int  95 70 84 43 77 94 94 83 66 49 ...
##  $ ShotPower               : int  85 95 80 31 91 82 79 86 79 22 ...
##  $ Jumping                 : int  68 95 61 67 63 56 68 69 93 76 ...
##  $ Stamina                 : int  72 88 81 43 90 83 89 90 84 41 ...
##  $ Strength                : int  59 79 49 64 75 66 58 83 83 78 ...
##  $ LongShots               : int  94 93 82 12 91 80 82 85 59 12 ...
##  $ Aggression              : int  48 63 56 38 76 54 62 87 88 34 ...
##  $ Interceptions           : int  22 29 36 30 61 41 83 41 90 19 ...
##  $ Positioning             : int  94 95 89 12 87 87 79 92 60 11 ...
##  $ Vision                  : int  94 82 87 68 94 89 92 84 63 70 ...
##  $ Penalties               : int  75 85 81 40 79 86 82 85 75 11 ...
##  $ Composure               : int  96 95 94 68 88 91 84 85 82 70 ...
##  $ Marking                 : int  33 28 27 15 68 34 60 62 87 27 ...
##  $ StandingTackle          : int  28 31 24 21 58 27 76 45 92 12 ...
##  $ SlidingTackle           : int  26 23 33 13 51 22 73 38 91 18 ...
##  $ GKDiving                : int  6 7 9 90 15 11 13 27 11 86 ...
##  $ GKHandling              : int  11 11 9 85 13 12 9 25 8 92 ...
##  $ GKKicking               : int  15 15 15 87 5 6 7 31 9 78 ...
##  $ GKPositioning           : int  14 14 15 88 10 8 14 33 7 88 ...
##  $ GKReflexes              : int  8 11 11 94 13 8 9 37 11 89 ...
##  $ Release.Clause          : Factor w/ 1245 levels "","€1.1M","€1.2M",..: 295 84 296 106 234 189 104 170 24 123 ...

Variable Selection and Filtering

We need to keep some identification variables and those that would help us answer our research question.

#===============================
#SELECTION OF SPECIFIC VARIABLES
#===============================

#Use 'select' and the pipeline operator to create a new tibble containing
#only your variables of interest
selectedData <- data %>%  select(X, ID, Name, Age, Club, Nationality, Value, Overall, Preferred.Foot, Agility)

#check your new tibble
head(selectedData)
dim(selectedData) 
## [1] 18207    10
#check for rows containing NA values
selectedData[!complete.cases(selectedData),] 
#remove rows with NA values
selectedData<- na.omit(selectedData)

#check your data again
dim(selectedData) 
## [1] 18159    10

We would like to examine only the players from our countries of interest. We do not need any players from other countries.

#===============================
#FILTERING TO SPECIFIC VALUES
#===============================
#filter to "left" OR "right". The vertical bar | symbol indicates "or". 
#filteredData <- filter(selectedData, Preferred.Foot == "Left" | Preferred.Foot ==  "Right")
filteredData <- filter(selectedData, Nationality == "Uruguay" | Nationality ==  "Argentina")

#see new dimensions -- how did this change?
dim(filteredData)
## [1] 1085   10
#check data
head(filteredData)

Calculations and Data Visualization

Histogram: Overall Score

#set some variables for use in the plot
#they are set up here to make it easy to change
#if you have to type it more than once, you might consider making a variable
#it is also perhaps easier to read?

#histogram bin width
binWidth = 5

#alpha value for fill colors
transparency = 0.5

#colors for each category 
colorA  = "#E69F00" #Argentina
colorB  = "#56B4E9" #Uruguay

#maximum height for y axis
yMax = 0.07

#we will need the means later so we can just calculate that here
#we store the output of the group_by and summarise functions in a new data structure
overallMeans <- filteredData %>% 
        group_by(Nationality) %>% 
        summarise(mOverall = mean(Overall, na.rm = TRUE))

#print the results
overallMeans
#set up the main plot
overallPlot <- ggplot(filteredData,aes(x=Overall, fill=Nationality)) + 
               geom_histogram(data=filter(selectedData, Nationality == "Argentina"), 
                     binwidth = binWidth,
                     alpha = transparency,
                     position="identity", 
                     aes(y = ..density..), 
                     color="black") +
               geom_histogram(data=filter(selectedData, Nationality == "Uruguay"),
                     binwidth = binWidth,
                     alpha = transparency, 
                     position="identity", 
                     aes(y = ..density..), 
                     color="black") +
               scale_fill_manual(values=c(colorA,colorB), 
                     name="Nationality",
                     labels=c("Argentina", "Uruguay"))+
               #scale_x_continuous(breaks = seq(0,100,by=10),     limits = c(0,100))+
               scale_y_continuous(breaks = seq(0,yMax,by=0.01), limits = c(0,yMax))
               
#add some lines for the means using the other data we obtained earlier
overallPlot + 
  geom_vline(xintercept = overallMeans$mOverall[1], linetype="dashed", color = colorA, size=1.0) +
  geom_vline(xintercept = overallMeans$mOverall[2], linetype="dashed", color = colorB, size=1.0) +
  theme_minimal()+
  labs(x="Overall", y = "Density") +
  theme(legend.position="bottom")

#another way
ggplot(filteredData,aes(x=Overall,fill=Nationality))+ 
    geom_histogram(data=subset(filteredData,Nationality=='Argentina'),
                   aes(y = ..density..),
                   alpha=0.5, 
                   binwidth = binWidth)+
    geom_histogram(data=subset(filteredData,Nationality=='Uruguay'),
                   aes(y = ..density..),
                   alpha=0.5, 
                   binwidth = binWidth)+
    scale_fill_manual(name="Nationality", 
                      values=c(colorA,colorB),
                      labels=c("Argentina","Uruguay")) + 
    theme_minimal()

Histogram: Agility Score

#histogram bin width
binWidth = 5

#alpha value for fill colors
transparency = 0.5

#colors for each category 
colorA  = "#E69F00"
colorB  = "#56B4E9"

#maximum height for y axis
yMax = 0.04

#group by Nationality and find means 
agilityMeans <- filteredData %>% 
        group_by(Nationality) %>% 
        summarise(mAgility = mean(Agility, na.rm=TRUE))

#print
agilityMeans
#set up plot
agilityPlot <- ggplot(filteredData,aes(x=Agility, fill=Nationality)) + 
               geom_histogram(data=filter(selectedData, Nationality == "Argentina"), 
                     binwidth = binWidth,
                     alpha = transparency,
                     position="identity", 
                     aes(y = ..density..), 
                     color="black") +
               geom_histogram(data=filter(selectedData, Nationality == "Uruguay"),
                     binwidth = binWidth,
                     alpha = transparency, 
                     position="identity", 
                     aes(y = ..density..), 
                     color="black") +
               scale_fill_manual(values=c(colorA,colorB), 
                     name="Nationality",
                     labels=c("Argentina", "Uruguay")) +
               #scale_x_continuous(breaks = seq(0,100,by=10),     limits = c(0,100))+
               scale_y_continuous(breaks = seq(0,yMax,by=0.01), limits = c(0,yMax))
               
#add lines for the means using the summary data
agilityPlot + 
  geom_vline(xintercept = agilityMeans$mAgility[1], linetype="dashed", color = colorA, size=1.0) +
  geom_vline(xintercept = agilityMeans$mAgility[2], linetype="dashed", color = colorB, size=1.0) +
  theme_minimal()+
  labs(x="Agility", y = "Density") +
  theme(legend.position="bottom")

Density Plot: Agility Score

A Density Plot visualises the distribution of data over a continuous interval or time period. This chart is a variation of a Histogram that uses kernel smoothing to plot values, allowing for smoother distributions by smoothing out the noise. The peaks of a Density Plot help display where values are concentrated over the interval.

An advantage Density Plots have over Histograms is that they’re better at determining the distribution shape because they’re not affected by the number of bins used (each bar used in a typical histogram). A Histogram comprising of only 4 bins wouldn’t produce a distinguishable enough shape of distribution as a 20-bin Histogram would. However, with Density Plots, this isn’t an issue.

Source: https://datavizcatalogue.com/methods/density_plot.html

#maximum height for y axis
yMax = 0.035

#set up the plot
densityPlot <- ggplot(filteredData,aes(x=Agility, fill=Nationality)) + 
               geom_density(alpha=transparency, col="#00000055")+
               scale_fill_manual(values=c(colorA,colorB), 
                     name="Nationality",
                     breaks=c("Argentina", "Uruguay"),
                     labels=c("Argentina", "Uruguay"))+
               theme_minimal()+
               labs(x="Agility", y = "Density") +
               theme(legend.position="bottom")+
               scale_y_continuous(breaks = seq(0,yMax,by=0.005), limits = c(0,yMax))

#draw the plot
densityPlot

Scatterplot: Three Dimensions of Data (Interactive)

Let’s take a look at the Agility and Overall scores for older (40+) FIFA players. We can use a 3D scatter plot for some initial exploration. These interactive plots may not always be a great choice for publication, but they can quickly lead to some new observations in some cases.

library(plotly) #use the plotly library for easy interactivity
library(shiny) #shiny is used for interactive webpages but we just need it to help center this graphic

#filter by Age
filteredByAgeData <- filter(filteredData, Age >= 35)

#set up the plot (See plotly documentation for more info)
p <- plot_ly(filteredByAgeData, 
             x = ~Agility, 
             y = ~Age, 
             z = ~Overall, 
             text = ~paste('Name:',Name, 
                           '<br>Age:', Age,
                           '<br>Nationality:', Nationality,
                           '<br>Club:', Club,
                           '<br>Overall:', Overall,
                           '<br>Agility:', Agility), 
             color = ~as.factor(filteredByAgeData$Nationality), 
             colors = c(colorA, colorB)
             ) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'Agility'),
                     yaxis = list(title = 'Age'),
                     zaxis = list(title = 'Overall')))

#use Shiny to wrap this in a div so it will center properly
#div tags are from html code to create divisions on web pages
div(p, align = "center")

Joint Kernel Density Estimate: 3D (Interactive)

We can think of a joint density as two dimensions of observed data and one dimension of calculated density. There are many ways to plot this, but the Plotly library gives us the ability to quickly render an interactive 3D plot which could, again, be useful in some cases, but not likely for publication.

For this, we can use the kde2d function with two input variables. The output is X,Y, and Z coordinates where Z is a matrix which represents the density at each intersection.

https://www.rdocumentation.org/packages/MASS/versions/7.3-51.4/topics/kde2d

Note: This plot uses the Viridis color palette.

https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html

Other color palette options can be seen in the Documentation for Plotly.

“Alternatively, colorscale may be a palette name string of the following list: Greys,YlGnBu,Greens,YlOrRd,Bluered,RdBu,Reds,Blues,Picnic,Rainbow,Portland,Jet,Hot,Blackbody,Earth,Electric,Viridis,Cividis.”

Source: Plotly Documentation: https://plot.ly/r/reference/

library(MASS) #"Functions and datasets to support Venables and Ripley, "Modern Applied Statistics with S" (4th edition, 2002)."
library(shiny) 

#obtain a kernel density estimate for selectedData for Age vs Overall
kd <- with(selectedData, kde2d(Age, Overall))

#check the KDE output to see what it looks like
#X and Y coords, Z matrix of densities at each (x,y) coord
head(kd)
## $x
##  [1] 16.00000 17.20833 18.41667 19.62500 20.83333 22.04167 23.25000
##  [8] 24.45833 25.66667 26.87500 28.08333 29.29167 30.50000 31.70833
## [15] 32.91667 34.12500 35.33333 36.54167 37.75000 38.95833 40.16667
## [22] 41.37500 42.58333 43.79167 45.00000
## 
## $y
##  [1] 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90
## [24] 92 94
## 
## $z
##               [,1]         [,2]         [,3]         [,4]         [,5]
##  [1,] 3.643008e-05 1.328420e-04 2.894704e-04 3.809084e-04 3.297089e-04
##  [2,] 9.517950e-05 4.225062e-04 9.341709e-04 1.220396e-03 1.491282e-03
##  [3,] 1.259237e-04 4.639893e-04 1.147969e-03 1.913088e-03 2.646468e-03
##  [4,] 4.426838e-05 2.419467e-04 8.065223e-04 1.541005e-03 2.334248e-03
##  [5,] 1.026240e-05 1.099965e-04 4.728507e-04 1.065396e-03 1.735764e-03
##  [6,] 2.568281e-06 3.299784e-05 1.949006e-04 5.595471e-04 9.629142e-04
##  [7,] 3.652866e-07 8.303468e-06 9.061721e-05 2.847804e-04 5.611249e-04
##  [8,] 1.550717e-08 5.076986e-06 5.459848e-05 1.187462e-04 3.030508e-04
##  [9,] 1.369788e-08 5.519013e-06 4.677845e-05 5.358828e-05 1.946307e-04
## [10,] 1.438464e-08 5.741687e-06 4.487014e-05 2.647622e-05 8.744705e-05
## [11,] 2.769403e-09 1.223895e-06 1.739196e-05 3.061577e-05 8.208738e-05
## [12,] 2.616376e-10 1.310125e-07 4.581235e-06 2.843829e-05 7.385552e-05
## [13,] 6.054991e-09 1.349279e-06 1.255612e-05 2.323828e-05 2.676547e-05
## [14,] 3.088523e-07 3.274289e-06 9.534117e-06 1.388997e-05 1.192268e-05
## [15,] 1.698516e-06 1.251832e-05 2.705918e-06 5.997482e-06 8.897995e-06
## [16,] 4.639433e-07 3.408584e-06 5.549928e-07 6.398574e-06 3.287522e-05
## [17,] 6.242805e-09 4.593822e-08 2.263761e-07 1.347430e-05 3.017471e-05
## [18,] 4.138643e-12 7.910909e-11 1.212668e-07 6.794820e-06 1.561991e-05
## [19,] 4.155426e-14 8.968254e-10 3.892624e-07 6.113831e-06 1.845616e-05
## [20,] 2.673750e-13 5.764214e-09 2.409299e-06 2.493796e-05 1.515473e-05
## [21,] 6.959490e-13 5.267749e-09 2.117569e-06 1.719128e-05 4.236167e-06
## [22,] 1.405578e-09 1.095343e-08 2.506099e-07 1.855929e-06 2.990115e-07
## [23,] 2.160408e-07 1.587195e-06 2.356637e-07 9.775573e-07 9.659894e-07
## [24,] 1.635762e-06 1.201749e-05 1.771003e-06 7.303562e-06 7.299433e-06
## [25,] 6.100979e-07 4.482219e-06 6.605391e-07 2.724042e-06 2.722503e-06
##               [,6]         [,7]         [,8]         [,9]        [,10]
##  [1,] 2.931462e-04 3.211586e-04 2.421444e-04 1.867937e-04 1.381084e-04
##  [2,] 1.471555e-03 1.533144e-03 1.290541e-03 1.009071e-03 7.094359e-04
##  [3,] 2.979604e-03 2.998346e-03 3.080519e-03 2.550007e-03 1.953970e-03
##  [4,] 3.252378e-03 3.756343e-03 4.435533e-03 4.156213e-03 3.435170e-03
##  [5,] 2.635364e-03 3.603435e-03 4.360415e-03 4.811860e-03 4.717164e-03
##  [6,] 1.661031e-03 2.677728e-03 3.754429e-03 4.633542e-03 5.211521e-03
##  [7,] 1.078757e-03 1.694542e-03 2.876114e-03 4.089349e-03 4.933534e-03
##  [8,] 6.248276e-04 1.157921e-03 2.198879e-03 3.402200e-03 4.687108e-03
##  [9,] 4.162334e-04 8.014722e-04 1.600392e-03 3.013301e-03 4.602783e-03
## [10,] 2.417068e-04 5.785808e-04 1.256862e-03 2.602060e-03 3.985951e-03
## [11,] 2.125241e-04 4.751916e-04 1.070454e-03 2.034342e-03 3.165752e-03
## [12,] 1.501097e-04 2.730653e-04 7.431611e-04 1.590938e-03 2.723299e-03
## [13,] 9.987349e-05 2.408198e-04 5.143130e-04 1.305559e-03 2.268115e-03
## [14,] 7.199691e-05 1.936297e-04 4.601432e-04 1.158272e-03 1.679519e-03
## [15,] 3.984347e-05 1.429185e-04 3.078353e-04 7.784243e-04 1.308496e-03
## [16,] 5.936787e-05 1.279184e-04 2.506945e-04 5.370214e-04 9.715486e-04
## [17,] 5.324801e-05 1.215878e-04 1.693410e-04 3.775203e-04 6.077390e-04
## [18,] 3.315047e-05 9.984251e-05 1.140968e-04 2.057103e-04 3.328661e-04
## [19,] 1.962760e-05 5.715596e-05 5.722303e-05 7.865410e-05 1.384037e-04
## [20,] 1.534503e-05 2.677634e-05 3.162288e-05 2.579843e-05 6.099565e-05
## [21,] 1.115559e-05 2.637756e-05 1.940280e-05 2.040413e-05 2.797553e-05
## [22,] 1.476844e-06 9.532852e-06 8.171759e-06 9.031720e-06 1.423099e-05
## [23,] 3.814983e-08 5.966763e-07 5.963810e-07 7.018433e-07 5.968509e-06
## [24,] 1.352893e-07 2.526690e-09 2.523925e-09 7.691003e-09 2.813493e-07
## [25,] 5.044114e-08 1.783812e-11 5.375783e-13 1.373830e-11 7.132665e-10
##              [,11]        [,12]        [,13]        [,14]        [,15]
##  [1,] 5.360912e-05 2.658121e-05 1.747204e-05 1.043814e-05 1.398271e-06
##  [2,] 4.075866e-04 2.165888e-04 1.549621e-04 9.735751e-05 3.099050e-05
##  [3,] 1.228172e-03 7.047861e-04 4.857567e-04 3.486787e-04 2.119312e-04
##  [4,] 2.499886e-03 1.739232e-03 1.244130e-03 8.291024e-04 6.286239e-04
##  [5,] 4.149180e-03 3.015003e-03 2.170086e-03 1.559449e-03 1.074826e-03
##  [6,] 4.833667e-03 3.649504e-03 2.824962e-03 2.124527e-03 1.478509e-03
##  [7,] 4.934936e-03 4.600520e-03 3.556574e-03 2.715123e-03 2.009300e-03
##  [8,] 5.453879e-03 5.145862e-03 3.932539e-03 3.125221e-03 2.376956e-03
##  [9,] 5.362010e-03 5.157982e-03 4.435555e-03 3.675580e-03 2.789033e-03
## [10,] 4.483442e-03 4.650308e-03 4.293779e-03 3.477532e-03 2.791055e-03
## [11,] 3.972625e-03 4.276758e-03 3.760451e-03 3.142764e-03 2.618511e-03
## [12,] 3.683162e-03 4.083063e-03 3.566132e-03 2.763445e-03 2.309712e-03
## [13,] 2.908487e-03 3.216281e-03 3.125009e-03 2.700005e-03 2.035218e-03
## [14,] 2.213235e-03 2.298136e-03 2.289569e-03 2.128410e-03 1.552276e-03
## [15,] 1.734877e-03 1.745877e-03 1.604868e-03 1.520428e-03 1.220067e-03
## [16,] 1.262521e-03 1.447914e-03 1.283425e-03 1.268231e-03 9.604276e-04
## [17,] 7.579048e-04 7.327101e-04 7.212016e-04 5.669270e-04 4.018321e-04
## [18,] 3.819398e-04 3.513110e-04 4.064398e-04 3.482611e-04 2.387331e-04
## [19,] 1.940650e-04 1.370836e-04 2.328461e-04 2.420312e-04 1.344319e-04
## [20,] 7.624598e-05 7.498929e-05 1.252384e-04 1.288436e-04 6.024378e-05
## [21,] 2.730534e-05 2.116675e-05 3.358347e-05 4.903403e-05 2.098571e-05
## [22,] 9.668804e-06 1.152569e-05 9.664883e-06 1.078177e-05 1.956995e-06
## [23,] 5.535432e-06 1.060613e-06 7.332088e-07 6.233283e-07 3.325791e-08
## [24,] 2.794870e-07 4.067409e-08 1.697254e-06 1.696706e-06 3.148539e-08
## [25,] 7.614177e-10 1.414433e-07 7.633486e-06 7.633486e-06 1.414293e-07
##              [,16]        [,17]        [,18]        [,19]        [,20]
##  [1,] 2.988114e-06 2.853904e-06 8.295145e-08 2.047093e-07 2.783864e-08
##  [2,] 1.943503e-05 1.357860e-05 2.118250e-06 7.118998e-06 9.593690e-07
##  [3,] 1.084036e-04 5.928620e-05 2.529315e-05 2.206581e-05 2.683998e-06
##  [4,] 3.761740e-04 2.028324e-04 9.437877e-05 3.737542e-05 4.796939e-06
##  [5,] 7.660311e-04 4.749435e-04 2.577365e-04 1.278245e-04 4.898397e-05
##  [6,] 1.100683e-03 6.059633e-04 3.750680e-04 2.523715e-04 1.639409e-04
##  [7,] 1.442637e-03 8.599729e-04 4.815565e-04 3.114095e-04 1.968687e-04
##  [8,] 1.689935e-03 9.997953e-04 6.968163e-04 4.391894e-04 2.073453e-04
##  [9,] 1.836376e-03 1.046801e-03 7.025020e-04 4.764009e-04 2.487180e-04
## [10,] 1.946843e-03 1.087330e-03 6.939126e-04 4.730340e-04 2.421683e-04
## [11,] 1.972574e-03 1.093334e-03 7.416681e-04 4.185078e-04 2.371808e-04
## [12,] 1.751422e-03 9.658781e-04 5.340841e-04 3.851915e-04 2.764841e-04
## [13,] 1.542109e-03 7.985194e-04 5.040396e-04 3.693242e-04 2.414277e-04
## [14,] 1.040469e-03 5.657205e-04 4.642371e-04 2.981794e-04 1.554495e-04
## [15,] 8.380934e-04 3.851598e-04 3.185133e-04 1.683923e-04 8.308579e-05
## [16,] 5.928049e-04 3.064744e-04 2.083442e-04 8.870889e-05 7.327479e-05
## [17,] 2.486479e-04 1.174382e-04 6.688423e-05 6.442119e-05 4.483322e-05
## [18,] 1.376896e-04 4.880215e-05 3.814128e-05 5.174009e-05 2.401157e-05
## [19,] 5.354302e-05 1.911732e-05 1.050913e-05 1.701277e-05 9.443467e-06
## [20,] 1.681483e-05 7.231181e-06 1.333184e-05 2.230948e-06 3.137208e-07
## [21,] 2.013526e-05 1.806295e-05 4.824911e-06 4.255365e-07 5.770296e-09
## [22,] 8.685778e-06 8.641764e-06 4.039882e-07 5.756006e-09 6.271794e-10
## [23,] 5.905853e-07 5.904179e-07 1.245184e-08 1.114242e-11 4.441341e-12
## [24,] 2.488879e-09 2.478034e-09 4.644235e-11 1.739293e-14 1.576671e-15
## [25,] 4.907136e-11 5.235805e-13 9.704191e-15 3.355106e-18 2.762964e-20
##              [,21]        [,22]        [,23]        [,24]        [,25]
##  [1,] 2.299254e-10 1.173429e-09 1.597217e-10 4.027991e-13 1.882043e-17
##  [2,] 6.491147e-08 4.591166e-07 6.249296e-08 1.575998e-10 7.363711e-15
##  [3,] 1.211468e-06 8.848815e-06 1.204462e-06 3.037510e-09 1.419249e-13
##  [4,] 1.272597e-06 8.406827e-06 1.143549e-06 2.883864e-09 1.347462e-13
##  [5,] 7.675184e-06 8.849250e-07 5.512244e-08 1.441101e-10 9.518570e-15
##  [6,] 5.186253e-05 3.895875e-06 3.050766e-07 5.634177e-09 2.547584e-12
##  [7,] 8.451989e-05 1.922815e-05 9.203499e-06 2.364827e-07 9.412102e-10
##  [8,] 8.876922e-05 4.957252e-05 2.442155e-05 2.632415e-06 1.513751e-07
##  [9,] 1.196493e-04 1.116851e-04 3.986150e-05 1.629574e-05 1.596188e-06
## [10,] 1.280652e-04 9.307399e-05 5.626059e-05 2.919318e-05 1.195834e-06
## [11,] 1.354320e-04 6.194074e-05 3.683616e-05 9.551452e-06 1.541341e-07
## [12,] 1.424166e-04 8.076310e-05 3.219312e-05 2.682563e-06 6.340337e-07
## [13,] 1.217669e-04 8.575662e-05 2.942890e-05 9.489469e-06 9.870421e-06
## [14,] 8.529933e-05 5.204219e-05 5.702064e-05 2.324741e-05 1.009344e-05
## [15,] 4.338101e-05 4.108954e-05 3.158394e-05 9.980196e-06 1.288829e-05
## [16,] 3.166103e-05 1.133216e-05 3.462318e-06 6.840836e-07 3.411771e-06
## [17,] 2.174466e-05 7.441841e-07 4.169145e-08 6.989930e-09 4.586727e-08
## [18,] 8.396411e-06 1.347411e-07 8.428034e-11 4.531503e-12 3.040082e-11
## [19,] 1.303390e-06 7.654560e-08 9.257660e-09 2.334128e-11 2.083223e-15
## [20,] 5.927654e-07 4.105931e-06 5.588681e-07 1.409399e-09 6.585291e-14
## [21,] 1.662383e-06 1.221260e-05 1.662327e-06 4.192190e-09 1.958764e-13
## [22,] 2.435669e-07 1.789411e-06 2.435669e-07 6.142467e-10 2.870013e-14
## [23,] 1.757984e-09 1.291536e-08 1.757984e-09 4.433426e-12 2.071479e-16
## [24,] 6.250378e-13 4.591960e-12 6.250378e-13 1.576271e-15 7.364985e-20
## [25,] 1.094693e-17 8.042373e-17 1.094693e-17 2.760686e-20 1.289906e-24
#set up a plot using the KDE output
p <- plot_ly(x = kd$x, y = kd$y, z = kd$z, colorscale = "Viridis") %>% 
  add_surface()  %>% 
  layout(scene = list(xaxis = list(title = 'Age'),
                     yaxis = list(title = 'Overall'),
                     zaxis = list(title = 'Density')))

#use Shiny to wrap this in a div so it will center properly
div(p, align = "center")

Observations and Conclusions

Overall Scores

  • The overall scores …
  • The mean …

Agility Scores

  • The agility scores …
  • The mean of the Agility characteristic …

Additional Notes on Hexadecimal Colors

Color values can be represented as base-16 notation (00 (low) through FF (high)) for four ‘channels’: red-green-blue-alpha. Alpha represents opacity (00 is fully transparent and FF is fully opaque).

A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. This represents the least (0) to the most (255) intensity of each of the color components. Thus web colors specify colors in the True Color (24-bit RGB) color scheme. The hex triplet is formed by concatenating three bytes in hexadecimal notation, in the following order Byte 1: red value (color type red), Byte 2: green value (color type green), Byte 3: blue value (color type blue).

Source: https://en.wikipedia.org/wiki/Web_colors

#dummy data
df<-data.frame(seq(0,5,1),seq(0,5,1))
ggplot() +
  theme_void() +
  theme(legend.position = "none") +
  geom_hline(yintercept = 0.5, size=10, linetype="solid", col="#000000", alpha=1)+
  geom_hline(yintercept = 1.5, size=10, linetype="solid", col="#000000", alpha=1)+
  geom_hline(yintercept = 2.5, size=10, linetype="solid", col="#000000", alpha=1)+
  geom_hline(yintercept = 3.5, size=10, linetype="solid", col="#000000", alpha=1)+
  geom_rect(data=df, aes(xmin=0, xmax=1, ymin=0, ymax=1), fill="#FF000011", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=1, xmax=2, ymin=0, ymax=1), fill="#FF000022", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=2, xmax=3, ymin=0, ymax=1), fill="#FF000033", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=3, xmax=4, ymin=0, ymax=1), fill="#FF000044", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=4, xmax=5, ymin=0, ymax=1), fill="#FF0000FF", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=0, xmax=1, ymin=1, ymax=2), fill="#00FF0011", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=1, xmax=2, ymin=1, ymax=2), fill="#00FF0022", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=2, xmax=3, ymin=1, ymax=2), fill="#00FF0033", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=3, xmax=4, ymin=1, ymax=2), fill="#00FF0044", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=4, xmax=5, ymin=1, ymax=2), fill="#00FF00FF", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=0, xmax=1, ymin=2, ymax=3), fill="#0000FF11", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=1, xmax=2, ymin=2, ymax=3), fill="#0000FF22", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=2, xmax=3, ymin=2, ymax=3), fill="#0000FF33", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=3, xmax=4, ymin=2, ymax=3), fill="#0000FF44", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=4, xmax=5, ymin=2, ymax=3), fill="#0000FFFF", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=0, xmax=1, ymin=3, ymax=4), fill="#00000011", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=1, xmax=2, ymin=3, ymax=4), fill="#00000022", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=2, xmax=3, ymin=3, ymax=4), fill="#00000033", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=3, xmax=4, ymin=3, ymax=4), fill="#00000044", inherit.aes = FALSE)+
  geom_rect(data=df, aes(xmin=4, xmax=5, ymin=3, ymax=4), fill="#000000FF", inherit.aes = FALSE)