Spice up your Choropleth Maps with Excel

6 (+1) tips how to mitigate some of the disadvantages of Choropleth Maps in Microsoft Excel

Spiced Up Choropleth MapWe already had a couple of articles on Choropleth Maps, either using Microsoft Excel or Tableau. To be honest, I was really surprised how well these posts were received by our readers. The workbook provided for download with the first article Choropleth Maps with Excel is still in undisputed first place of all downloads here on Clearly and Simply. Thus, there was quite an avalanche of posts and comments on this topic and – despite the fact that I promised to stop posting on Choropleth Maps several times before – I announced at the end of the latest article that I am having left one ace up my sleeve regarding Choropleth Maps. Here it is and it will definitely be the last one:

Already back in September last year, Lavih sent me an email including a map template of Argentina based on Gabriel’s implementation using transparencies. With the first email Lavih asked me for an easy way to let the user change the basic fill color of the map. Over a couple of weeks we emailed back and forth and together we developed some ideas on how to improve Choropleth Map visualizations with Microsoft Excel, far beyond only changing the basic fill color.

Today’s post provides 6 (+1) tips on how to spice up your Choropleth Map visualizations using Microsoft Excel, as always including the workbooks for free download.

The challenge

At the end of the first article on Choropleth Maps here, I already mentioned a small, non-exhaustive list of disadvantages coming with this type of visualization:

  1. No visualization of development over time
  2. No information on exact values (unless you are implementing tooltips including the data)
  3. Very limited direct comparability of the regions
  4. Possible perception problems with regards to the size of regions (e.g. Rhode Island on a US map)
  5. Possible misinterpretation because the size of a region may have a greater impact on the user’s visual perception than the intensity of the fill color
  6. Requirement of real estate on a dashboard

Although you can’t do much about numbers 4 to 6, you have options to somehow mitigate at least numbers 1 to 3. The challenge of today’s post is implementing a couple of additional features helping to overcome some of the deficiencies of a Choropleth Map.

Tip 1: Increase direct comparability with an additional bar chart

Additional sorted bar chart - click to enlargeThe first tip addresses disadvantage #3 of the list above: the limited direct comparability of the regions. The idea is pretty simple: add an additional bar chart of all regions including the exact values (see left). Sorting the bar chart makes the comparison easier. A how-to tutorial on sorting a list using formulas can be found on Chandoo’s blog.

Highlighting the actual selected region (see also tip 3 below) with a red background helps to immediately identify the position of the selected province within the list of all regions. The highlighting is done with standard Excel conditional formatting.

Tip 2: Format values according to the selected KPI

If you want to let the user select from a list of different KPIs, you may face the following challenge: the displayed values of different KPIs may need different formatting, e.g. the values close to the bar chart (see tip 1 above). If you have some KPIs measured in absolute numbers and others measured as percentages, for instance, you need to change the format of the cells accordingly.

Here is one possible technique how to do this:

  • Define the desired custom format of each KPI in a cell range on the worksheet “control”
  • Detect the format string for the selected KPI from this list using the function INDEX based on the user selection
  • Add the following line to the sub “UpdateMap”:
    Range("myMetricsData").NumberFormat = Range("myMetricFormats").Value

“myMetricFormats” and “myMetricsData” are cell range names for the cell containing the required custom format string and the cell range with the data to be displayed / formatted.

That’s it. Each time the user selects a new KPI from the drop down list, the macro “UpdateMap” is called and automatically changes the format of the cells close to the bar chart.

Please be advised that I used a German version of Microsoft Excel to create the workbook posted for download (see below). If you are using an English version, you have to replace the commas by decimal points and vice versa in the cell range C5:C14 on the worksheet “control”.

Tip 3: Show the exact value of one selected region of the map

Exact value after clicking - click to enlarge Overcoming the lack of information on the exact values in a Choropleth Map (limitation number 2 mentioned above) is the next issue we will address.

You have two different options to do this:

Option number one is enabling the user to click on each shape of the map and displaying a Message Box including the name of the selected province, the name of the KPI and the value. The Message Box will stay visible until the user leaves the box via the ok button or the ESC key.

To implement this little feature you have to copy the following macro to your workbook and to assign this macro to every freeform shape of your map:

