Alternative Map Projections in Tableau

 

If you've already read the blog post and you are just looking for the files, you can download them from my Google Drive here.  Otherwise, please check out the full blog post below.


******************************************************


A map projection is, in its simplest form, a flattened version of our globe.  The flattening of a spherical object is never easy nor perfect, therefore there have been numerous attempts resulting in numerous different map projections. 

 

As a standard, Tableau, and most mapping applications, utilize the Mercator projection.  The Mercator projection is a cylindrical map projection, which means it essentially takes the spherical Earth, turns it into a cylinder, which is then flattened as a map.  The problems with the Mercator projection are well documented.  The major problem is that it distorts the size of geographical objects more and more as you get further away from the equator.  This can be easily demonstrated with two different examples.  The first shows what Greenland looks like in the Mercator projection then shows its actual size (from https://thetruesize.com/).





How crazy is that?  Many years ago, Ken actually created a viz and blog post about this very topic where he shows this comparison of Greenland (in blue) to Africa on the Mercator projection versus real world. 




The second example is something I shared on Twitter a couple months back.  It uses a Mercator projection map in Tableau along with a buffer calculation.  In this example, the buffer measures 1000 km (~620 miles).  Check out the size of the buffer near the equator then how it increases as you move away from the equator…pretty insane, huh?





 

  

If you want to learn more about this topic, I’d suggest you check out the 20 minute presentation from Adi McCrea on the DatafamCommunityJam Episode 4 (about 4:30 minutes into the video).


As mentioned above, there are numerous different map projections.  And although the Mercator projection is the standard in Tableau, we can implement some of these different map projections fairly easily.  There are a lot of different projections as well as a lot of different techniques (shapefiles, polygons, etc.).  To keep this simple, I’m going to focus on the United States and utilize the Lambert Conformal Conic projection via a shapefile, which I obtained from the brilliant (and regular FlerlageTwins contributor) Sarah Battersby on the Tableau Forums.  (Note that the Lambert Conformal Conic utilizes a cone instead of a cylinder.  You can read more about it here).



WHY?

 

But before we get into “how to do it”, let’s talk about why we might want to use an alternative projection shapefile?  I mean, it’s really easy to build maps in Tableau, why would we want to take these additional steps?  For me, there are three major reasons.

 

1) It is More Accurate

 

As we saw above with the Mercator projection, geographic objects become much more skewed the further they are from the equator.  Using a different map projection can improve upon this problem leading to more accurate maps.  That said, I should probably be a bit more careful with my loose usage of the phrase "more accurate".  Every map projection is a compromise.  In order to address the problems of projections like Web Mercator, map projections may introduce other distortions such as the shape of the land, etc.  

 

2) Eliminate the Alaska Effect

 

If you’ve ever built a US map in Tableau, you probably know what I’m talking about.  Alaska is so large (well, it appears so large in the Mercator projection) and is so far away from the lower 50 states, that it makes the lower 50 states much more difficult to read and it just looks bad. 





 

There are plenty of workarounds for this.  If you are not worried about geography, hex maps are a great option.  You could also create additional sheets, i.e. one for Alaska, one for Hawaii, and one for the lower 50 states.  Unfortunately, this takes a lot of additional work, it is difficult to float those sheets exactly where you want without covering portions of the other sheets, and it is just really clunky. 

 

The Lambert Conformal Conic projection shapefile that we will be using today takes Alaska and Hawaii and places them to the bottom left of the lower 50 states.  It does, however, decrease the size of Alaska to about 50% as well as adjusts Hawaii slightly simply so that they are a better fit; please keep this in mind.  (Note that sizing is also a major problem if you were attempting to float Alaska and Hawaii in other sheets). 





 

3) It Looks so much Nicer!!!!

 

I think our dataviz work should not only be quick to insight and informative, but also engaging…it should look good!  I don’t know what it is about the Lambert Conformal Conic projection (or any conic projection) compared to the Mercator projection, but it just looks sooooo much nicer!  It is really polished.



 


HOW?


Okay, so now that we know why we might use an alternative projection, how do we do it?  Well, it’s not too difficult.

 

As mentioned above, we will use the Lambert Conformal Conic projection.  For the purpose of this blog post, we will focus solely on the United States and will plot a map by county then one by state. 

 

Before getting started, let’s talk about the data.  In order to utilize these shapefiles, you are going to need FIPS codes.  FIPS (Federal Information Processing Standard) codes are codes to specify certain geographical regions in the US.  For plotting a county map, we need to understand state FIPS codes and county FIPS codes.  A state FIPS code is a unique two digit number that identifies a state.  For example, Kentucky’s FIPS code is 21.  A county FIPS code is a three digit code that identifies a county.  For example, I live in Boone County and the FIPS code is 015.  However, county FIPS codes are not unique in and of themselves; in fact, 46 states have a county with a FIPS code of 015.  So when plotting a map at the county level, you must utilize both state FIPS and county FIPS in order to result in a unique county.  To stick with our example, 21015 is unique to Boone County, Kentucky. 


If you are working with county-level data at work, it is very likely that FIPS codes are already in your table or you have access to FIPS codes in your database.  If your data does not already contain FIPS codes, you can download the codes from Census website.  That file comes in a format that requires a bit of cleanup, which I’ve done already.  So use my cleaned up file insteadJ.  Below is a snippet of the file.  (I should note that one issue with FIPS codes is that sometimes they have the leading zero and other times they do not.  In my file, I've turned them into integers with no leading zeros.  However, if you want to join on both State and County codes at the same time, I've included a combined code which includes the leading zero for counties).  





The spreadsheet lists every county (some are called parishes, census areas, or boroughs) in the US (3138 of them) along with the county’s FIPS code and the state’s FIPS code.  In this spreadsheet, I changed the codes to be integers versus strings (1 versus 001).  I’ve also included a Combined FIPS Codes columns which is simply the combination of state FIPs and county FIPS. 

 

If your data doesn’t contain FIPS codes, you will need to join or do a vlookup from this spreadsheet in order to obtain the codes.  I’m not going to go into a lot of detail here, but if working with counties, you will need to bring in both the state FIPS and the county FIPS.  In doing so, you are going to have to do a join or vlookup on the state and county names.  Joining on state names is typically pretty straightforward and if you were to get a null, then you should be able to quickly fix it.  Joining on county names can be a bit more work and you may see a number of nulls, especially in Alaska where they utilize census areas and boroughs versus true counties.  In this case, it’s going to take a bit more work for you to clean up your data in order to get the FIPS codes into your dataset.

 

At this point, we are going to assume that your data contains FIPS codes for both state and county and that those FIPS codes are integers, like in my spreadsheet. 

 

Before we jump into Tableau, please download the data we will be visualizing (Poverty Rates) and the shapefiles (County & State - Lambert Conformal Projection.zip) from this Google Drive folder.  I’ve also include the FIPS codes here.  (If you are unable to access it, please send an email to flerlagek@gmail.com).  The shape files are in zip format, go ahead and unzip them.  (All credit for these goes to Sarah Battersby – thank you for being so awesome, Sarah!  You can read more about how she developed these here.)  You should also feel free to follow along with my pre-built visualization on Tableau Public. 


Okay, let’s get into Tableau!  Connect to the Poverty Rates data spreadsheet and drag the County Poverty Rate worksheet to the data pane.  Remember that I’m assuming you have brought in your FIPS codes, therefore my Poverty Rates data includes these codes.   Although you could certainly use a relationship, I am going to use an old school join (since many people are not yet on a version which contains that feature).  So double click on the County Poverty Rate table to allow you to use a normal join. 

 

Next, click Add to bring in an additional file and choose a spatial file.  Since we are working with county level data, navigate to the folder in which you extracted the shapefiles and choose cb_2015_us_county_5m_lambertConformal-fauxWebMercator.shp.  It should bring in both the county and state files, with county on top (or you can hover to see their full names).  Drag the county shapefile out to the right to join to the County Poverty Rate data. 

 

As mentioned earlier, to get down to a unique county, you must utilize FIPS codes for both state and county.  Therefore, there will be two join conditions, one on state and one on county.  In the County Poverty Rate data, these fields are called State FIPS and County FIPS and you should recall that that are integers.  The corresponding fields in the shapefile are named Statefp and Countyfp and are strings.  Let’s create our first join condition.  For the poverty data, choose State FIPS.  For the shapefile, choose Create Join Calculation.  Here we need to change that string to an integer so that we have the same data type.  To do this, enter INT([Statefp]).  We will do the same with county by joining County FIPS to INT([Countyfp]).






Now that the joins are set, let’s go to a sheet and build our county map.  To build the map, simply double-click on Geometry.  You should now have a map that looks like the following:






 


On the top menu, go to Map and choose Map Layers then uncheck ALL map layer checkboxes. 

 

Okay, let’s color the counties by Poverty Rate by dragging the Poverty Rate pill onto color.  Nothing really happens other than it aggregates the poverty rates for the entire US.  Although we have the shape of these counties, we still need to tell Tableau how to break it out.  So let’s place State FIPS and County FIPS onto the Detail card (you’ll need to first change them both to discrete dimensions). 

 

Now you should see poverty rates represented at the county level.  To confirm its accurate, hover over the largest county/census area in Alaska (Yukon-Koyukuk) and you should see a poverty rate of 23.8%. 

 

If you want to make it look like my Tableau Public example, on the Color card, change the Opacity to 92%, change the borders to None, and change the color palette to Inferno (you may need to obtain this color palette codes using Jacob Osulfka’s Tableau Public Color Palette viz).  The result should look like the following, which I personally think is gorgeous!





You can do the same thing with a state map as well.  My Poverty Rate spreadsheet includes a worksheet called State Poverty Rate (which also contains FIPS).  Just connect to that spreadsheet, join to the cb_2016_us_state_5m_AKHIShift-fauxWebMercator.shp shapefile and join on State FIPS = INT([Statefp]), then follow the same basic steps but focused solely on state, not county.  (This shapefile actually contains state names and abbreviations, so you can join on these fields instead of FIPS if you wish). Your end result should look something like the following:




