A visual representation of the difference between mph and kmh

Needed packages

The problem:

I have recently moved to Canada and have been having a lot of fun settling and exploring Toronto. However, one thing I’ve not been fond of is the transition to metric, especially on the road and with temperatures. Relearning how fast the speed limit is intuitively will take time, and it’s pretty annoying having to look down at my speedometer to tell me how fast I’m going instead of ‘feeling’ how fast I’m going (I know, first world problem and also dangerous). So, to help myself out, I’m going to create a visual representation of miles per hour with respect to kilometers per hour

Create the data

# create a df of mph from 1 to 75 mph
mph <- 1:75
df <- data.frame(mph = mph) %>% as_tibble()

# add a column with conversion to kmh (1mile = 1.60934 km)
df <- df %>% mutate(kph = round(mph*1.60934, digits = 0))

df
## # A tibble: 75 x 2
##      mph   kph
##    <int> <dbl>
##  1     1     2
##  2     2     3
##  3     3     5
##  4     4     6
##  5     5     8
##  6     6    10
##  7     7    11
##  8     8    13
##  9     9    14
## 10    10    16
## # ... with 65 more rows

visualize it!

p <- ggplot(df, aes(mph, mph)) +
  geom_line(color = "dodgerblue") + 
  geom_line(aes(y = kph), color = "red") + 
  labs(x = "miles per hour", y = "mph/kph") +
  theme_stata()

p

# add a second axis for kph

p_axes <- p +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ .*1.60934, 
                                         name = "kph", 
                                         breaks = seq(0, 200, by = 20)))

p_axes

# interactive plot: Try hovering over the lines to see what the coversion between kph and mph are.

ggplotly(p)
# Essentially, for every kph, subtract a bit more than a third and that's mph: 100 -> 62; 60 -> high 30's

A cleaner representation

  • The issue with the above is that there is no legend and the hover tool has too many values. This can be fixed with a long pivot table.
df_long <- df %>% mutate(miles_per_hour = mph) %>%
              pivot_longer(c(mph, kph), names_to = "type", values_to = "unit")
df_long
## # A tibble: 150 x 3
##    miles_per_hour type   unit
##             <int> <chr> <dbl>
##  1              1 mph       1
##  2              1 kph       2
##  3              2 mph       2
##  4              2 kph       3
##  5              3 mph       3
##  6              3 kph       5
##  7              4 mph       4
##  8              4 kph       6
##  9              5 mph       5
## 10              5 kph       8
## # ... with 140 more rows

Now we can redo the plot

p_clean <- ggplot(df_long, aes(miles_per_hour, unit, color = type)) + geom_line()

p_clean

That looks a lot better. The axes make more sense, and there is a legend explaining the line colors.

The clean final interactive plot

ggplotly(p_clean)