Drill Up and Down on Choropleth Maps in Excel

Interactive drill up and down geographical hierarchies on a Choropleth Map in Microsoft Excel

Drill Down Choropleth Map USA

The post Faster Choropleth Maps with Microsoft Excel provided a faster version to update a Choropleth Map in Microsoft Excel. The approach made it possible to use Choropleth Maps with several thousand regions on an interactive Microsoft Excel dashboard in production.

This also opened up new possibilities to enhance the maps with additional features. Leonid Koyfman contributed a couple of great enhancements in the follow-up article Fast Choropleth Map with Enhanced Features like filtering the data by value bin, showing tooltips and letting the user decide whether the map shall be colored by state or by county.

Very soon after this follow-up article was published, Leonid came up with another great idea. He suggested to take the user selection of how to color the map to the next level: let the user easily drill up and down the geographical hierarchy by simply clicking on the map. One click toggles from coloring the entire state to the counties in that state and vice versa. I have to admit, I am sitting on this nugget for one and half years already and never found (well, more precisely never took) the time to publish it. But finally the time has come. Here it is.

Today’s article explains Leonid’s idea and implementation how to drill up and down geographical hierarchies on a Microsoft Excel Choropleth Map.

The article includes two example workbooks for free download: the USA by states and counties and Germany by the two common ZIP-code levels PLZ2 and PLZ5 (first two digits of the ZIP-code and the entire five digits ZIP-code).

The Idea

Let’s say you have data divided into several geographical hierarchies like administrative levels, e.g. USA by states and counties, France by régions and départements or Germany by ZIP-code levels (“PLZ2” and “PLZ5”).

If you want to visualize this data of both hierarchy levels on one Excel dashboard, you have various options:

  • Create several dashboards, one for each hierarchy. The disadvantage: the user has to switch forth and back between the dashboards
  • Display several maps on one dashboard. The disadvantage: maps require a lot of real estate. You either have to make the maps very small and/or you refrain from showing any other charts and tables on this dashboard
  • Let the user decide (e.g. with an option button) which hierarchy level to be displayed and use the camera object to switch between the geographical hierarchies. This avoids the additional maps, but allows the display of only one hierarchy level at a time

If you are creating an interactive Excel dashboard, the last option is probably the one you would want to go with.

Having said that, wouldn’t it be nice, if the user would be able to define the hierarchy level not only for the entire map, but also for each region individually by simply clicking on the map?

Exactly this feature is the idea of today’s article.

The Functionality

Here is a map of the United States colored by states:

USA by States

Nothing new under the sun. A Choropleth Map with the states colored according to the values in the data.

Here is a map of the USA by counties:

USA by Counties

Same data, same functionality, just a different level of detail.

So what’s new?

It is all on one dashboard and the user can easily switch from one hierarchy level to the other by clicking on one of the map icons at top right of the dashboard.

But that’s not all. The user also has the option to click on any state to toggle the level of detail for only this state. E.g. you are interested especially in the details on the West Coast? Starting with the map showing all data on state level, you need only three clicks to get this:

USA West Coast by Counties

You can switch back to the state level by clicking again or show the entire map by one level of detail with one click on one of the map icons on top right.

The Implementation

The implementation is based on the workbook provided in the article Faster Choropleth Maps with Microsoft Excel. I won’t go into the details of this approach again. If you are interested, please check out that post.

Providing the option of drilling up and down geographical hierarchy levels requires a few additional things:

  • The dashboard contains two entire maps, one for each hierarchy level (e.g. states and counties) sitting on top of each other
  • A naming convention for the shapes is introduced to differentiate between the hierarchy levels. In our implementation we used “S1_” as the prefix for level 1 shapes (states, i.e. “S1_WY”) and “S2_” as the prefix for level 2 shapes (counties, e.g. “S2_WY_Weston”).
  • The basic idea is to make those shapes invisible which are not selected to be displayed, e.g. if only states shall be displayed, all level 2 shapes are made invisible by setting the .Visible property of those shapes to False.
  • The additional VBA code is in the module “modDrill”. There are 4 subs in total:
    • ToggleEntireMapLevel (intLevel As Integer) makes all shapes of the selected hierarchy level visible and all shapes of the other level invisible
    • Toggle2Level1 and Toggle2Level2 use this sub to switch to the according level. These two subs are assigned to the according map icon on top right of the dashboard
    • The sub “Drill” detects which shape has been clicked on and changes the visibility of the according shapes of this state depending on the current status. I.e. if the map currently displays this region on state level, it makes the state shape invisible and all county shapes of this state visible and vice versa.
      This sub is assigned to each shape of both maps, i.e. it is executed each time the user clicks on the map

The VBA code isn’t too complicated. A couple of Ifs, For Each loops, Lefts to get the hierarchy level according to the naming convention (see above) and setting the .Visible property. That’s pretty much it. 79 lines of code. No rocket science. If you are interested in the details, please download one of the workbooks (see below) and have a look.

The Disadvantages

What can I say? It is still a workaround and it comes with a couple of disadvantages:

  • It is quite a challenge to find and insert two maps which fit exactly in size. The shapes always get a bit distorted during the process of converting SHP to SVG, SVG to EMF and ungrouping inserted EMF files in Excel. I think we managed it pretty well in the two examples posted for download, but if you go to a very high zoom level, you will still see some distortions
  • Two maps, a lot of shapes, naming convention, assigning a macro to all shapes, etc. require a lot of time to set up the workbook
  • The performance: although toggling the level of detail does not recolor the shapes, it still takes some time to make shapes visible / invisible. Not as much as recoloring the map when selecting another measure or color scale, but still. However, for a workaround it is still sufficiently fast in my humble opinion
  • Finally, the file size: more shapes = bigger file. The USA workbook by states and counties has more than 2.5 MB, Germany by ZIP-codes PLZ2 and PLZ5 even almost 9 MB. Unfortunately there is nothing you can do about that. The more shapes you have, the bigger the workbook and even zipping the file doesn’t really help

Despite all those downsides, the workaround for creating Choropleth Maps is still a viable option from my point of view. It is free, it works well and it is fast enough even on an interactive dashboard in production.

The Download Links

Here are the Excel workbooks for free download.

The USA by states and counties:

Download Choropleth Map USA by states and counties (Microsoft Excel 2007 – 2013, 2656.4K)

Germany by ZIP-code level PLZ2 and PLZ5:

Download Drill Down Choropleth Map Germany by ZIP-codes (Microsoft Excel 2007 – 2013, 9106.2K)

Acknowledgement

A big thank you very much goes again to Leonid Koyfman for his great ideas, his time, his ongoing support and last but not least his admirable patience with me.

Thanks, Leonid!

Stay tuned.

Comments

18 responses to “Drill Up and Down on Choropleth Maps in Excel”

  1. roberto mensa Avatar

    robert, this is really awesome!
    thanks for sharing!

  2. Daniel Avatar
    Daniel

    That’s really cool!
    Robert, have you ever done any work/analysis regarding sports betting?

  3. Robert Avatar

    Daniel,
    thanks for your comment. You must be really all over the idea of predicting sports data with Excel, aren’t you?
    Well, as mentioned before, I do not.
    However, I recently saw an interesting Monte Carlo simulation of the FIFA World Cup done by the folks of Best Practice Modeling. I only had a very quick look at the model, but it looks really interesting and very professional.
    Have a look:

    BPM – World Cup 2014 – Monte Carlo Simulator

  4. Daniel Avatar
    Daniel

    Ah sorry, forgot I had asked that before 🙂
    But thanks for the link.
    Well I like sports and numbers/finance so it’s s nice comb for me.

  5. Prateek Bhardwaj Avatar

    Very interesting post Robert..thanks for sharing!

  6. Marc Avatar
    Marc

    Hi Robert,
    Thank you for all your posts on Choropleth maps, super helpful!
    Do you know of a way to post the state initials and data point for each state without doing it manually.
    I’m trying to create something similar to this
    http://taxfoundation.org/blog/real-value-100-each-state
    Best
    Marc

  7. Robert Avatar

    Marc,
    you cannot add a text to the freeform shapes used in the maps. Texts in shapes are only possible in standard Excel shapes like rectangles, circles etc.
    The only way to add text to the map would be to insert additional textboxes, assign the name of the region and the data value to the text of the textbox and position the textbox in the middle of the shape.
    The code to add a textbox would be something like this:
    Dim shpText as Shape
    Set shpText = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 10, 10)
    The code to change the text would be:
    shpText.TextFrame.Characters.Text = “Alabama”
    The position of the box could be changed with the properties .Left and .Top
    Having said that, I would strongly recommend against your idea. Adding textboxes to all shapes will definitely clutter the display and cause more confusion than insight. I would rather work with a data table or a bar chart next to the map or provide tooltips.

  8. Ryan Avatar
    Ryan

    How do I get the link to open in an excel file, all I get when I click the link is unuseable folder….. Please help!

  9. Ryan Avatar
    Ryan

    Got it, thanks

  10. Jonathan Avatar
    Jonathan

    I’m new to these maps and I am doing some analysis just on California using the spreadsheet?
    Thank you!!

  11. Jonathan Avatar
    Jonathan

    Hit post before I finished. Is there a way to just display data for a single state?
    Thanks

  12. Robert Avatar

    Jonathan,
    1. Delete all shapes (counties and states shapes, i.e. the visible and the invisible) you do not need
    2. Delete all rows on the worksheet of all counties / states you do not need
    3. Run the Recreate Shape Index sub
    This should do the job.

  13. Jeff Avatar
    Jeff

    In regards to what Jonathan said,
    I am working on a few midwest states, so I deleted the other states that I am not working with but now the map won’t update when I move to a different year. I tried to run the Recreate Shape Index and I got this error..
    Run-Time error ‘-2147024809 (80070057)’:
    The index into the specified collection is out of bounds.
    When I ran the Debug, this is the highlighted section of code:
    varShapeIndex(lngCount) = Sheets(C_SHEET_NUMBER).Shapes(varShapeNames(lngCount, 1)).ZOrderPosition
    Any ideas on how to fix this?

  14. Robert Avatar

    Jeff,
    hard to say without seeing your workbook. Did you update the named ranges, too? Maybe it is a problem with the names. If you want, you can send me your workbook by email and I will have a look.

  15. Damon Avatar
    Damon

    This is an excellent tool…Thank you! Is there an easy way to put the state abbreviations on the map?

  16. Robert Avatar

    Damon,
    please see my reply to Marc in the comment above (August 28, 2014).

  17. bryan r Avatar
    bryan r

    can you please describe how you solved this issue..getting the same thing happening to me

  18. bryan r Avatar
    bryan r

    Ok I got it posted from another comment on another page – see below
    Amit,
    thanks for your comment. Microsoft Excel 2007/2010 files are in fact zipped folders containing XML and other files. Depending on the settings of your system, it may well be that Windows tries to open the file as a zipped folder with Windows Explorer and then you only see the XML files. Simply right click on the link, select Save As and save the file to your computer. You should then be able to open the file with Microsoft Excel by simply double clicking.
    also, try renaming the file when you save as to .xls, this should work 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *