Introduction

This code through explores using the Leaflet package to create interactive bubble maps.


Content Overview

Specifically, I will explain and demonstrate how using the “leaflet package”, we can create bubble maps with markers coresponding to geographical coordinates, make them display data interactively, and vary their size and color according to different variables. Below, I will also show how to import Excel spreadsheets into R using the “readxl” package and convert them into data sets.


Why You Should Care

This topic is valuable because it shows those interested in data science how they can create interactive maps that display different kinds of data for specific geographical locations. Interactive maps such as the one used as an example below can be useful in a variety of fields.


Learning Objectives

Specifically, you will learn how to do the following:

  1. Import data from Excel spreadsheets using the “readxl” package

  2. Load a map of the world using “leaflet” and the “addTiles” function

  3. Add markers for geographical coordinates using the “addCircleMarkers” function

  4. Make those geographical markers interactive using the “popup_info”, “mutate”, and “paste” functions

  5. Make the markers vary in color according to a variable using the “colors”, “pal”, and “color” functions

  6. Make the markers vary in size according to a variable using the “addCircles” and “radius” functions



Organization

Here, we will simply show how to import data from Excel spreadsheets and add markers to a map according to geographical coordinates under “Basic Example” for Learning Objectives one through three. Under “Advanced Examples”, we will then add instructions for Learning Objectives four to six. Further resources for learning to use “leaflet” and other ways of visualizing data with R, as well as the Works Cited section can be found at the end of this document.


Basic Examples


Importing the Data Set

Before we can create our map, we of course need our data set that it will be based off of. I have created my own data set for this code through that contains data on Japan’s thirty largest cities. It has nine variables as follows:

  1. “city” (the city name)

  2. “prefecture” (the Japanese “state” that the city is located in)

  3. “japanese” (the Japanese name)

  4. “population” (total population)

  5. “area” (the city’s area)

  6. “density” (population density)

  7. “dialect” (the dialect of Japanese native to the region that the city is located in)

  8. “lat” (latitude)

  9. “long” (longitude)

Using the “readxl” package, simply call the directory location of the Excel file in my computer and then assign it to “jp”. This creates the data set in R Studio’s Global Environment (Data to Fish, 2020). The sources from which I have gathered the data are listed under “Works Cited” at the end of this code through.


Loading the World Map

A basic example of a leaflet map shows how we can create circle markers for the thirty largest Japanese cities.

We load the leaflet package and then use “addtiles()” to pull up a world map (A&G Statworks, 2019).


Adding Geographical Markers

Next, we use the “addCircleMarkers” function to mark where the cities are on the map, using the latitude and longitude coordinates provided in the data set. Here, we have to make “data” equal the data set you are using. “lat” and “lng” represent latitude and longitude, respectively. Therefore, we assign them the “lat” and “long” values from the data set. The tilde is a DPLYR shortcut that replaces having to type “[data set]$”. Using DPLYR makes organizing the code easier and will allow us to use the mutate function later. I have also chosen to set “radius” to three in order to control the size of the markers and “opacity” to a value of five in order to enhance readability (A&G Statworks, 2019).



Advanced Examples


Adding Interactivity

Next, we can make the map interactive by using the “popup” function. However, we first need to mutate the data set in order to make “popup” display what we want it to. Using the DPLYR “mutate” function and leaflet “popup_info” function, we can designate what information appears when the user clicks on one of the circle markers. This is done with “popup_info” and the base R “paste” function, indicating what text and data points will be displayed, using breaks (“
”) in order to separate lines.

We then implement this in the “addCircleMarkers” function by adding “popup = ~popup_info” (A&G Statworks, 2019).


Varying Marker Color by Variable

Additionally, we can vary the color of the markers by population density using the “colors”, “pal”, “colorFactor”, and “color” functions. First we give “colors” a collocation of blue and red, using “c(”blue“,”red“)”. Next, we give “pal”(pallette) the “colorFactor” function defined by our recently collocated “colors” in relation to population density (“colorFactor(colors, jp$density)”). Finally, we plug this into the primary map function with “color = ~pal(density)” (A&G Statworks, 2019).


Varying Marker Size by Variable

Last of all, we can vary the size of the markers according to the population of each city. The method for doing this is quite simple, as we just make “radius” equal to the population and replace “addCircleMarkers” with “addCircles”. However, given that many Japanese cities are so heavily populated, we have to divide “populaton” by 2,000 in order to maintain readability (It might be worth noting that without implementing that last step, the Tokyo circle ends up swallowing most of East Asia) (Stack Exchange, 2016).



Further Resources

Learn more about Leaflet and data visualization in R with the following:




Works Cited

The data set used was created from the following sources:



This code through references and cites the following sources: