Great Britain Railway Network

[This article was first published on R - datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The {blimey} logo.

Introducing the nascent R package {blimey} (repository). At this stage it contains only the following data:

  • railways — latitude and longitude segments along railway lines (wide format);
  • railways_pivot — latitude and longitude segments along railway lines (long format); and
  • railway_stations — codes, names and locations of railway stations.

Load a couple of core packages.

library(dplyr)
library(ggmap)

Load the {blimey} package.

library(blimey)

Let’s take a look at the railways data.

head(railways)

# A tibble: 6 × 7
    fid elr    trid lat_start lat_end lon_end lon_start
  <dbl> <chr> <dbl>     <dbl>   <dbl>   <dbl>     <dbl>
1     0 MLN3   1100      50.3    50.3   -5.06     -4.83
2     1 SDS    3100      50.3    50.3   -4.83     -4.83
3     2 MLN3   1600      50.3    50.3   -5.07     -5.07
4     3 MLN4   3700      50.2    50.2   -5.45     -5.45
5     4 MLN4   3601      50.1    50.1   -5.53     -5.53
6     5 MLN4   1100      50.2    50.1   -5.50     -5.45

We’re going to plot those data out on a map. Define the bounding box for the map.

bb <- c(left = -8, bottom = 49.80, right = 3, top = 59.50)

Use {ggmap} to load Stamen Map tiles for the toner lite map.

map_toner <- get_stamenmap(bb, zoom = 8, maptype = "toner-lite")

Define some colours for plotting the lines and stations.

COLOUR_MUTED_BLUE <- "#1f77b4"
COLOUR_SAFETY_ORANGE <- "#ff7f0e"

Create a function which will tweak the map appearance.

theme_map <- function(plot) {
  plot + 
  coord_map() +
  scale_x_continuous(expand = expansion(0, 0)) +
  scale_y_continuous(expand = expansion(0, 0)) +
  theme(
    legend.position = "none",
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    text = element_text(size = 12)
  )
}

Show the railway line segments over the toner lite map.

map <- ggmap(map_toner, extent = "normal") +
  geom_segment(
    data = railways,
    aes(
      x = lon_start,
      y = lat_start,
      xend = lon_end,
      yend = lat_end,
    ),
    size = 1, alpha = 0.5, col = COLOUR_MUTED_BLUE
  )

(map <- theme_map(map))

Now add in the railway stations.

(ggmap(map_toner, extent = "normal") +
  geom_segment(
    data = railways,
    aes(
      x = lon_start,
      y = lat_start,
      xend = lon_end,
      yend = lat_end
    ),
    size = 2, alpha = 0.5, col = COLOUR_MUTED_BLUE
  ) +
  geom_point(
    data = railway_stations,
    aes(
      x = lon,
      y = lat
    ),
    size = 2, alpha = 0.5, col = COLOUR_SAFETY_ORANGE
  )) %>%
  theme_map()

The maps are fairly large. Open them in a separate tab so that you can zoom in on the details.

To leave a comment for the author, please follow the link and comment on their blog: R - datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)