Fast Choropleth Map with Enhanced Features

Enhance a detailed Choropleth Map in Microsoft Excel with additional features

Choropleth Map with enhanced featuresThe recent article Faster Choropleth Maps with Microsoft Excel provided a faster version of the VBA code to update a detailed Choropleth Map in Microsoft Excel.

Leonid Koyfman, a faithful reader of Clearly and Simply liked this article. Leonid already contributed his invaluable ideas and insights here before (Excel Multiple Value Filters with Invert Selection). He had a couple of very interesting ideas for the fast Choropleth Map and he is kind enough to share them with us:

 

  1. Let the user filter the data by value bin and thereby highlight the bins of interest on the map
  2. Show tooltips when hovering over the map to display the name of the county and the unemployment rate in percent
  3. Let the user switch the level of detail: color the map by county or by state

Today’s article describes Leonid’s enhancements and includes a link to the Excel workbook for free download.

Enhancement 1 – Filter by Value Bins

A checkbox in the map legend for each bin lets the user filter the data and highlight the corresponding counties to the bins of interest. Additional icons above the legend allow to select all, deselect all or invert the selection with one click.

The main steps of the implementation of filters:

  • add checkboxes to the legend of the map
  • define a range on worksheet [control] as the target range for the checkboxes
  • add 3 icons for select all, deselect all and invert selection
  • add a cell to the worksheet [control] to let the user define the fill color of the shapes which are not included in the filters (could be white, could be light grey or whatever you’d prefer)
  • in the VBA we need a new module handling the clicks on the new icons. The subs are pretty easy to understand, please have a look for yourself. The idea and the implementation is along the lines of what Leonid already contributed in this post way back in 2011: Excel Multiple Value Filters with Invert Selection.
  • finally in the sub which updates the map we need one more array (varFilters). This array is initialized with the user’s filter selection and which will be used afterwards in an IF-statement to decide if the current shape will get a fill color from the color scale (if it belongs to a bin selected in the filter) or the default fill color, if it belongs to an unselected value bin.

Enhancement 2 – Tooltips

The Choropleth Map provides a very good visualization of the geographical distribution of the data, but it lacks a feature to also access the exact values of the counties or states. One of the best ways of showing additional information on graphs or maps are tooltips. Hovering with the cursor over the map displays a small text box and shows the name of the county and the unemployment rate.

The trick is to assign a hyperlink to each shape. The link has no target address, but the screen tip is used to automatically display a tooltip. After the user changed the data to be visualized, the VBA code loops through all shapes and assigns the name of the county and the unemployment rate to the shapes’ screen tips. Basically a very simple piece of code: a For Next loop and a .Hyperlinks.Add statement within the loop. I used this little trick also in one of the workbooks provided in the post Spice up your Choropleth Maps with Excel.

Enhancement 3 – Switch between Counties and States

The original intention of the whole project was a detailed map of the US by counties (more than 3,000 shapes) responding within an acceptable time frame. For a map by states, the template provided here based on the original implementation of Tushar Mehta would have been fully sufficient.

Having said that, an interactive feature to easily switch the visualization from county level to state level and back can be helpful for analyzing the data.

Implementing this feature is a piece of cake and does not even require any changes in the VBA. All we need is

  • the data by states
  • radio buttons to toggle between “by counties” and “by states”
  • an update of the formulas to fill the named range “myValues” with the data according to user defined level of data: a simple formula using CHOOSE, INDEX and MATCH and you are good to go

Please be advised that the tooltips (see enhancement 2 above) will show the same value for each county of a state: the unemployment rate of the state, not the individual unemployment rate of this county.

That’s it.

Calling the VBA Subs

In order to optimize performance, there are now 2 main subs which are called from the different interactive controls on the dashboard: the first one will update the map and the tooltips (i.e. if the user changed the year or switched from counties to states) and one without updating the tooltips (i.e. if the user only changes the filters or the color scale).

The Performance

It comes as no real surprise that the additional lines of code for realizing the new features require additional run-time of the VBA. In other words, updating of the map takes longer than before, but I think the performance is still acceptable:

