Main Content

Create Choropleth Map from Table Data

This example shows how to create a choropleth map from spatial and attribute data contained in tables. A choropleth map indicates the values of numeric attributes within spatial regions by using colors along a gradient. Choropleth maps are commonly used to illustrate population density within specified regions.

To create a classification map, where discrete colors indicate the values of classified or categorical attributes, see Create Classification Map from Table Data.

Read Data into Tables

Read a comma-separated text file into a table. The file contains farmland statistics for each US state and the District of Columbia, based on the National Agricultural Statistics Service (NASS) Farms and Land in Farms 2019 Summary. For more information about the statistics, see [1]. Display the first row of the table.

farms = readtable("farmland.txt","TextType","string");
ans=1×4 table
    StateName    NumberOfFarms    AcresInFarms    AverageFarmSize
    _________    _____________    ____________    _______________

    "Alabama"        38800            8300              214      

To create a map from the table, you also need spatial information. Read a shapefile containing polygon shapes for each of the US states and the District of Columbia into a geospatial table. A geospatial table has a Shape variable containing shape information for each row. Display the first row of the geospatial table.

states = readgeotable("usastatelo.shp");
ans=1×5 table
       Shape          Name       LabelLat    LabelLon    PopDens2000
    ____________    _________    ________    ________    ___________

    geopolyshape    "Alabama"     32.283     -86.921        87.6    

The Shape variable of the table contains geopolyshape objects, which are polygons in geographic coordinates. Display the properties of the geopolyshape object in the first row.

ans = 
  geopolyshape with properties:

              NumRegions: 1
                NumHoles: 0
                Geometry: "polygon"
    CoordinateSystemType: "geographic"
           GeographicCRS: [1×1 geocrs]

Join Tables

The table of farmland statistics identifies the District of Columbia as Washington DC, which does not match the corresponding row of the table of polygon shapes. Change the name in the table of farmland statistics so that it matches the name in the table of polygon shapes.

dcRow = farms.StateName == "Washington DC";
farms.StateName(dcRow) = "District of Columbia";

Join the tables, using the state name variables Name and StateName as the key variables. Display the first row of the joined table.

statesFarms = outerjoin(states,farms,"LeftKey","Name","RightKey","StateName");
ans=1×9 table
       Shape          Name       LabelLat    LabelLon    PopDens2000    StateName    NumberOfFarms    AcresInFarms    AverageFarmSize
    ____________    _________    ________    ________    ___________    _________    _____________    ____________    _______________

    geopolyshape    "Alabama"     32.283     -86.921        87.6        "Alabama"        38800            8300              214      

For more information about how to combine vector data using table joins, see Combine Vector Data Using Table Joins.

Create Choropleth Map

Create a choropleth map that shows the average farm size for each state in the conterminous US.

rows = statesFarms.Name ~= "Hawaii" & statesFarms.Name ~= "Alaska";
statesFarmsConus = statesFarms(rows,:);

Change the basemap. Add a title and a labeled colorbar.

geobasemap darkwater
title("Average Farm Size Per US State in 2019")
cb = colorbar;
cb.Label.String = "Size in Acres";

Change the colormap so that, as the average farm sizes increase, the colors of the states transition from yellow to green.

cmap = flip(summer);

Export Map

Export the map to a PNG file.

gx = gca;


[1] National Agricultural Statistics Service. “Number of Farms, Land in Farms, and Average Farm Size — States and United States: 2018–2019.” In Farms and Land in Farms 2019 Summary, 6. USDA, February 2020.

See Also



Related Topics