This article describes how to go from a general geographic map:
To a version of your geographic map with an initial zoom and position applied:
Requirements
A geographic map visualization.
Please note this requires the Data Stories module or a Displayr license.
Method
- Select the map visualization on a Page.
- From the object inspector, go to Chart > APPEARANCE > Map package > leaflet.
- OPTIONAL: If you have multiple columns of data in the input table that was used to create the original geographic map visualization, you can indicate which columns to include by updating the COLUMN MANIPULATIONS settings. For example, if I have 3 columns, and only want to show the first column, go to Data > COLUMN MANIPULATIONS > Number of columns from left to show and set to "1".
- Show background map for context by ticking Chart > APPEARANCE > Background map.
- Set the color of missing regions to transparent by selecting Chart > APPEARANCE > Color of NA values > More colors > No Fill.
- Set the shading so that darker red means a lower population using Chart > DATA SERIES > Color palette > Reds, dark to light.
- Access the R code underlying the map by going to Data > R CODE.
- At the bottom of the R code, load in the leaflet R package with library(leaflet) to get access to the full set of functions to customize the leaflet map.
# load leaflet library
library(leaflet)
9. Add another line after that, to set the initial position and zoom of the map using the setView function from the leaflet R package. The syntax is as follows:
#Set the zoom to center on a particular coordinate on the map
#Replace YourVisualizationName with the name of your visualization above near "#Creating the chart or table" i.e. viz.1, chart.2, etc
#Replace TheLongitude with the longitude coordinate for the center
#Replace TheLatitude with the latitude coordinate for the center
#Replace TheZoomLevel with the desired zoom from 0 (the world) to 18 (street-level).
YourVisualizationName <- setView(map = YourVisualizationName$htmlwidget, lng = TheLongitude, lat = TheLatitude, zoom = TheZoomLevel)
For example, my code is:
# load leaflet library
library(leaflet)
# set chart and add longitude, latitude, and zoom levels
chart.50 <- setView(map = chart.50$htmlwidget, lng = -87.6298, lat = 41.8781, zoom = 9)
After your map re-calculates, it will now automatically be zoomed in to your desired area.
OPTIONAL:
- You can tweak your initial zoom level by using decimals, such as 1.5.
- To find the latitude and longitude to center your map you can:
- Click on a blank spot on a Google map. A small window at the bottom of the screen will display the coordinates.
- Google "YourCity/State/Country lat long", and use coordinates from the results.
- Use a website such as https://www.latlong.net/ to find the coordinates.
Next
How to Create a Geographic Map