if (!file.exists("./data/valiaikadata.RDS")){
d_splits <- data_frame()
for (leg in 1:10){
splits <- unlist(looptbl$splits[leg])
split_dat <- data_frame()
for (split1 in splits){
read_html(paste0("http://online.10mila.se/index2.php?classId=1&legNo=",leg,"&splitNo=",split1)) %>%
html_table(fill = TRUE) %>%
.[[5]] %>%
.[-1:-2,] %>%
as_tibble() -> tmp
names(tmp) <- c("place","name","club","time","diff")
tmp$leg <- leg
tmp$split1 <- split1
split_dat <- bind_rows(split_dat,tmp)
}
d_splits <- bind_rows(d_splits,split_dat)
}
d_splits %>%
mutate(
min = as.integer(sub(":.+$", "", time)),
s = as.integer(sub("^.+:", "", time)),
secs = min * 60 + s,
diff_min = as.integer(sub(":.+$", "", diff)),
diff_s = as.integer(sub("^.+:", "", diff)),
diff_secs = diff_min * 60 + diff_s
) -> d_splits2
saveRDS(d_splits2, "./data/valiaikadata.RDS")
}
d_splits2 <- readRDS("./data/valiaikadata.RDS")
split_info <- d_splits2 %>% count(leg,split1) %>% select(-n)
split_info %>%
mutate(length = case_when(
leg == 1 & split1 == 7 ~ 2.5,
leg == 1 & split1 == 8 ~ 5.1,
leg == 1 & split1 == 10 ~ 8.3,
leg == 1 & split1 == 11 ~ 9.9,
leg == 1 & split1 == 12 ~ 11.1,
leg == 2 & split1 == 15 ~ 2.8,
leg == 2 & split1 == 17 ~ 5.4,
leg == 2 & split1 == 18 ~ 7.8,
leg == 2 & split1 == 19 ~ 9.5,
leg == 2 & split1 == 20 ~ 10.1,
leg == 3 & split1 == 24 ~ 2.8,
leg == 3 & split1 == 25 ~ 4.2,
leg == 3 & split1 == 26 ~ 5.9,
leg == 3 & split1 == 27 ~ 8.5,
leg == 4 & split1 == 31 ~ 2.4,
leg == 4 & split1 == 32 ~ 4.4,
leg == 4 & split1 == 34 ~ 9.5,
leg == 4 & split1 == 35 ~ 13,
leg == 4 & split1 == 36 ~ 14.3,
leg == 5 & split1 == 39 ~ 3.3,
leg == 5 & split1 == 40 ~ 5.8,
leg == 5 & split1 == 42 ~ 6.5,
leg == 6 & split1 == 45 ~ 3.3,
leg == 6 & split1 == 46 ~ 5.9,
leg == 6 & split1 == 47 ~ 6.6,
leg == 7 & split1 == 50 ~ 2.7,
leg == 7 & split1 == 51 ~ 6.2,
leg == 7 & split1 == 52 ~ 7.9,
leg == 7 & split1 == 53 ~ 8.8,
leg == 8 & split1 == 56 ~ 2.8,
leg == 8 & split1 == 57 ~ 5.8,
leg == 8 & split1 == 58 ~ 8.9,
leg == 8 & split1 == 59 ~ 9.9,
leg == 9 & split1 == 62 ~ 3.1,
leg == 9 & split1 == 63 ~ 5.2,
leg == 9 & split1 == 64 ~ 7.4,
leg == 9 & split1 == 65 ~ 8,
leg == 10 & split1 == 68 ~ 2.9,
leg == 10 & split1 == 70 ~ 5.8,
leg == 10 & split1 == 71 ~ 8.9,
leg == 10 & split1 == 72 ~ 11.5,
leg == 10 & split1 == 73 ~ 12.9
)) %>%
group_by(leg) %>%
mutate(length_cum = cumsum(length)) -> split_info
d_splits_10 <- left_join(d_splits2,split_info)
d_leg0 <- all_legs %>% filter(leg == 1) %>% mutate(length = 0,
diff_secs = 0,
leg = 1)
d_leg_11 <- bind_rows(all_legs,d_leg_10)
d_leg_11_0 <- d_leg_11 %>% mutate(length = 0, leg = leg + 1)
legdata <- bind_rows(d_leg_11,d_leg_11_0)
d_splits_10 <- bind_rows(d_splits_10,legdata)
d_splits_10 <- d_splits_10 %>% filter(leg != 0, leg != 11)
d_splits_10$leg2 <- paste0("Leg ", d_splits_10$leg)
d_splits_10$leg2 <- factor(d_splits_10$leg2, levels = c("Leg 1","Leg 2","Leg 3","Leg 4","Leg 5","Leg 6","Leg 7","Leg 8","Leg 9","Leg 10"))
all_splits <- d_splits_10 # exceliin
d_splits_10 <- d_splits_10 %>% filter(club %in% top10)
library(ggplot2)
library(hrbrthemes)
library(viridis)
ggplot(d_splits_10, aes(x = length, y = diff_secs/60, color = club, fill = club)) +
geom_line(alpha = .8) + geom_point(shape = 21, color = "white", alpha = .8, size = 4) +
ggrepel::geom_text_repel(data = d_splits_10 %>%
group_by(leg2) %>%
filter(length == max(length)),
aes(label = paste0(name,"\n",club)), nudge_y = -.2, nudge_x = 1, size = 2.0, lineheight = .8, family = "Roboto Condensed") +
facet_wrap(~leg2, ncol = 1, scales = "free") +
scale_x_continuous(breaks = 0:18, labels = 0:18, limits = c(0,18)) +
scale_y_reverse() +
geom_text(aes(label = place), color = "white", family = "Roboto Condensed", size = 2.5, fontface= "bold") +
theme_ipsum_rc() +
theme(legend.position = "none") +
scale_fill_manual(values = c(rev(ipsum_palette),"black")) + scale_color_manual(values = c(rev(ipsum_palette),"black")) +
# theme(plot.margin = unit(0,0,0,0)) +
# theme(margin(t = 0, r = 0, b = 0, l = 0, unit = "pt")) +
labs(title = "Top ten teams in 10mila 2018 per leg",
subtitle = "Time difference to lead in each split",
caption = paste0("Data: online.10mila.se\n",Sys.time()),
x = "distance from exchange (km)", y = "minutes behind the lead") -> splitspic
ggsave(filename = "./kuvat/10mila2018_splits_twitter.png", splitspic, width = 6, height = 30, dpi = 120)
ggsave(filename = "./kuvat/10mila2018_splits_blogi.png", splitspic, width = 6, height = 30, dpi = 240)
# **************************************************************************************
# Omat juoksu suhteessa kärkeen
top10 <- d_leg2 %>% filter(leg == 10) %>% slice(1:10) %>% pull(club)
ourteam <- d_leg2 %>% filter(leg == 1, grepl("Veteli", club)) %>% pull(club)
top10 <- c(top10,ourteam)
d_splits_10 <- all_splits %>% filter(club %in% top10, leg == 2)
library(ggplot2)
library(hrbrthemes)
library(viridis)
ggplot(d_splits_10, aes(x = length, y = diff_secs/60, color = club, fill = club)) +
geom_line(alpha = .8) + geom_point(shape = 21, color = "white", alpha = .8, size = 6) +
ggrepel::geom_text_repel(data = d_splits_10 %>%
group_by(leg2) %>%
filter(length == max(length)),
aes(label = paste0(name,"\n",club)), nudge_y = -.2, nudge_x = 1, size = 2.0, lineheight = .8, family = "Roboto Condensed") +
facet_wrap(~leg2, ncol = 1, scales = "free") +
scale_x_continuous(breaks = 0:18, labels = 0:18, limits = c(0,18)) +
scale_y_reverse() +
geom_text(aes(label = place), color = "white", family = "Roboto Condensed", size = 2.5, fontface= "bold") +
theme_ipsum_rc() +
theme(legend.position = "none") +
scale_fill_manual(values = c(rev(ipsum_palette),"orange","black")) + scale_color_manual(values = c(rev(ipsum_palette),"orange","black")) +
# theme(plot.margin = unit(0,0,0,0)) +
# theme(margin(t = 0, r = 0, b = 0, l = 0, unit = "pt")) +
labs(title = "My own run on leg 2 in 10mila 2018",
subtitle = "Time difference to lead in each split, position in relay inside the dot",
caption = paste0("Data: online.10mila.se\n",Sys.time()),
x = "distance from exchange (km)", y = "minutes behind the lead")