Using path to display movement in map

We can visualize movements in Tableau by drawing lines between points. This takes advantage of the Line mark and the Path property.

In this recipe, we are going to visualize who Shaquille O'Neal played for from 1992 to 2011:

Using path to display movement in map

Getting ready

To follow this recipe, open B05527_05 – STARTER.twbx. Use the worksheet called Using Path, and connect to the Player Stats (NBA Players Regular Season 2009) data source:

Getting ready

You will also need to download the team logos you would like to use (and, of course, check the term agreements to using these logos if you need to use these in public dashboards). Create a folder called NBA in the Shapes folder, and place the icons in this folder:

Getting ready

How to do it...

Here are the steps to create the map with path lines:

  1. Double-click on Latitude and Longitude under Measures. Make sure you double-click on the data source fields, not the Tableau-generated fields. By default, AVG(Longitude) and AVG(Latitude) should be the aggregations that are used for these fields:
    How to do it...
  2. Use Ctrl + drag with the Latitude pill in the Rows shelf to the Rows shelf to copy it:
    How to do it...
  3. Click on the first AVG(Latitude) pill to activate the first Marks card.
  4. Change the mark of the first AVG(Latitude) on Marks card to Line.
  5. Drag the ID field from Dimensions to Path of the first Marks card.
  6. Select the second AVG(Latitude) on Marks card and change the mark type to Shape.
  7. From Dimensions, drag Team Name to Shape in the second AVG(Latitude) on Marks card.
  8. Click on Shape and assign the icons to the correct team:
    How to do it...
  9. Adjust the size of the team icons. Click on the Size property in the Marks card and slide a little bit to the right to increase the size of the team icons.
  10. While the second Marks card is still selected, drag the following dimension fields to Label: ID, Team Name, From-To, and # Years Label.
  11. Adjust the label formatting to look similar to the following screenshot:
    How to do it...
  12. Right-click on the second AVG(Latitude) pill in the Rows shelf and click on Dual Axis.

How it works...

In this recipe, we visualized which teams Shaquille O'Neal played for. We represented the teams with the team logo, and drew lines between teams he moved from and to. These are two different marks; therefore, we needed to use a dual axis graph.

To show the teams using the team logo, we simply need to use the Shape mark type. Before this, we need to load the team logos into Tableau Repository's Shape folder:

How it works...

If you create the folder and copy the icons while you have Tableau open, you will need to click Reload Shapes in the Edit Shapes window first before you can see it:

How it works...

To draw the lines between the teams, we first need to change the mark type to Line. When we change the mark type to Line, a new property appears in the Marks card, which is Path:

How it works...

To better understand what this Path property is for, we can start by understanding what the term path means. One of the definitions of path in www.dictionary.com is as follows:

A route, course, or track along which something moves

This is exactly what Path helps with—we are trying to visualize a route. In our recipe, we are trying to picture Shaq's movement from team to team. Since we want the movement to be influenced by when Shaq moved, we place the Year field onto Path. This, together with the Line mark type, draws the lines, in sequence, between the teams Shaq played for. For other use cases, a date, date/time, or sequence number can be placed onto this Path property to control where the lines go.

There's more...

It is possible to calculate distance between locations in Tableau. To calculate distance, we need to have the source and destination latitude and longitude values in the same row. We can achieve this by either manipulating the data source so it already has it, or by performing a self join (that is, a table being joined to itself).

If the data source is Excel, we can do a self join if we connect to it using Legacy Connection:

There's more...

Legacy Connection uses the Microsoft Jet Data Engine driver, which has since been replaced by Tableau.

Note

The Legacy Connection option was introduced in Tableau 8.2. You can learn more about this in the Tableau KB article Differences between Legacy and Default Excel and Text File Connections, which can be found at http://bit.ly/tableau-legacy-jet.

Choosing Legacy Connection enables us to have a new option called New Custom SQL:

There's more...

The self join query will look like the following code:

SELECT 

  [Shaq$].[ID] AS [ID1], 
  [Shaq$].[Team Name] AS [Team Name1], 
  [Shaq$].[Latitude] AS [Latitude1], 
  [Shaq$].[Longitude] AS [Longitude1], 
  
  [Shaq2$].[ID] AS [ID2], 
  [Shaq2$].[Team Name] AS [Team Name2], 
  [Shaq2$].[Latitude] AS [Latitude2], 
  [Shaq2$].[Longitude] AS [Longitude2]

FROM 
  [Shaq$] 
  LEFT JOIN [Shaq$] [Shaq2$] 
  ON ([Shaq$].[ID] = [Shaq2$].[ID] - 1)

Once the latitude and longitude values of the previous and next teams are in the same row, we can use the great-circle distance formula to calculate the distance. The great-circle distance has been defined in Wikipedia (https://en.wikipedia.org/wiki/Great-circle_distance) as follows:

The shortest distance between two points on the surface of a sphere, measured along the surface of the sphere (as opposed to a straight line through the sphere's interior)

The procedure for calculating the distance in Tableau is documented here - http://bit.ly/tableau-distance. The formula for the Tableau calculated field will look like this:

3959 * ACOS 
( 
SIN(RADIANS([Latitude1])) * SIN(RADIANS([Latitude2])) + 
COS(RADIANS([Latitude1])) * COS(RADIANS([Latitude2])) * COS(RADIANS([Longitude2]) - RADIANS([Longitude1])) 
)

Once we have the distance values, we can utilize them on the maps:

There's more...

One more tidbit about Tableau maps. If you zoom in enough and use the Radial Selection tool (the dashed circle icon) from the View Toolbar, you will be able to see the distance between two points. The limitation is this is fairly visual in nature and currently cannot be annotated or saved like a calculated field:

There's more...

See also

Please refer to the recipes in Chapter 7, Data Preparation

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.228