Updating the map after changing the visualized values (the year of the unemployment rates or switching from county to state level or vice versa) takes 8.8 seconds with the progress update in the status bar and 4.7 seconds without the status bar. If you only change the color scale or the filter settings, the VBA takes 5.4 seconds to update including the status bar updates and 3.3. seconds without the progress information on the status bar.

It is a matter of taste whether you want to accept the additional time for the status bar updates. To be honest, I do not have a recommendation for you. 4.7 seconds is already on the borderline to leave the user without any information on the progress. However, 8.8 seconds is almost twice the time. If your users know about the fact that the update takes a few seconds, I would probably go without updating the status bar. You can easily set the status bar update on worksheet [control]. You do not have to change the VBA. Please decide for yourself.

The Download Link

Here is the Microsoft Excel workbook for free download:

Download Choropleth Map US Counties Enhanced (Microsoft Excel 2007/2010 workbook, 2,562.3K)

More Ideas

There are many more things you could wish for. Here are just 2 obvious ones:

  • Add a bar chart to show the exact values of the counties. Certainly not possible with a standard Excel bar chart, but Jon Peltier’s Accordion chart may be an alternative: Accordion Chart.
  • Add an interactive feature to display the unemployment rate over the years after clicking on a county, like I showed here: Spice up your Choropleth Maps with Excel. You would lose the hyperlink tooltips, but the user would get even more information by one single click.

About Leonid

Leonid Koyfman is a BI professional and Database Architect. He is passionate about database design, dashboards and data visualization and an outstanding Xcelsius expert. Leonid has published several posts on Everything XCelsius, the Xcelsius Guru Network. Read more about Leonid in his introduction on Everything XCelsius and also have a look at his guest posts there:

Dynamic status update of Check Box components in Xcelsius: Part 1/4

Dynamic status update of Check Box components in Xcelsius: Part 2/4

Thank you, Leonid

A big time thank you very much goes to Leonid for all his great ideas and for contributing content to Clearly and Simply. This is greatly appreciated.

Thanks, Leonid!

What’s next?

Performance of VBA code is a very interesting topic and I am planning one or two more posts on this very soon. Next article, however, will be a post on the power of web page objects on Tableau dashboards.

Stay tuned.

Update on Monday, 10th of September, 2012

Enhancement 3 (see above) came very late to the party and Leonid and I added this feature last-minute before publishing. In the aftermath we discussed whether it is really the best option to show the name of the county in the tooltip, but the unemployment rate of the entire state (if “by state” is selected).

In general, there are 3 different options:

  1. The way described above: the tooltip shows the name of the county, but the unemployment rate of the state. The advantage: the tooltip provides the name of the county and the unemployment rate of the state and this corresponds with the color of the map. The disadvantage: it may confuse the user because the level of detail does not match in the tooltip (county name, but unemployment rate of state).
  2. Another option would be a tooltip showing the name and the unemployment rate of the county. This is not recommended, since the value shown in the tooltip would not be in line with the color of the county.
  3. Third option is a tooltip displaying the name and the unemployment rate of the state. All counties of a state have the same tooltip, but it is very straight forward and corresponds exactly to the selected level of detail and the visualization.

Option 3 as an alternative to the version above does not require many changes. All you have to do is change the named formula “myRegions” to switch between county names and state names depending on the user’s selection (radio buttons). You could do this with a CHOOSE function, an IF clause or an INDEX formula.

Here is the alternative workbook for free download:

Download Choropleth Map US Counties State Tooltips (Microsoft Excel 2007/2010 workbook, 2,487.7K)

Comments

