Get startedGet started for free

Compare graph distance vs. geographic distance

1. Compare graph distance vs. geographic distance

It's important to keep in mind that a graph is an abstract representation of connected things, and often they are connected in physical space. Because of that, properties of a graph can be constrained by, and often reflect, the underlying nature of the system. In this case, the graph represents the geography of the city of Chicago. In this video we'll take a look at how graph distance is related to geographic distance.

2. A map of the graph

Here we can see an overview of the all the bike sharing stations (with no edges drawn) that represent the vertices in our graph. Now let's look at graph distance and plot that back on our map.

3. Graph distance

Here we've identified the longest shortest path between two vertices. There are two functions we can use: farthest_vertices() which tells us the two end vertices and the path length, and get_diameter() which returns the actual path. We might expect these two vertices to also be quite far geographically so next let's plot these on the map.

4. Graph distance vs geographic distance

Now we can see that the vertices with the longest shortest path are quite far apart on the map as well. But just how far apart? Luckily our initial data frame has the latitude and longitude of each bike station. Let's walk through how we can find the geographic distance.

5. Geographic distance

To find the farthest distance we only need to use a simple function in the geosphere package. The real guts are just grabbing a single vector of latitude and longitude from the dataframe. To do that we use dplyr to first filter by station_id, take just one row using sample_n(1) and and lastly select just the longitude and latitude. Once we have a vector, we can just input these into the distm() function.

6. Geographic distance

Because we may want to do this on more than one station, we can easily convert these few lines of code into a function to use later called bike_dist().

7. Let's practice!

Now let's start to use our bike_dist function to better understand how the geographic distance relates to different properties of the bike sharing graph.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.