Sub SelectShape()
On Error Resume Next
UpdateMap
Range("myLastClick").Value = Application.WorksheetFunction.Match( _
ActiveSheet.Shapes(Application.Caller).Name, _
Range("myShapeNames"), 0)
MsgBox "Province: " & Range("mySelectedState").Value & vbCrLf & _
Range("mySelectedMetric").Value & ": " & _
Format(Range("mySelectedValue").Value, _
Range("myMetricFormats").Value), vbOKOnly, _
Range("mySelectedMetric").Value
Range("myLastClick").Value = 0
End Sub

After calling the sub UpdateMap, the worksheet function MATCH is used to detect and assign the number of the clicked region. Finally a Message Box is displayed including the according information of the selected province.

Exact values in screen tips - click to enlargeOption number 2 is using screen or tooltips to display the detailed information of one region when hovering over the shape with the mouse.

The basic idea is exploiting Microsoft Excel’s hyperlink functionality.  Actually the links point nowhere and clicking on the shapes won’t do anything. But Excel also allows assigning screen tips to hyperlinks and this is where we will include the information (name of province, name of KPI and value). The drawback: the hyperlink does not select a region and thus we will not see the highlighting in the bar chart (see tip 1 above).

To use this option, we need some small modifications of the sub “Update Map” in our VBA: 

Sub UpdateMap()
Dim myCell As Range
Dim myScreenTip1 As String
Dim myScreenTip2 As String
Application.ScreenUpdating = False
On Error Resume Next
For Each myCell In Range("MapShapeToTransparency").Columns(1).Cells
Sheets(1).Shapes(myCell.Value).Fill.Transparency = _
Application.WorksheetFunction.VLookup(myCell.Value, _
Range("MapShapeToTransparency"), 4, False)
myScreenTip1 = Application.WorksheetFunction.VLookup( _
myCell.Value, _
Range("MapShapeToTransparency"), 2, False)
myScreenTip2 = Application.WorksheetFunction.VLookup( _
myCell.Value, _
Range("MapShapeToTransparency"), 3, False)
Sheets(1).Shapes(myCell.Value).Hyperlink.ScreenTip = _
"Province: " & myScreenTip1 & vbNewLine & _
Range("myActualMetric").Value & ": " & _
Format(myScreenTip2, Range("myMetricFormats").Value)
Next myCell
Range("myMetricsScale").NumberFormat = _
Range("myMetricFormats").Value
Range("myMetricsData").NumberFormat = _
Range("myMetricFormats").Value
Application.ScreenUpdating = True
End Sub

You will notice 3 additional lines in the For Next statement. The first 2 of the new lines detect the information to be displayed using VLOOKUPs, the last line assigns the text to the screen tip of the hyperlink.

This works like a charm in Excel 2003 and earlier, but I hit a minor roadblock using this technique in Microsoft Excel 2007: hovering over a region consisting of different grouped freeform shapes (like Tierra del Fuego or Buenos Aires) does not display the screen tip. Even worse: right clicking on these grouped shapes makes Excel 2007 crash.

Tip 4: Add a color scale caption

Color scale caption - click to enlargeIn the previous posts on Choropleth Maps I always used a very generic color scale caption, simply indicating how to read and interpret the color grades on the map (the higher the value, the darker the color and vice versa).

Of course this is lacking context and information and we can considerably improve this by adding data ranges to the colors in the caption (see left).

The implementation is along the lines of the map itself, using rectangle shapes, assigning shape names and coloring them with VBA code. The values are calculated with rather simple formulas creating equivalent buckets.

Please be advised that there is one inaccuracy in this approach: the scale legend is discrete (20 steps of transparencies), whilst the original data uses a continuous range (i.e. assigning the exact percentage according to the data). This is a mismatch that might confuse the users. However, from my point of view, providing this color scale is definitely better than the generic caption used so far.

Tip 5: Let the user change the basic color

Color chooser dialogue - click to enlargeAgreed, this one is not really dedicated to the deficiencies of Choropleth Maps. It is rather an additional nifty interactive option to let the user change the basic color used on the dashboard. Not really necessary, but this one was the starting point of my discussion with Lavih and for the sake of completeness…

Since we are using Gabriel’s implementation with transparencies, we only need one single basic color to define the look and feel of the dashboard. If you want to provide the opportunity to conveniently switch to another look and feel, you may include the following VBA code:

Private Declare Function ChooseColor Lib "comdlg32.dll" Alias _
"ChooseColorA" (pChoosecolor As myChooseColor) As Long
Dim myCustomColors(0 To 15) As Long
Private Type myChooseColor
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As Long
flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Function ShowColor() As Long
Dim myCC As myChooseColor
myCC.lStructSize = Len(myCC)
myCC.lpCustColors = VarPtr(myCustomColors(0))
myCC.flags = 2 '0 for normal, 2 for extended
If ChooseColor(myCC) <> 0 Then
ShowColor = myCC.rgbResult
Else
ShowColor = 0
End If
End Function
Sub SelectColor()
Dim myColor As Long
Dim myCell As Range
Application.ScreenUpdating = False
On Error Resume Next
myColor = ShowColor
For Each myCell In _
Range("MapShapeToTransparency").Columns(1).Cells
Sheets(1).Shapes(myCell.Value).Fill.ForeColor.RGB = myColor
Next myCell
For Each myCell In Range("myScaleShapeNames").Columns(1).Cells
Sheets(1).Shapes(myCell.Value).Fill.ForeColor.RGB = myColor
Next myCell
' The following line is only working with Excel 2007 or higher
' ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1). _
' Format.Fill.ForeColor.RGB = myColor
' This is the work around for Excel 2003 and earlier:
' Change the color palette of the first chart fill color (number 17)
' This color has to be selected as the fill color
' of the bars in the bar chart
ActiveWorkbook.Colors(17) = myColor
Application.ScreenUpdating = True
End Sub

The macro “SelectColor” assigned to a button on top of the dashboard calls the color choose dialogue shown above. One single mouse click or inserting the desired RGB values allows a pretty convenient way of changing the basic color of the whole dashboard.

Tip 6: Add trend information of one region after clicking

Additional column chart - click to enlarge Tip #6 is supposed to mitigate problem #1 of Choropleth Maps mentioned in the introduction (see above): No visualization of development over time.

The idea is simple: since we already have a functionality of enabling the user to select one specific region on the map (see tip 3 above), we can easily add value by displaying a trend chart for this region.

The implementation is almost as simple as the idea: Create a column chart on the worksheet "control" displaying the trend of the selected region (using IF and INDEX), use a camera object on the dashboard linking to this column chart and use some additional lines of VBA to make this camera object visible after the user selected a region and invisible again after the user left the Message Box. Here is the adopted sub SelectShape:

Sub SelectShape()
On Error Resume Next
UpdateMap
Range("myLastClick").Value = Application.WorksheetFunction.Match( _
ActiveSheet.Shapes(Application.Caller).Name, _
Range("myShapeNames"), 0)
Sheets(1).Shapes("myBarChart").Visible = True
Sheets(3).ChartObjects(1).Chart.SeriesCollection(1). _
DataLabels.NumberFormat = Range("myMetricFormats").Value
Application.Calculate
MsgBox "Province: " & Range("mySelectedState").Value & vbCrLf & _
Range("mySelectedMetric").Value & ": " & _
Format(Range("mySelectedValue").Value, _
Range("myMetricFormats").Value), vbOKOnly, _
Range("mySelectedMetric").Value
Sheets(1).Shapes("myBarChart").Visible = False
Range("myLastClick").Value = 0
End Sub

Besides making the camera object visible and invisible, we have to add another line to format the data labels of the chart according to the defined custom format of the actual KPI (see also tip 2).

The result: 6 tips at a glance

These were our main 6 tips on how to improve Choropleth Map visualizations and here is an overview of what we have done so far:Spiced up Choropleth Map - click to enlargeThe bonus tip – animate your dashboard

If you are having data over time in your workbook, as it would be required for tip #6, you might want to animate the visualization over time on the dashboard.

I confess: I simply “stole” the idea and the code from Jon Peltier (again!) who has the perfect how-to tutorial (again!). Many thanks Jon (again!).

Thus, I guess there is no need to describe a how-to tutorial here. Simply visit Jon’s PTS blog and read how to do this and download the implementation with Choropleth Maps below.

The Download Links

Here are 3 different Microsoft Excel workbooks for free download:

  1. The simple version using Message Boxes (clicking)
    Download Spice up your Choropleth Map clicking (Excel 97 – 2003, 762.5K)
  2. The simple version using hyperlink screen tips (hovering)
    Download Spice up your Choropleth Map hovering (Excel 97 – 2003, 761K)
  3. The full version including trends and animation
    Download Spice up your Choropleth Map full (Excel 97 – 2003, 806K)
    All data in this workbook is made up.

