This article describes how to create a geographic region that differs from built-in regions in a standard geographic map. For example, you may want to include/exclude a state from a region or create a "Midwest" region on a geographic map of the United States.
In the example below, we start with a standard geographic map that shows the percentage of respondents in a handful of states:
and then we'll create custom regions, which will show the combined percentage for each:
Requirements
- Geographic map visualizations require data that has been combined into categories or entities arranged in a table with two columns:
- Column 1: a list of geographic entities. The entities may be countries, continents, states, regions, or zip codes, see the technical reference for what entities are supported for each area.
- Column 2: a data value for each geographic entity.
- You can also use one of Displayr's built-in tools to combine more granular data into one of the supported entities. See How to Automatically Combine Categories - By Geography.
- You will also want some familiarity with R code since custom code is involved, see How to Learn R for a list of resources.
Method
In this example, we will use sample data to combine a subset of US states into regions and then combine their results into a geographic map.
Step 1: Setup the custom regions
1. Create a table for the geographic entity you want to aggregate into your custom region. In this example, it will be the State variable. From the Data Sources tree, drag the State variable onto the page to create a summary table. The Name of this table is table.State.
2. Create a copy of the variable, which you will use to group the entities into regions. In this example, right-click the "State" variable in the Data Sources tree, and select Duplicate.
3. Right-click the duplicated State variable and select Rename.
4. Update the name of this variable to "Regions".
5. Now, you can create the regions by combining the specific entities on a table. Drag the "Regions" variable onto the page to create a summary table.
6. Select the states that you want to combine into regions, then right-click and select Combine.
7. Right-click and Rename the rows to give them region labels. In this example, I've combined California and Oregon into "West", Iowa, Kansas, and Missouri into "Midwest", and renamed the Texas row to "South". The name of this table is table.Regions.
Step 2: Create a table of the entities using their region value
In order for the entities to look like a custom region, they need to have the same value (and thus same color). Next, we will create a custom calculation to map the regional value to all of the state within the region for plotting.
1. From the toolbar, click Calculation > Custom Code.
2. Click on the page to place the custom calculation.
3. In the R Code editor at the top of the page, paste the code below and modify the variables and tables used to point to those used in your example. This code uses the State and Region variables to get a list of unique pairs. Then merges in the region numbers to the pairs and only keeps states listed in the original State table.
####get the data
##Deduplicate the State and Custom Region pairs from the variables in the data set to use as a lookup table
##(this is so data will update automatically if custom regions change in the future)
# combine state and custom region variables (by name) as a lookup table of unique combinations
lookup = unique(data.frame(State,Regions))
##pull in the original summary tables for each
# table of states
states = table.State
#table of custom regions
regions = table.Regions
####give all states in the custom region the % for the custom region so they are colored the same
#turn region table into a data.frame for merging
regions = data.frame("%"=regions,check.names=F)
#merge in the region % numbers into the lookup table listing the state-region pairs
percent = merge(lookup,regions,by.x="Regions", by.y="row.names",all.x=T)
####return final table
#only keep the states that are listed in the original state table
customregions = percent[percent$State %in% rownames(states),c("State","%")]
4. Click Calculate.
Your table should look like so where each state in the region has the same regional value:
Step 3: Create a geographic map
Use the final table from Step 2 above as the input table to your geographic map.
1. Select the table from Step 2, and from the toolbar, click Visualization > Geographic Maps > Geographic Map.
2. Optionally, you can update Chart > Appearance > Map package to leaflet to include only the states/regions in the map, rather than all 50 states:
Note: When you hover over any of the states in the visualization, it will show the combined percentages instead of each state's percentage.
3. Use the Chart tab to further customize your map. Options are explained in our technical documentation Visualization - Geographic Map - Geographic Map.
This is just an example of what you can do to create custom regions on a geographic map. Your geographies and desired output will likely differ, but you can use the same steps above with any entity as long as it can be plotted by a geographic map output.
Next
How to Create a Geographic Map
How to Access an Exhaustive List of Geographic Entities Available for Geographic Maps