Again, if you want to see this in action, you can check out my Tableau Public example.

 

As mentioned above, there are numerous options and techniques for implementing alternative map projections in Tableau.  I do believe that utilizing a shapefile (as described above) is the best method as well as the easiest method.  However, you can read about another method that my brother implemented a couple years back for IronViz using polygon files. Note that this method, in his words, is a bit antiquated.


And last, but certainly not least, Adi McCrea suggested the Dirty Reprojectors website where you can download all kinds of different map projections.  You can choose from a variety of different projections, download them as geojson files, then connect to them via the Spatial file option.  It's really incredible!  Thank you, Adi!


And that’s all there is to it.  Thanks so much for reading and as always, please feel free to reach out if you ever have any questions.  Thanks!

 


Kevin Flerlage, March 22, 2021

Twitter | LinkedIn | Tableau Public

 


 


18 comments:

  1. Hi - this is FANTASTIC! I was wondering if you had a state shapefile with the projection with the territories (PR, Guam, American Somoa) you could add to drive? Would love it if I could also show those areas with that lovely projection.

    ReplyDelete
    Replies
    1. Thanks so much. I actually do have one with the US Territories. I've added it to the Google drive. It is a hyper file called US Territories. You can connect to it in a similar way you did the shapefile. Let me know if you have any issues.

      Delete
    2. THANK YOU SO MUCH! Kevin this is absolutely wonderful - as GIS person who ends up having to use Tableau sometimes it makes me so happy to be able to use a much nicer map projection. I really appreciate your guide (and your blog)!

      Delete
    3. Awesome! Thanks for the kind words!

      Delete
    4. Kevin - Thank you very much for this. I found this page recently and it saved the day. I am now using the state map with territories for many projects at my org. We do a lot of map building and embedding on our webpage and need to include the US territories and this checks all of the boxes. -Eric

      Delete
  2. Thanks for the blog Kevin. While I went to Dirty Reprojectors website, I downloaded the spatial file of rivers but while connecting it in tableau it is giving an error saying that the Line string must have two coordinates. Could you please help me in fixing the error?

    ReplyDelete
    Replies
    1. I got the same error. I apologize, but I'm not really sure what is happening on that one.

      Delete
  3. Thank you so much for this detailed explanation and for both you and your brother continue to do for the Tableau Community! You wouldn't happen to know if there is anywhere I could find the map shapefiles (Lambert Conformal) in more recent (i.e. 2019, 2020) versions with county or place level detail? I know Sarah has some guidance on how to use QGIS to create these, but it's a bit of a learning curve for me to use QGIS. If not, no worries and thanks again!

    ReplyDelete
    Replies
    1. Thanks for the kind words! In this blog post, I include the county level shapefile. What kind of detail are you looking for when you say "place level detail"?

      Delete
    2. Yeah, the file you included is from 2015. The geographies sometimes change year to year with the Census updates. This is usually minor, but if the newer census data (FIPS Codes) change it throws things off with the joins to the shapefiles.
      Place=Cities https://www2.census.gov/geo/docs/reference/codes/files/national_places.txt

      Delete
    3. Ah yes, I see. Great point. I am not aware, but did you check out the link at the end of the blog post. It has a lot of options, but not sure it really has what you are looking for. It's certainly not my area of expertise either. https://devseed.com/dirty-reprojectors-app/

      Delete
  4. Thank you for such great information!

    ReplyDelete
  5. I'm a 30 year veteran of GIS and was totally losing my mind wondering why your shape worked and mine wouldn't. Then I read Sarah's blog. Before I go on, THANK YOU GUYS!

    Okay, GIS nerds, here is the key: change the header in the .prj to fool Tableau into believing the projection is Mercator. Just replace whatever is there with this:

    PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.017453292519943295]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]

    ReplyDelete
    Replies
    1. I've run into this a couple of times--thanks so much for sharing the tip!

      Delete
  6. Hola Ken, consulta quiero hacer un Drill Down en un mapa, pero como tengo que hacer una unión de un archivo gis y otro de Excel, cuando creo la región no me desaparece la division menor. Es esto posible? o solo con el base mapa que viene de tableau? Otra consulta puedo generar mi propio mapa base (mapbox) en tableau, sin necesidad de usar un gis? Al ser de Argentina, no tiene una división para elecciones que si hay en EEUU. Muchas Gracias

    ReplyDelete
    Replies
    1. I translated your message, but I believe I'm going to need more information on what you are trying to do. Your note mentions GIS, but this blog post doesn't mention it at all. Please advise. Thanks! :)

      Delete
  7. Any tips on if you want to display state abbreviations? They're off-center since the conical projection shifts the map

    ReplyDelete
    Replies
    1. If I add State Abbr from the shapefile, the labeling looks fine to me. Are you saying they are just slightly off center? If so, you can click on those labels and just move them around.

      Delete

Powered by Blogger.