This article describes how to label states on a Geographic Map visualization. While this specific example labels US states, the same process can be modified to add labels on any kind of map based on latitude and longitude coordinates.
This examples goes from a Geographic Map of the US without labels:
To a map with labels on each state:
Requirements
- A Displayr document with a Geographic Map using the leaflet map package (see How to Create a Geographic Map).
- A reference table containing 3 or 4 columns:
- Entity names that can be matched to your table of data (i.e. full state names)
- Latitude* for where each label will go.
- Longitude* for where each label will go.
- [Optional] Modified label if wanted (i.e. the state abbreviation)
* Many times you can find lists of commonly referenced geographic points and their coordinates through googling. This example uses population data from the US census imported into Displayr and a table of state names/abbreviations with their corresponding latitude and longitude coordinates that can be downloaded here.
Method
At a high-level, the method uses a reference table of labels and their lat/long coordinates to add individual labels to the map after it's initially been created.
- Create the reference table with the labels and coordinates. Our example includes the state, lat, long, and state abbreviation. Click the Table down arrow > Paste or Enter Table.
- Click on the Paste or type data button.
- Copy the data in this excel file for state labels and their lat/long and paste it in the window. Click OK. Your label reference table should look like so:
- Change the General > GENERAL > Name of this table to states and click Hide. This name will be used in the R code below.
- Now let's use the table to modify the map. Click on the map, ensure Chart > Appearance > Map package is leaflet.
- Go to the Data > R CODE box and add a line to the bottom.
- You'll be prompted with a message that says if you edit the R code we don't guarantee it will work with your custom code. Click Yes.
-
Paste the following code on the new empty line at the bottom of the R CODE and change chart in
chart$htmlwidget
(bolded below) to the name of the chart made by the visualization (check General > Name):#load the leaflet functions
library(leaflet)
#pull off the htmlwidget to edit
#change chart to the name of this specific map
new.chart = chart$htmlwidget
#bring the states labeling info into R
#change states to the General > Name of your reference table
states = as.data.frame(states)
#loop through each row/state in the reference table
#and use a function to add in the label
for (j in 1:nrow(states)) {
#use the addLabelOnlyMarkers function to add a label to the new chart
new.chart = addLabelOnlyMarkers(map = new.chart, #map= will be the name of the new chart to add the label to
lng = as.numeric(states[j, "long"]), #longitude for label
lat = as.numeric(states[j, "lat"]), #latitude for label
label = states[j, "abbr"], #column in table of label to use, change abbr to state to use full name
labelOptions = labelOptions(noHide = T, opacity = 0.75, textsize = "8px", direction = "center", textOnly = TRUE)) #other formatting options for label
}
#call the new chart with all the labels appended
new.chart - Click Calculate.
- [Optional]: Edit the lat/long columns in your states table to tweak the placement of labels in your map.
- [Optional]: You can change the labels in the abbr column to something entirely different if you require different labels.
Next
How to Set Initial Zoom and Position of Geographic Maps
How to Create a Geographic Map With Custom Regions
How to Create Small Multiples (Facet, or Panel) Visualizations