---
title: "Gearing Ratio"
author: "Martin Ueding"
date: "11 August 2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(dplyr)
library(ggplot2)
```

```{r}
front <- c(26, 36, 48)
back <- c(11, 13, 15, 17, 19, 21, 23, 26, 30, 34)
```

```{r}
df <- expand.grid(front = front, back = back)
df$ratio <- df$back / df$front
```

```{r}
ggplot(df, aes(x = back, y = ratio, color = factor(front))) +
  geom_point() +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano XT FC-T8000 (front) and Shimano CS-HG500 11-34 (back)',
       x = 'Teeth back',
       y = 'Ratio back/front',
       color = 'Teeth front')
```

```{r}
ggsave('Radon_Solution_Comfort_90-Gearing_Ratio.png')
```

```{r}
front <- list(
  'Ultegra 53-39T' = c(53, 39),
  'Ultegra 52-36T' = c(52, 36),
  'Ultegra 50-34T' = c(50, 34),
  'Ultegra 46-36T' = c(46, 36),
  'Deore 26-48T' = c(26, 36, 48)
)

back <- list(
  'Ultegra 11-25T' = c(11, 12, 13, 14, 15, 16, 17, 19, 21, 23, 25),
  'Ultegra 12-25T' = c(12, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25),
  'Ultegra 11-28T' = c(11, 12, 13, 14, 15, 17, 19, 21, 23, 25, 28),
  'Ultegra 11-30T' = c(11, 12, 13, 14, 15, 17, 19, 21, 24, 27, 30),
  'Ultegra 11-32T' = c(11, 12, 13, 14, 16, 18, 20, 22, 25, 28, 32),
  'Ultegra 11-34T' = c(11, 13, 15, 17, 19, 21, 23, 25, 27, 30, 34)
)
```

```{r}
df2 <- expand.grid(front_name = names(front), back_name = names(back))
df2$front = front[df2$front_name]
df2 <- tidyr::unnest(df2)
df2$back = back[df2$back_name]
```

```{r}
df3 <- tidyr::unnest(df2)
```

```{r}
df3$ratio = df3$back / df3$front
```

```{r}
ggplot(df3, aes(x = back, y = ratio, color = factor(front))) +
  geom_point() +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio back/front',
       color = 'Teeth front')
ggsave('gearing_all.png')
```


```{r}
ggplot(df3, aes(x = back, y = ratio, color = factor(front))) +
  geom_point() +
  facet_grid(front_name ~ back_name) +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio back/front',
       color = 'Teeth front')
ggsave('gearing_grid.png', width = 10, height = 7)
```

```{r}
ggplot(df3, aes(x = back, y = 1/ratio, color = factor(front))) +
  geom_point() +
  scale_x_reverse() +
  coord_cartesian(xlim = c(20, max(df3$back)),
                  ylim = c(0.6, 2)) +
  facet_grid(front_name ~ back_name) +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio front/back',
       color = 'Teeth front')
ggsave('gearing_grid_inv.png', width = 10, height = 7)
```


```{r}
ggplot(df3, aes(x = back, y = 1/ratio, color = factor(front))) +
  geom_point() +
  scale_x_reverse() +
  scale_y_log10() +
  facet_grid(front_name ~ back_name) +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio front/back',
       color = 'Teeth front')
ggsave('gearing_grid_inv_log.png', width = 10, height = 7)
```

```{r}
ggplot(df3,
       aes(x = back,
           y = ratio,
           color = interaction(front_name, back_name, sep = ' + ', lex.order = TRUE))) +
  geom_point(position = position_dodge(width = 0.4)) +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio back/front',
       color = 'Combination')
ggsave('gearing_interaction.png', width = 10, height = 7)
```


```{r}
ggplot(df3,
       aes(x = back,
           y = ratio,
           color = front_name)) +
  geom_point(position = position_dodge(width = 0.4)) +
  facet_wrap(~ back_name) +
  labs(title = 'Gearing Ratio',
       subtitle = 'Shimano Ultegra',
       x = 'Teeth back',
       y = 'Ratio back/front',
       color = 'Crank')
ggsave('gearing_wrap_crank.png', width = 10, height = 7)
```