READ THIS DISCLAIMER THIS QUARTO FILE OFFERS A BASIC OUTLINE FOR THE REPORT AND EXAMPLE FOR SOME OF THE CODE.
THIS FILE IS NOT COMPLETE AND NOT INTENDED FOR YOU TO SIMPLY REPLICATE.
YOU WILL NEED TO WORK THROUGH ALL REQUIREMENTS ON YOUR OWN, INCLUDING INTERPRETATIONS, AESTHETICS, LABELS, COLORS, TITLES, SUBTITLES, ETC.
BE CREATIVE!
PART 0: Useful code ONLY for those assigned the customers{datasetsICR}
Skip (DELETE) this if you’re using the gss_sm dataset
Note, if you are using the customers dataset, then you will need to transform the Channel and Region from integer into factor variables, using text for the levels. The data dictionary for the dataset provides the factor values. Here is some code for you to accomplish this task :-).
# Collapse positionsFIFA <- FIFA %>%mutate(Position =as.factor(Position) %>%recode_factor("GK"="Goalkeeper","RB"="Defender","LB"="Defender","CB"="Defender","RWB"="Defender","LWB"="Defender","LCB"="Defender","RCB"="Defender","CDM"="Midfielder","CM"="Midfielder","CAM"="Midfielder","RM"="Midfielder","LM"="Midfielder","RAM"="Midfielder","LCM"="Midfielder","LAM"="Midfielder","LDM"="Midfielder","RCM"="Midfielder","RDM"="Midfielder","RW"="Forward","LW"="Forward","CF"="Forward","RF"="Forward","LF"="Forward","LS"="Forward","RS"="Forward","ST"="Forward" ))head(FIFA$Position)
# A tibble: 27 × 5
# Groups: Position [4]
Position Work.Rate N freq pct
<fct> <fct> <int> <dbl> <dbl>
1 Goalkeeper Medium/ Medium 2025 1 100
2 Defender High/ High 254 0.0433 4
3 Defender High/ Low 41 0.00699 1
4 Defender High/ Medium 1045 0.178 18
5 Defender Low/ High 341 0.0581 6
6 Defender Low/ Low 3 0.000511 0
7 Defender Low/ Medium 362 0.0617 6
8 Defender Medium/ High 868 0.148 15
9 Defender Medium/ Low 21 0.00358 0
10 Defender Medium/ Medium 2931 0.500 50
# ℹ 17 more rows
Interpretation
The data does make sense. I subset all players into four general categories, midfielder, forward, goal keeper, and defender.
PART 2: CREATE STACKED AND DODGED BAR CHARTS FROM 2 CATEGORICAL VARIABLES
Position <- pip1 %>%filter(Position !="")p_title <-"Position by Work Rate"p_caption <-"ICRdatasets, FIFA"# AS STACKED BAR CHARTp <-ggplot(data =subset(pip1, !is.na(Position) &!is.na(Work.Rate)), aes(x=Position, y=pct, fill = Work.Rate))p +geom_col(position ="stack") +labs(x="Position", y="Percent", fill ="Work Rate",title = p_title, caption = p_caption, subtitle ="As a stacked bar chart") +geom_text(aes(label=pct), position =position_stack(vjust=.5))
# AS DODGED BAR CHARTp +geom_col(position ="dodge2") +labs(x="Position", y="Percent", fill ="Work Rate",title = p_title, caption = p_caption, subtitle ="As a dodged bar chart") +geom_text(aes(label = pct), position =position_dodge(width = .9))
# AS FACETED HORIZONTAL BAR CHARTp +geom_col(position ="dodge2") +labs(x=NULL, y="Percent", fill ="Work.Rate",title = p_title, caption = p_caption, subtitle ="As a faceted horizontal bar chart") +guides(fill ="none") +coord_flip() +facet_grid(~ Position) +geom_text(aes(label = pct), position =position_dodge2(width =1))
PART 3: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CONTINUOUS & ONE CATEGORICAL VARIABLE
The older average players have a lower potential value. This does make sense because as players get older the less likely they are to drastically improve in skill.
PART 4: SCATTERPLOT WITH A THIRD CATEGORICAL VARIABLE
This code does not remove NA – make sure you deal with that.
::: {.cell}
pip3 <- pip2[complete.cases(pip2$age_mean, pip2$potential_mean),]p <-ggplot(pip3, aes(x = age_mean, y = potential_mean, color = Position))p +geom_point(size=8) +annotate(geom ="text", x =2.0, y=60, label ="The positions midfielder and forward are overlapping each other.", hjust=0) +labs(y="Average Potential Score", x="Average Age.", title="Age and Potential of Player by Position", subtitle ="Goalkeeper has the lowest Potential Score", caption <-"FIFA, ICRdatasets")
:::
PART 5: LEGEND AND GUIDES
This is just an example – do not simply duplicate these options.
::: {.cell}
pip3 <- pip2[complete.cases(pip2$age_mean, pip2$potential_mean),]p <-ggplot(pip3, aes(x = age_mean, y = potential_mean, color = Position))p +geom_point(size=8) +annotate(geom ="text", x =24, y=70, label ="The positions midfielder and forward are closest to each other.", hjust=0) +labs(y="Average Potential Score", x="Average Age.", title="Age and Potential of Player by Position", subtitle ="Goalkeeper has the lowest Potential Score", caption <-"FIFA, ICRdatasets") +theme(legend.title =element_text(color="black", size=15, face="italic"),legend.position ="top")
:::
PART 6: DATA LABELS VS LEGEND
p <-ggplot(pip3, aes(x=age_mean, y=potential_mean, color=Position))p +geom_point(size=7) +geom_text(mapping =aes(label=Position), hjust=1.3, size=3.5) +annotate(geom ="text", x =24, y=70, label ="Forward has the lowest average age and highest potential score.", hjust=0) +labs(y="Average Potential Score", x="Average Age", title="Age and Potential of Player by Position", color ="Position") +theme(legend.position ="none")
PART 7: INTERPRETATION
Forwards and midfielders are the youngest average players, which correlates with these players having a higher potential score. Defenders are about 25.5 years old with a potential score of 71.0. Lastly, Goalkeepers have the highest average age of 26, and the lowest potential score just under 70.
Summarize those insights as the subtitle.
p <-ggplot(pip3, aes(x=age_mean, y=potential_mean, color=Position))p +geom_point(size=7) +geom_text(mapping =aes(label=Position), hjust=1.3, size=3.5) +annotate(geom ="text", x =24, y=70, label ="Forward has the lowest average age and highest potential score.", hjust=0) +labs(y="Average Potential Score", x="Average Age", title="Age and Potential of Player by Position",subtitle ="Forwards and midfielders are younger with higher potential scores than goalkeepers and defenders",color ="Position") +theme(legend.position ="none")