It’s useful to quickly map a dataset of locations, and to do it in the same environment that you’re doing your data analysis. There are many quick and easy ways to map latitude and longitude markers with R, such as using RgoogleMaps, ggmaps or the maps package.
But those packages generate static images. Sometimes you need to zoom in or pan around a map for greater comprehension.
This tutorial will show you how to take a dataset with latitude and longitude columns, map it and style it — all with just a few lines of code in R.
Packages are collections of user-created R scripts, data and functions that simplify complex operations. There are thousands of them for all sorts of repetitive processes. TrendCT made a package to clean Connecticut town names. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used. There are two steps to working with packages:
- Downloading the package to your computer with
install.packages("package_name")— after which you never have to re-download it (unless you want to because there’s been an update).
- Load it to your session with
require(package_name). Every time you start up R or RStudio, you have to reload the package to your computer’s memory by calling its library/directory.
Start by downloading and installing the Leaflet package from the servers at CRAN, the Comprehensive R Archive Network. You’ll need one more package, dplyr, for some piping functions.
A note on Pipes in R
The piping operator
%>% is a feature that streamlines coding in R.
It’s easier to wrap your head around it if you think of coding grammatically. Normal coding in R is rigid declarative sentences: “Bob is 32. Nancy is 4 years younger than Bob.” Coding with the pipe operator: “Nancy is 4 years younger than Bob, who is 32.” Pipes are a comma (or a semi-colon, if you want) that lets you create one long, run-on sentence.
Making a map with a marker
Explaining the R code
leaflet()initializes the leaflet workspace
addTiles()by itself will bring in the default OpenStreetMap tiles.
Note: OpenStreetMaps is a wonderful and free open-source service. Their only stipulation for using their tiles is to be sure to credit and link to them in the map.
setView()is pretty self-explanatory but is simpler to implement.
addMarkerswith some specific parameters.
Note: The order of commands is important. A view can’t be set unless there are tiles established first.
How to put it online
If you want to share your map outside of the RStudio environment, simply click on the Save as Web Page option in Export. It will generate an HTML file that expands to the whole screen of the browser. From here, you can upload it to your server and iframe it to the appropriate height and width you want.
A note about file sizes
However, when looking at how much data is actually downloaded to load the map html, the differences aren’t as drastic.
It’s just something to keep in mind.
It’s always better in terms of efficiency and customization to code from scratch. Here’s a useful tutorial for reference.
But on a deadline, sometimes user experience is sacrificed.
Multiple locations from a CSV
- The spreadsheet ctlist.csv was saved to the variable
- The dataframe
ctwas added within the
- Then it pipes to the
addTilesfunction with a link to a set of dark map tiles. Here’s a large list of map tiles you can substitute.
- View and zoom is set the same as before.
- Then it pipes to the
addCirclesfunction, passing it the names of the latitude and longitude column in the
ctdataframe, as well as where to find the text for the pop-up information, and the parameters to set the style of the circles on the map.
Adding a legend to the map
- Pipe in
Five lines of code in R to make a complete map.
You could specify a palette of colors and it would show a range of colors if needed in the legend and map. But for now we just have one type of dot on the map, so we specify the variables. To get a good look at the legend options, type
?addLegend in the console of R.
But we’ll get into that next time when we discuss mapping with choropleths.