26 responses to “Fast Choropleth Map with Enhanced Features”

  1. Marko Avatar
    Marko

    Wow, I feel pretty good about excel but you always amaze me with your work…fantastic!
    Keep it up Robert!

  2. Doctor Science Avatar

    You are truly one of the heros of the internet. Thank you for these templates!

  3. Marcos de Brito Avatar

    Thank you very much for this very useful tool. I use it to colorcode our unemployment maps (I work at the public unemployment service in Austria). I have just one question: Is there a possibilty to have more than one and different maps in a file with different color codings and data? Lets say one map with unemployment rates from 0% to 12% and a second map with difference in percentage of unemployment from last year from -20% to +20%? (maybe then a third map and so on…)I am real bad at VBA, so I am not able to do it myself. Now i just use three different Files.
    Thank you anyway for the great ressources on your site.

  4. Robert Avatar

    Marcos,
    thanks for your comment.
    Yes this is possible, of course. It requires some changes in the workbook (like additional lookup tables to assign the values to the colors, additional named ranges, etc.) and some changes in the VBA. Not too complicated, just some laborious work, but too much to explain it in a comment here. If possible, send me your workbook(s) by email and I will try to pull together an example for you (“Email Me” link at the top of the blog).

  5. Denis J Collins Avatar
    Denis J Collins

    Hi, this site is amazing, the tips and ideas go beyond the basic and make the common everyday tasks look great and the great tasks look amazing. And, you give free examples, workbooks….
    I am running MS Excel 2010 and the zip file link for the Download Choropleth Map US Counties State Tooltips (Microsoft Excel 2007/2010 workbook, 2,487.7K) does not have an excel file……..What am I missing to open this file…?
    Thank you for the blogs

  6. Rust Avatar
    Rust

    My suggestion is to have a default color if the value for a county is not available. My data did not have values for half of the counties which gave me different kinds of bugs.
    I did not want to change the code so i replaced the blanks with a dummy value in one of the dropdown(below the years) options which i then used to reset the map to all-white or all-black.

  7. Robert Avatar

    Denis,
    thanks for your comment and your kind words.
    Excel 2007/2010 files are in fact zipped folders containing several XML and other files. If you try to open the document by directly clicking on the download link, it may well be that Windows opens the file using the Windows Explorer (as a zipped folder) instead of Excel.
    As far as I know this issue only occurs with Internet Explorer and Opera. Either use Firefox or Google Chrome to avoid this or right click on the download link, select Save As, change the file extension from .zip to .xlsm, save the file to your hard drive and open it by double clicking.

  8. Robert Avatar

    Rust,
    thanks for your comment.
    You are right, the current implementation does not consider missing data, i.e. blank cells will be treated as a zero value.
    I think the easiest way of resolving this is to use one of the color code categories for the missing data. Assuming we still have percentage values as the data, here is the step-by-step:
    1. Replace all missing data on the data worksheet (blank cells) by a dummy value of e.g. 999%
    2. Change the color of the last color category to e.g. a light gray (indicating missing data)
    3. Change the thresholds of the last 2 categories (100% and 1000% instead of 18% and 100%)
    4. Change the legend of the last 2 categories (“more than 16%” and “data not available”)
    5. Run the macro UpdateColorScale to update the RGB color values and to update the map

  9. Santri Avatar
    Santri

    I need to increase no. of color scales to more than 10. is there a way to do it? I am not that good with Macros. Any help would be great!! Thanks 🙂

  10. Robert Avatar

    Santri,
    you do not have to change the VBA:
    Simply insert additional rows in the color scale matrix on worksheet [control], copy down the formulas, enlarge the legend on the dashboard and adjust the named range myLegend and it should work.

  11. David Bolton Avatar
    David Bolton

    Hi Robert,
    Wonderful web site and thank you for the great information.
    I am having a problem when replacing the US map with one of Western Australia divided into postcodes.
    I manually named the shapes in the form PC_XXXX_WA. I entered the new names on the table, adjusted the myShapeNames and myShapeIndex to cover them. On the map are some other shapes that are not named.
    My problem is that when I run the macro it starts filling in the shapes that are not listed in the myShapeNames range.
    Is there a solution to this please?
    Many thanks,
    David

  12. Robert Avatar

    David,
    thanks for your kind words.
    If you have other objects on the sheet than the shapes of the map, you have to make sure they are the last objects on the sheet.
    Go to the Selection Pane (Home Tab | Find&Select | Selection Pane or ALT-F10). The additional objects have to be at the top of the list shown in the Selection Pane. If they aren’t, you can rearrange the objects with the little arrows at the bottom of the Selection Pane.

  13. dcsl Software Avatar

    Thanks very much for the this very practical and useful tool . It always amazes me the people like you make these and then give them away .
    thanks

  14. Carla Avatar

    Hi Robert,
    I can’t believe I just discovered your site!
    I’m using the “simple” choropleth, without the enhancement cause I just need the countries – from here http://www.clearlyandsimply.com/clearly_and_simply/2009/06/choropleth-maps-with-excel.html
    How can I add only the filters?
    Thanks!

  15. Robert Avatar

    Carla,
    if you want to provide a filter for the categories in the simple version of the Choropleth Map, you do not have to change the VBA. Simply conduct the following steps:
    1. Define a column on worksheet control as the target cells of the checkboxes, let’s say in column I. The values in those cells will be TRUE if the checkbox is checked and FALSE if it is not checked.
    2. Change the formulas in column E:
    Replace
    =udf_RGB(C15,C15,C15)
    by
    =IF (I15, udf_RGB(C15,C15,C15),udf_RGB(255,255,255))
    If the value in column I is FALSE (i.e. the filter is set and the category shall not be colored), the according region on the map will get a white fill color (RGB 255, 255, 255).

  16. Laurent Bosc Avatar

    Hello,
    thank you for this wonderful and useful tool.
    However, i have a suggestion:
    We could speed up perf by using 2 maps. One for county and one for state. So the state map would be quickly updated Since a few number of shapes.
    Do you think it is possible to dynamically change the map, depending on the user choice : state or county?
    I suppose this would have an impact on data structure and/or vba code.
    Moreover this would have more benefits : with a system where the map can be changed, we could use it in order to have for example a worldwide map and several maps by region (north America, south America, Europa, etc…), one by region, which provide like a zoom for the user (the user would select the target map previously of course)
    It would be very useful.
    If someone is able to do it, it would be a great improvement. ( i have not the ability to do it 🙁 )
    Thank you

  17. Robert Avatar

    Laurent,
    thanks for your comment. It is possible to display different level of details of the map (i.e. state and county) by using different maps on top of each other and quickly let the user toggle from one level to the other by changing the visibility of the map shapes via VBA. Actually, I do have a template for this already. It is still on my list of blog posts waiting to be finalized and published (hopefully soon). Having said that, it would not really speed up performance if you change the metric to be displayed or the color scale. On the contrary: both maps have to be updated (in order to allow a quick toggling from one level to the other) and this will need even more time,
    As per your second question: zooming in and out of a map is more complex. It is not impossible, though. One idea would be to have all maps (World, North America, South America, etc.) on a different sheet and show the selected map using the camera object. There are some disadvantages coming with this, of course: it requires a lot of manual set-up for each new map, you have to align the sizes of all maps, you would lose the tooltips, etc. As I said, it is not impossible, but it requires a lot of manual set-up.

  18. Laurent Avatar
    Laurent

    Robert, Thank you for your reply.
    My intention is just having 2 levels of maps, not really zoom / unzoom. So use several maps and display only one visible at a time is a good idea indeed ! And sufficent.
    About perf, I understand in you County/state maps why you want to Quick toggle.
    This is globally the same map (USA), so you want to make the map change, invisible for the user.
    But in my example with ww map and several region maps, the user have to manually select a map to be displayed.
    What is the problem, if the user select a map, then this will display the right map and recalculate ? –> if the map has few shapes, it will be quick, otherwise it will be longer (please note that ww map and region maps would not have a lot of shapes… So probably it will be quick in all cases).
    So no need to keeppîng updated all maps (and keeping updated several maps (probably between 5 and 10) is not a good idea…).
    Just recalculate on demand based on the map selected, no ?
    Hope you will be able to finalize your template and post it soon 😉
    If i bring some improvements in the next weeks. I will send it to you 😉
    Nb: other point : i have seen a web tool call targetmaps with a basic but useful feature: when the user click on a country, a triangle shape is displayed just at the right of the legend in order to really see the color of the legend used for this country. Simple and very useful.

  19. Robert Avatar

    Laurant,
    1. Performance
    You are absolutely right: for a world map with ca. 200 shapes, performance is no problem. You do not even need the faster algorithm described in this and the previous blog post. Tushar’s original algorithm is fast enough to update a world map in a reasonable time. Have a look at my first post on Choropleth Maps here:

    Choropleth Maps with Excel

    2. Different level of details / zooming
    You can change the workbook and VBA to display a user selected part of the region by making the relevant shapes visible or invisible. This shouldn’t be a big deal. However, if you do not zoom, you will have an ugly effect: the shapes are always in the same position on your worksheet, i.e. if you select South America, for one, you will see South America in the bottom left of the map and a lot of white space above and to the right of it.
    3. Additional information by clicking on a shape
    Have a look at this article:

    Spice up your Choropleth Maps with Excel

    The details of a region are shown in a message box after cklicking on the shape, but you could easily change this and display the metrics in a cell somewhere on your worksheet or – as you suggested – show a triangle next to the legend.

  20. Laurent Avatar
    Laurent

    Robert,
    About perf:
    actually i would need a mix about both articles 😉
    Let me explain :
    Indeed i not need improved algorithm about ww map and region maps. But i would have a secret second goal 😉 –> show points (cities) with colored semi transparent circles shapes which dynamically grows depending on data value.
    ( i know it is not choropleth maps anymore 😉 but this would enables to have 2 ways of map visualisation on the same tool)
    And in this case i will probably need your improved method (probably modifed) since there will be thousand points (6000 Max in my case). But my first problem for this would be to create a point DB with the excel x,y coordinates based on the map displayed (i know it is possible to transform long/lat in x,y, but this need maps with a known projection). If i manage to do it, i will share it.
    But anyway, i need to go by steps. And the first one is to focus on choropleth maps and be able to have 2 level of maps, where the user is able to select the map he wants (so i will start from your version to do this in order to benefit of your improvements which will probably be useful afterwards)
    Which brings to the second point: 2 level of details:
    Instead of using several maps one above each other, and using visibilty shapes property, another option would be to have each map in a separate sheet, and a main sheet with the current active map. And when the user select a map, the current map is deleted then the right map is copied from its sheet to the main sheet. I don t really see any problem on that ?

  21. Robert Avatar

    Laurant,
    as for your “secret second goal”: if you want to have a bubble chart on top of a choropleth map, you would need an Excel chart sitting perfectly aligned on top of the shapes and the fast algorithm wouldn’t help you, because it just updates the shape colors. The bubbles would be an Excel chart and they would be updated by Excel itself.
    As for the 2 level of details map: I agree, you can keep the maps on separate worksheets, but you would not have to delete and copy the map. As I suggested in my reply to your first comment, you could use the camera object to display the map selected by the user. Of course, you could also use some VBA to delete and copy the shapes, but I think the camera object would probably be the easier way to go.

  22. Laurent Avatar
    Laurent

    Thank you for your time.
    About bubbles i would prefer to dynamically create vba circle shapes. Because not sure i will be able to create an excel bubble chart which exactly fit to the map. However i will Check this.
    About camera tool i agree with you. Only drawback as you have already pointed out is that i will not be able to click on a shape. And i will probably need this functionality.
    So now i will work on all of this ! 😉
    I ll keep you informed.
    Nb: i have seen that excel 2013 and power view/power maps seems to be a great tool for map visualisation. In my case i m stuck with excel 2010, that is why i cannot use it

  23. Robert Avatar
    Robert

    Robert,
    This is phenomenal work. Also we share the same name.
    I would like to use this choropleth graph for sequential data analysis.
    I would like to use custom color selection from color brewer website on the map. How do I do so?
    Also, if I want to use less than 10 data classes from my own selected color selection how do I do so.

  24. Robert Avatar

    Robert,
    1. You can change the color scales on worksheet [control] in the named range myColorScale (C19:AC28). Simply set the fill color of the cells in one column of this range to what you would like (respectively to what colorbrewer suggests) and select this color scale in the drop down on the dashboard.
    2. Delete rows on worksheet [control] somewhere between row 19 and 28 and adjust the formulas in the remaining rows. Delete the rows on the dashboard to adjust the legend (drag the legend somewhere else first, in order to avoid a distortion of the map) and adjust the links to worksheet [control]. Finally delete the unwanted checkboxes of the legend.

  25. Daniel Avatar
    Daniel

    This is amazing work!
    I am trying to use one map to measure multiple functions and would like my color scales to adjust accordingly for example:
    Selection 1= Scales would range from 0 to 100%
    Selection 2= Scales would Range from -100% to +100%
    Selection 3= Scales would range from -5% to +5%
    Thanks again for the great spreadsheet

  26. Daniel Avatar
    Daniel

    I figured out an easy way to do this, If I base my Threshold and legend off cell C15 the Selection # I can adjust it to anything I want.

Leave a Reply

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