Skip to content

Instantly share code, notes, and snippets.

@gabrielzanlorenssi
Created November 26, 2022 22:10
Show Gist options
  • Save gabrielzanlorenssi/c7040e0e9517287c836e447d5375b947 to your computer and use it in GitHub Desktop.
Save gabrielzanlorenssi/c7040e0e9517287c836e447d5375b947 to your computer and use it in GitHub Desktop.
# Resultados copa ---------------------------------------------------------
library(tidyverse)
library(readxl)
library(googlesheets4)
url = "https://docs.google.com/spreadsheets/d/1m9fPRyk5fNVP7CXuVw2MqF3ZGAMbFFhceTsEoYSCUy4/edit#gid=0"
sheets <- read_sheet(url)
# Visualizacao ------------------------------------------------------------
v <- crossing(a1=0:5,
a2=0:5,
b1=0:5,
b2=0:5) %>%
rowid_to_column(var="id")
sheets %>%
filter(grupo=="C") -> s
final <- map_df(v$id, function(x) {
s[5,"res1"] <- v$a1[x]; s[5,"res2"] <- v$a2[x]
s[6,"res1"] <- v$b1[x]; s[6, "res2"] <- v$b2[x]
s %>%
drop_na() %>%
mutate(p1 = case_when(res1==res2~1,
res1>res2~3,
res1<res2~0),
p2 = case_when(res1==res2~1,
res1>res2~0,
res1<res2~3),
s1 = res1-res2,
s2 = res2-res1) %>%
select(time1,time2,
res1,res2,
s1,s2,p1,p2) %>%
rename(r1=res1, r2=res2,
t1=time1, t2=time2) %>%
pivot_longer(everything(),
names_to=c(".value", "set"),
names_pattern=c("(.)(.)")) %>%
group_by(t) %>%
summarise(n=n(), p=sum(p), s=sum(s),
gp=sum(r)) -> f
f %>%
arrange(-p, -s, -gp) %>%
rowid_to_column(var="pos") %>%
#filter(pos%in%c(1,2)) %>%
select(pos, t) %>%
mutate(id=x) -> g
return(g)
})
#--
final %>%
left_join(v, by="id") %>%
filter(t=="Argentina") %>%
mutate(r1 = paste(a1, a2),
r2 = paste(b1, b2)) %>%
mutate(p1 = as.character(case_when(a1==a2~1,
a1>a2~3,
a1<a2~0)),
p2 = as.character(case_when(b1==b2~1,
b1>b2~0,
b1<b2~3)),
o1 = a1-a2,
o2 = b1-b2) %>%
rename(`Arabia x Mexico` = r1,
`Polonia x Argentina` = r2) %>%
pivot_longer(cols=c('a1', 'a2', 'b1','b2'),
names_to=c(".value", "set"),
names_pattern=c("(.)(.)")) %>%
pivot_longer(cols=c("a", "b")) %>%
select(-t, -id) %>%
ggplot(aes(x=reorder(`Arabia x Mexico`, o1),
y=reorder(`Polonia x Argentina`, o2),
fill=as.character(pos))) +
geom_tile(width=.9, height=.9) +
facet_grid(rows=vars(p2), cols=vars(p1),
scales='free', space='free') +
scale_fill_manual(values=c('blue', 'lightblue',
'red', 'red'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment