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 etcWant to share your content on R-bloggers? click here if you have a blog, or here if you don't.
shiny app
Highcharter is a R wrapper for Highcharts javascript based charting modules.
Rendering Choropleth Maps with Highcharts in Shiny
- To see Highcharts/Shiny interaction, we will begin by creating basic Shiny dashboard layout: It contains a single select widget and single tab for displaying map.
[code]]czo2MjE6XCJsaWJyYXJ5KHNoaW55KTxiciAvPgpsaWJyYXJ5KHNoaW55ZGFzaGJvYXJkKTxiciAvPgpsaWJyYXJ5KGhpZ2hjaGFydGV7WyYqJl19cik8YnIgLz4KbGlicmFyeShjb3VudHJ5Y29kZSk8YnIgLz4KbGlicmFyeShkcGx5cik8YnIgLz4KPGJyIC8+CnNhbGVzICZsdDstIHtbJiomXX1yZWFkLmNzdihcJ3NhbGVzcG9pbnQuY3N2XCcpPGJyIC8+CnVpJmx0Oy08YnIgLz4KPGJyIC8+CmRhc2hib2FyZFBhZ2UoPGJyIC8+CiB7WyYqJl19IGRhc2hib2FyZEhlYWRlcih0aXRsZSA9IFwiTWFwXCIpLDxiciAvPgo8YnIgLz4KICBkYXNoYm9hcmRTaWRlYmFyKDxiciAvPgogIHNpe1smKiZdfWRlYmFyTWVudSggPGJyIC8+CiAgc2VsZWN0SW5wdXQoXCd5ZWFyaWRcJyxcJ1NlbGVjdCBZZWFyXCcsY2hvaWNlcyA9IGMoMjAwMywyMDA0e1smKiZdfSwyMDA1KSxzZWxlY3RlZCA9ICAgIDIwMDMpIDxiciAvPgogICkpLDxiciAvPgo8YnIgLz4KICBkYXNoYm9hcmRCb2R5KDxiciAvPgp7WyYqJl19ICAgIHRhYkJveCh0aXRsZSA9IFwnVG95U2hvcFwnLGlkID0gXCd0YWJzZXQxXCcsd2lkdGggPSAxMiwgdGFiUGFuZWwoXCdXb3JsZC1TYWxle1smKiZdfXNcJyxoaWdoY2hhcnRPdXRwdXQoXCdjaGFydFwnLGhlaWdodCA9IFwnNTAwcHhcJykpKTxiciAvPgogICAgPGJyIC8+CiAgKTxiciAvPgopPHtbJiomXX1iciAvPgo8YnIgLz4KPGJyIC8+ClwiO3tbJiomXX0=[[/code]
- In server function, filtering and summarize data with dplyr library and create a reactive object
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.
- Passing reactive object in renderHighchart function. Customizing tooltip and sub-title content with reactive widgets.
[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.