Site icon R-bloggers

Choropleth maps with Highcharts and Shiny

[This article was first published on R-posts.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
We use Choropleth maps to show differences in colors or shading of pre-defined regions like states or countries, which correspond to differences in quantitative values like total rainfall, average temperature, economic indicators etc



In our case we will use sales of a toy making company, as quantitative value, in different countries around the world. See example with this shiny app



Highcharter is a R wrapper for Highcharts javascript based charting  modules.

Rendering Choropleth Maps with Highcharts in Shiny


[code]]czo2MjE6XCJsaWJyYXJ5KHNoaW55KTxiciAvPgpsaWJyYXJ5KHNoaW55ZGFzaGJvYXJkKTxiciAvPgpsaWJyYXJ5KGhpZ2hjaGFydGV7WyYqJl19cik8YnIgLz4KbGlicmFyeShjb3VudHJ5Y29kZSk8YnIgLz4KbGlicmFyeShkcGx5cik8YnIgLz4KPGJyIC8+CnNhbGVzICZsdDstIHtbJiomXX1yZWFkLmNzdihcJ3NhbGVzcG9pbnQuY3N2XCcpPGJyIC8+CnVpJmx0Oy08YnIgLz4KPGJyIC8+CmRhc2hib2FyZFBhZ2UoPGJyIC8+CiB7WyYqJl19IGRhc2hib2FyZEhlYWRlcih0aXRsZSA9IFwiTWFwXCIpLDxiciAvPgo8YnIgLz4KICBkYXNoYm9hcmRTaWRlYmFyKDxiciAvPgogIHNpe1smKiZdfWRlYmFyTWVudSggPGJyIC8+CiAgc2VsZWN0SW5wdXQoXCd5ZWFyaWRcJyxcJ1NlbGVjdCBZZWFyXCcsY2hvaWNlcyA9IGMoMjAwMywyMDA0e1smKiZdfSwyMDA1KSxzZWxlY3RlZCA9ICAgIDIwMDMpIDxiciAvPgogICkpLDxiciAvPgo8YnIgLz4KICBkYXNoYm9hcmRCb2R5KDxiciAvPgp7WyYqJl19ICAgIHRhYkJveCh0aXRsZSA9IFwnVG95U2hvcFwnLGlkID0gXCd0YWJzZXQxXCcsd2lkdGggPSAxMiwgdGFiUGFuZWwoXCdXb3JsZC1TYWxle1smKiZdfXNcJyxoaWdoY2hhcnRPdXRwdXQoXCdjaGFydFwnLGhlaWdodCA9IFwnNTAwcHhcJykpKTxiciAvPgogICAgPGJyIC8+CiAgKTxiciAvPgopPHtbJiomXX1iciAvPgo8YnIgLz4KPGJyIC8+ClwiO3tbJiomXX0=[[/code]
[code]]czozOTI6XCJzZXJ2ZXIgJmx0Oy0gZnVuY3Rpb24oaW5wdXQsIG91dHB1dCwgc2Vzc2lvbil7PGJyIC8+CiAgPGJyIC8+CiAgdG90YWx7WyYqJl19ICZsdDstIHJlYWN0aXZlKDxiciAvPgogICAgezxiciAvPgogICAgc2FsZXMgJSZndDslPGJyIC8+CiAgICBmaWx0ZXIoWUVBUl9JRHtbJiomXX0gPT0gYXMubnVtZXJpYyhpbnB1dCR5ZWFyaWQpKSAlJmd0OyUgPGJyIC8+CiAgICBncm91cF9ieShDT1VOVFJZKSAlJmd0OyU8YnIge1smKiZdfS8+CiAgICBzdW1tYXJpemUoXCJUT1RBTF9TQUxFU1wiID0gYXMuaW50ZWdlcihzdW0oU0FMRVMpKSkgJSZndDslIDxiciAvPgogICAgbXtbJiomXX11dGF0ZShpc28zID0gY291bnRyeWNvZGUoQ09VTlRSWSxcImNvdW50cnkubmFtZVwiLFwiaXNvM2NcIikpPGJyIC8+CiAgICB9PGJyIC8+CntbJiomXX0gICAgKTxiciAvPgo8YnIgLz4KXCI7e1smKiZdfQ==[[/code]
Here we used library countrycode to convert long country names into one of many coding schemes. Adding new column iso3 in the summarized data with mutate function.


[code]]czo4NDM6XCJvdXRwdXQkY2hhcnQgJmx0Oy0gcmVuZGVySGlnaGNoYXJ0KGhpZ2hjaGFydCh0eXBlID0gXCJtYXBcIikgJSZndDslIDxicntbJiomXX0gLz4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPHN0cm9uZz5oY19hZGRfc2VyaWVzX21hcChtYXAgPSB3b3Jse1smKiZdfWRnZW9qc29uLCBkZiA9IHRvdGFsKCksIHZhbHVlID0gXCJUT1RBTF9TQUxFU1wiLCBqb2luQnkgPSBcImlzbzNcIik8L3N0cm9uZz4gJSZne1smKiZdfXQ7JSA8YnIgLz4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaGNfY29sb3JBeGlzKHN0b3BzID0gY29sb3Jfc3R7WyYqJl19b3BzKCkpICUmZ3Q7JSA8YnIgLz4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgPHN0cm9uZz5oY190b29sdGlwKHtbJiomXX11c2VIVE1MPVRSVUUsaGVhZGVyRm9ybWF0PVwnXCcscG9pbnRGb3JtYXQgPSBwYXN0ZTAoaW5wdXQkeWVhcmlkLFwnICB7cG9pbnQuQ09Ve1smKiZdfU5UUll9IFNhbGVzIDoge3BvaW50LlRPVEFMX1NBTEVTfSBcJykpPC9zdHJvbmc+ICUmZ3Q7JSA8YnIgLz4KICAgICAgICAgICAgICAge1smKiZdfSAgICAgICAgICAgICAgICAgICAgIGhjX3RpdGxlKHRleHQgPSBcJ0dsb2JhbCBTYWxlc1wnKSAlJmd0OyUgPGJyIC8+CiAgICAgICAgIHtbJiomXX0gICAgICAgICAgICAgICAgICAgICAgICAgICBoY19zdWJ0aXRsZSh0ZXh0ID0gcGFzdGUwKFwnWWVhcjogXCcsaW5wdXQkeWVhcmlkKSl7WyYqJl19ICUmZ3Q7JSA8YnIgLz4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaGNfZXhwb3J0aW5nKGVuYWJsZWQgPSBUUntbJiomXX1VRSxmaWxlbmFtZSA9IFwnY3VzdG9tXCcpPGJyIC8+CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICkgPGJyIC8+Cn17WyYqJl19PGJyIC8+CjxiciAvPgpcIjt7WyYqJl19[[/code]

Dataset and shiny R file can be downloaded from here

To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.