-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFT_football ggplot.R
151 lines (135 loc) · 6.06 KB
/
FT_football ggplot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# https://raw.githubusercontent.com/johnburnmurdoch/johnburnmurdoch.github.io/master/slides/r-ggplot/r-scripts.R?
#set directory for packages
#setwd("//svrau100qsm00.oceania.corp.anz.com/mckinns2$/My Documents/Current Work/R")
library(ggplot2)
library(dplyr)
library(tidyr)
library(magrittr)
library(RColorBrewer)
library(scales)
#pull data
allSeasons <- read.csv('https://raw.githubusercontent.com/johnburnmurdoch/johnburnmurdoch.github.io/master/slides/r-ggplot/changing-tides-of-football.csv', stringsAsFactors = F)
oneSeason <- allSeasons %>% filter(year==2016)
#1 - dot chart in a line
ggplot(oneSeason
,aes(x=1,score,col=country)) +
geom_point(size=3) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="right", legend.direction="vertical") +
xlab('') +
ggtitle("dot chart in a line") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#2 - dot chart spread in across rank
ggplot(oneSeason
,aes(rank,score,col=country)) +
geom_point(size=3) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="top") +
ggtitle("dot chart spread in across rank") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#3 - dot chart linked by line, spread in across rank
ggplot(oneSeason
,aes(rank,score,col=country)) +
geom_path() +
geom_point(size=3) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="top")+
ggtitle("dot chart linked by line, spread in across rank ") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#4 ENG vs. ESP strenght gap
ggplot(allSeasons %>%
filter(year==2016 & country %in% c('ENG','ESP')) %>%
dplyr::select(score, rank, country) %>%
spread(., country, score) %>%
rowwise() %>%
mutate(
gap = ESP-ENG,
max = max(ESP,ENG),
min = min(ESP,ENG)
)
,aes(rank,min,fill=gap>0)) +
geom_rect(aes(xmin = rank-0.5, xmax=rank+0.5, ymin=min, ymax=max), alpha=0.5) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="none") +
ggtitle("strenght across the league gap chart") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#5 season strength across rank, smooth line
ggplot(oneSeason %>%
filter(country == 'ENG'),aes(rank,score,col=country)) +
geom_ribbon(aes(ymin = atw, ymax = atb, fill=country),col='transparent', alpha=0.5) +
#add a stair case plot
geom_path(alpha=1, size=1.5) +
geom_point(size=3) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="none") +
ggtitle("strenght band across the league") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#6 season strength across rank, box and dots
ggplot(oneSeason %>%
filter(country == 'ENG'),aes(rank,score,col=country, fill=country)) +
geom_rect(aes(xmin=rank-0.5, xmax=rank+0.5, ymin = atw, ymax = atb),col='transparent', alpha=0.5) +
geom_point(size=5) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="none")+
ggtitle("season strength across rank, box and dots") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#7 season strength across rank, box and lines, gaps between boxes
ggplot(oneSeason %>%
filter(country == 'ENG'),aes(rank,score,col=country, fill=country)) +
geom_rect(aes(xmin=rank-0.45, xmax=rank+0.45, ymin = atw, ymax = atb),col='transparent', alpha=0.5) +
geom_point(size=5) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
theme_bw() +
theme(legend.position="none") +
ggtitle("season strength across rank, gaps for boxes") +
theme(plot.title = element_text(lineheight=.6, face="italic"))
#8 all leagues, facetted across year, 5X5 grid
ggplot(allSeasons,aes(rank,score,col=country)) +
geom_path() +
geom_point(size=1) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
facet_wrap(~year, ncol=4) +
theme_bw() +
theme(legend.position="top") +
ggtitle("Facetted graph grid") +
theme(plot.title=element_text(size=12, color="red", face="italic"))
#9 strenght gap between ENG v ESP grid by season
ggplot(allSeasons %>%
filter(country %in% c('ENG','ESP')) %>%
dplyr::select(score, year, rank, country) %>%
spread(., country, score) %>%
rowwise() %>%
mutate(
gap = ESP-ENG,
max = max(ESP,ENG),
min = min(ESP,ENG)
)
,aes(rank,min,fill=gap>0)) +
geom_rect(aes(xmin = rank-0.5, xmax=rank+0.5, ymin=min, ymax=max), alpha=0.5) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
facet_wrap(~year, ncol=4) +
theme_bw() +
theme(legend.position="none") +
ggtitle("Facetted strenght gap grid") +
theme(plot.title=element_text(size=12, color="light green", face="italic"))
#10 Facetted strenght bar w gap grid
ggplot(allSeasons %>% filter(country == 'ENG'),aes(rank,score,col=country, fill=country)) +
geom_rect(aes(xmin=rank-0.45, xmax=rank+0.45, ymin = atw, ymax = atb),col='transparent', alpha=0.5) +
geom_path(alpha=1) +
geom_point(size=1) +
scale_color_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
scale_fill_manual(breaks=unique(allSeasons$country),values=brewer_pal('qual',2)(7)) +
facet_wrap(~year, ncol=4) +
theme_bw() +
theme(legend.position="none") +
ggtitle("Facetted strenght bar w gap grid") +
theme(plot.title=element_text(size=12, color="light blue", face="italic"))