ETC5521 Tutorial 6

Exploring bivariate dependencies

Author

Prof. Di Cook

🎯 Objectives

These are exercises in making scatterplots and variations to examine association between two variables, to explore association matrices and networks.

🔧 Preparation

The reading for this week is Wilke (2019) Ch 12 Visualizing associations. - Complete the weekly quiz, before the deadline! - Install the following R-packages if you do not have them already:

install.packages(c("tidygraph", "ggraph", "plotly", "colorspace"))
  • Open your RStudio Project for this unit, (the one you created in week 1, ETC5521). Create a .qmd document for this weeks activities.

📥 Exercises

Exploring associations and networks

Download the bike.rda file from the geomnet software site. This network is a summary of the bike trips taken by customers of the bike sharing company Capital Bikeshare () during the second quarter of 2015. Only trips between stations in the vicinity of Rockville, MD, are included. The data is organized as a list of two datasets, vertices (stations) and edges (trips between stations), as follows:

A list of two data frames:

  • trips: the trips data set consists of four variables of length 53:

    • Start.station: Station where bike trip starts
    • End.station: Station where bike trip ends
    • n: Number of trips between the two stations
    • minlength: Duration of shortest trip between the two stations (in seconds). Only those stations are included, if the shortest trip between them lasted not more than 15 minutes.
  • stations: the vertices data set consists of five variables with information on 21 stations:

    • id: Station ID number
    • name: Station name
    • lat: Latitude of station location
    • long: Longitude of station location
    • nbDocks: Number of bike docks at the station

Imagine you are the bike company, and you are interested in learning how to manage keeping bikes in places where people will use them.

a. Make some summaries of the bike station data.

b. Make some summary of the trips data.

c. Make an interactive heatmap

This is to understand the number of trips from one station to another. This will be examining where bikes typically are rented from and where they are left. You need to make sure you have a complete association matrix in order to do this.

d. Represent the association as an interactive network

You can generate a layout based on the number of trips, or you can use the geographic location of the bike stations.

Code
load(here::here("data/bikes.rda"))

ggplot(bikes$stations, 
       aes(x=long, y=lat)) +
  geom_point()

Code
ggplot(bikes$stations, 
       aes(x=fct_reorder(name, nbDocks), y=nbDocks)) +
  geom_col() +
  xlab("") +
  coord_flip()

Code
trips <- bikes$trips |> 
  tibble()

full_list <- expand_grid(Start.station = unique(trips$Start.station), 
                         End.station = unique(trips$End.station)) |> 
  left_join(trips,
            by = c("Start.station" = "Start.station",
                   "End.station" = "End.station")) |> 
  mutate(n = replace_na(n, 0),
         minlength = replace_na(n, 0))

bike_level <- full_list |> 
  group_by(Start.station) |> 
  summarise(n = sum(n)) |> 
  arrange(desc(n)) |> 
  pull(Start.station)

p <- full_list |> 
  mutate(Start.station = factor(Start.station, levels = bike_level),
         End.station = factor(End.station, levels = bike_level)) |> 
  ggplot( 
    aes(x = Start.station,
        y = End.station, fill = n)) +
  geom_tile() +
  scale_fill_continuous_sequential(palette = "YlGnBu") +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "none")

ggplotly(p, width = 600, height = 600)
Code
stations <- bikes$stations |> 
  tibble() |> 
  select(-id)
  
# graph data structure
bikes_graph <- tbl_graph(nodes = stations, edges = trips)

# kk layout
bikes_graph |> 
  ggraph(layout = "kk") +
  geom_edge_link(aes(edge_alpha = n)) +
  geom_node_point(aes(size = nbDocks)) +
  geom_node_label(aes(label = name), size = 1.5, repel = TRUE,
                  label.padding = 0.15) +
  theme(aspect.ratio=1,
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_rect(fill=NA, 
                                        colour="black"),
        legend.position = "none")

Code
# Spatial layout
bikes_graph |> 
  ggraph(x = stations$long, y = stations$lat) +
  geom_edge_link(aes(edge_alpha = n)) +
  geom_node_point(aes(size = nbDocks)) +
  geom_node_label(aes(label = name), size = 1.5, repel = TRUE,
                  label.padding = 0.15) +
  theme(aspect.ratio=1,
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_rect(fill=NA, 
                                        colour="black"),
        legend.position = "none")

Code
# interactive graph
interactive_graph <- bikes_graph |> 
  ggraph(x = stations$long, y = stations$lat) +
  geom_edge_link(aes(edge_alpha = n)) +
  geom_point_interactive(aes(x = x, y = y,
                             size = nbDocks,
                             tooltip = name,
                             data_id = name)) +
  theme(aspect.ratio=1,
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_rect(fill=NA, 
                                        colour="black"),
        legend.position = "none")

girafe(ggobj = interactive_graph,
       options = list(
         opts_hover(css = "fill:lightblue;stroke:grey;stroke-width:0.5px"),
         opts_zoom(min = 0.5, max = 3)
       ))
Code
# p2 <- ggplot() +
#   geom_point(data=bikes_nodes, 
#              aes(x=x, y=y, label=name)) +
#   geom_segment(data=bikes_edges, 
#                aes(x=x, y=y, xend=xend, yend=yend),
#                linewidth = 0.3) +
#   scale_color_viridis_c() +
#   theme(aspect.ratio=1,
#         axis.text = element_blank(),
#         axis.title = element_blank(),
#         axis.ticks = element_blank(),
#         panel.background = element_rect(fill=NA, 
#                                         colour="black"),
#         legend.position = "none")
# ggplotly(p2, tooltip = "label", width=900, height=600)

👌 Finishing up

Make sure you say thanks and good-bye to your tutor. This is a time to also report what you enjoyed and what you found difficult.