Last, but not least

Muchas gracias, Lavih! Many thanks for our discussion and all the ideas arising from it.

What’s next?

This was article number 10 on Choropleth Maps on this blog. Much more than I originally planned. But that’s it now. Definitely. I finally ran out of ideas regarding Choropleth Maps. Seriously.

We will come to something completely different during the next few weeks: The upcoming posts will

  • show a way how to export Microsoft Excel dashboards to PowerPoint with ease,
  • present an example of Tableau’s new fantastic service Tableau Public and
  • start a new category of articles here on Clearly and Simply: the development and implementation of optimization algorithms using Microsoft Excel and VBA.

I hope there is something you will be interested in.

Stay tuned.

Update on Thursday, April 19, 2012

Earlier this week, I received an interesting email from Petros Chatzipantazis, one of my few but avid readers. Based on the approaches described above, Petros developed a very clever idea how to make the hyperlink tooltip available and provide an action triggered by clicking on one of the shapes at the same time. Check out Petros’ website spreadsheet1.com for the details.

Many thanks, Petros.

 

Comments

26 responses to “Spice up your Choropleth Maps with Excel”

  1. Geoff Avatar

    Fantastic work as always!
    Thanks!

  2. fabrice Avatar
    fabrice

    Killer stuff. Pushing Excel’s limit a little bit further.
    Cheers
    Fabrice

  3. Ross Avatar

    Looks nice, but the number of points on the scale is way to many, I reckon I can differentiate 4 shades of the same colour anything more, and I’m struggling to compare meaningfully. Is that just me?

  4. Robert Avatar

    Ross,
    agreed, you certainly can’t exactly identify the value bucket of a region by comparing the color of the region to the colors of the caption.
    However, the legend is only supposed to give an impression of what the color range means. Especially if you keep in mind that there is a mismatch of the continuous coloring of regions on the map and the discrete buckets of the caption (also mentioned in the post).
    The caption is just a guideline providing more context. No more, no less.

  5. Federico T Avatar
    Federico T

    I am completely new to this, but, is there a way I could get the file with the Choropleth Map for Argentina?

  6. Federico T Avatar
    Federico T

    Oops, just noticed they were linked. Sorry about that, and thanks to all involved!!

  7. fernando Avatar
    fernando

    It’s a great job my friends! does anybody knows how can i put this map into a ppt slideshow??
    thank you all

  8. Robert Avatar

    Fernando,
    thanks for your comment.
    Yes you can embed the Excel file into a PowerPoint presentation. Go to Insert|Object|Create from file, browse to the Excel file with the choropleth map and click Ok. You will see an object on your PowerPoint slide with the dashboard and the choropleth map.
    However, there are a couple of things you should be aware of:
    1. You have to activate the object on your PowerPoint Slide if you want to use the interactivity of the dashboard
    2. The interactivity is not available, if you are in the Slide Show mode
    3. Since you are embedding the object into PowerPoint, changes in the original Excel file will not be updated in the PowerPoint presentation. You have to insert the file again to update
    4. Embedding the Excel file will considerably increase the size of your PowerPoint file.
    I hope this will answer your question.

  9. Srivatsa Avatar
    Srivatsa

    Hi The Excel tempaltes are good,
    Can i have the same for UK as i am working on project need to show the UK retail set up

  10. Robert Avatar

    Srivatsa,
    I do not have this template available for a map of the United Kingdom, but you can easily create your own map after reading some of my previous articles:
    The basic concept:
    Choropleth Maps with Excel
    A link to a Choropleth Map template for England:
    Choropleth Map Templates elsewhere
    How to create your own Choropleth Map:
    Build your own Choropleth Maps with Excel
    And last, but not least the article above, of course.
    These articles and workbooks should give you all the information and inputs you need to create your own interactive Choropleth Map of the UK.

  11. Michael Avatar
    Michael

    This is a great article and really enhances the previous choropleth guides! I am running into a slight problem, however (please note I’m using Excel 2010 so that might be the issue).
    I downloaded the hover version “choropleth_map_hover.xls” and replaced the map and also updated the control and data tabs. Everything is working as it should except for the hover. I kept the Argentina Map in there as well for the time being, and oddly enough that still works (even though I think I removed all references to those shape names on the Data and Control tabs).
    What am I missing? Thanks!

  12. Michael Avatar
    Michael

    I found the answer. I to hyperlink each shape I added to cell A1 in the first sheet. After doing that and running the Macro UpdateMap again everything works perfect! Thanks again!

  13. Robert Avatar

    Michael,
    thanks for your comment and the appreciation.
    The updating of the hyperlink screen tips is done in the VBA sub UpdateMap (the For Each Next statement). This statement loops through the range name “MapShapeToTransparency” which refers to the range C21 to F44 in the original workbook. The first column of this range contains the shape names. The code within the loop changes the screen tips of all those shapes. I would assume you still have the names of the Argentinian provinces there? You have to copy the shape names of your map to this range.
    If this does not fix your problem, you can send me your workbook by email and I will have a look.

  14. Depaul05 Avatar

    Robert,
    First, thanks for all this excellent work. Just fantastic, and the templates have made a world of difference.
    Everything transfers to different maps easily, however the ‘Info by Click’ is not working. Like I said, everything else works, but I can’t click new map shapes for pop info and corresponding graph showing over-time information.
    Any guess on what I may be missing? I’m happy to share my excel sheet.

  15. Robert Avatar

    Jason,
    thanks for your comment and your very kind words. If you add new shapes to the map, you have to assign the macro “Select Shape” to the new shape (right click | assign macro). Of course you have to adjust the names of the shapes etc. on the Control worksheet first.
    If this doesn’t solve your issue, you can send me your map by email (email-link see at the top of the blog) and I will have a look.

  16. JanDeDe Avatar
    JanDeDe

    Dear Robert,
    I’ve been trying to find good information on making cloropleth maps already for quite some time now. I guess I never expected being able to do this with Excel (and I also wasn’t aware of the correct name).
    I’m so happy I found your great blog!! Your posts and the excel files are truly awesome and help me further a great deal.
    Therefore a big thank you from Brussels!!!
    Best regards,
    Jan

  17. Robert Avatar
    Robert

    Hi I could do with some help on this. Could you please reply to me via emai.

  18. Rachel Avatar
    Rachel

    Hi. Have only just stumbled upon your site. Amazing work and information. Thank you! While working on the “Spice Up Your Chloropleth Maps” downloads on my 64-bit system, I received a compile error stating that the code must be updated, more specifically to “review and update the Declare statements and then mark them with the PtrSafe attribute.” Can you please advise on the best way to go about that (well, aside from completing the work on my 32-bit system at the office). Thank you.

  19. Robert Avatar

    Rachel,
    you have to update the declaration of the function ChooseColor and the Private Type myChooseColor at the top of the module in order to get the code working on a 64Bit Office installation.
    The following threat at StackOverflow explains how to do this:
    http://stackoverflow.com/questions/5724396/excel-64-bit-and-comdlg32-dll-custom-colours
    Please be advised that this is only necessary for the color picking feature. If you do not need this part of the code, simply delete the code and the command button on the sheet.

  20. Brenda Avatar
    Brenda

    Very good Robert all crack in excel. I am working your model with 4 colors 1: No data (white) 2: Low (low color) 3: Medium (moderate color) and 4: High (intense color), but I do not paint the shapes as it has to be according to the established ranges you can help me I would appreciate it.

  21. Robert Avatar

    Brenda,
    I am sorry, I do not understand your question. What exactly do you mean by “I do not paint the shapes as it has to be according to the established ranges”? Can you send your workbook by email or post it somewhere for download?

  22. Brenda Avatar
    Brenda

    Thank you, friend Robert, I already sent it to the email that shows at the top.

  23. Robert Avatar

    Brenda,
    I already sent you the solution by email yesterday.

  24. Brenda Avatar
    Brenda

    Robert,
    Thank you for your attention but I have not received any mail yet I will be waiting for you by email: brendazapataruiz501@gmail.com

  25. Robert Avatar

    Brenda,
    I just sent it again. Please check your inbox and maybe also your spam folder.

  26. Brenda Avatar
    Brenda

    Thank you Robert,
    I already received the file I was right it was like spam, I thank you very much and your excellent support to the users that you offer them with your excellent knowledge.
    regards

Leave a Reply to Depaul05 Cancel reply

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