# U.S. Counties Choropleth

**URL:** <https://discourse.julialang.org/t/u-s-counties-choropleth/20845>\
**Category:** Visualization\
**Tags:** shapefile\
**Created:** [February 16, 2019, 4:05am UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845 "2019-02-16T04:05:16Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [February 21, 2019, 8:01am UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/21 "2019-02-21T08:01:09Z")

</div>

Thanks. That helped.

But that topo-file did not contain iso\_a3 codes for the countries. I managed to create a custom one which contains that data with the help of [geojson-maps.ash.ms](https://geojson-maps.ash.ms/) and [mapshaper.org](https://mapshaper.org/).

My next task is to plot the country codes in the countries. The information is in the topology.json but I do not know at this stage to use that information.

Otherwise I will have to try and overlay this map on a base map which contains the country names. Will have to find out how to do that.

So far I have this output:

 ![africa_output](https://global.discourse-cdn.com/julialang/original/3X/2/3/23eb24cd07bf1edfd0e826061325407424bc3556.png)

In the topo.json I have

```julia
julia> a = JSON.parsefile("custom.topo.json")
Dict{String,Any} with 4 entries:
  "arcs" => Any[Any[Any[131242, 106085], Any[-78, -99], Any[-31, -51], Any[-89, -219], Any[-19, -76], Any[…
  "objects" => Dict{String,Any}("custom"=>Dict{String,Any}("geometries"=>Any[Dict{String,Any}("arcs"=>Any[Any…
  "type" => "Topology"
  "transform" => Dict{String,Any}("translate"=>Any[-25.3604, -46.9658],"scale"=>Any[0.000426044, 0.000420089])

julia> a["objects"]["custom"]["geometries"][1]["properties"]["iso_a3"]
"BDI"

```

How do I get BDI to be printed in the correct country?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [February 21, 2019, 12:53pm UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/22 "2019-02-21T12:53:42Z")

</div>

So I took a look at the [Vega docs](https://vega.github.io/vega/docs/expressions/#geo-functions) and there is a `getCentroid` function which would allow you to compute the central point of each shape (country). This could then be used for placement of text marks that contain whatever info you want. I played with it for about 20 minutes and was unable to get it working, so please post your resolution if you find one!

There is a github post [here](https://github.com/vega/vega-lite/issues/3829) that demonstrates a basic structure for computing and using centroids with Vega.

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [February 21, 2019, 2:42pm UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/23 "2019-02-21T14:42:07Z")

</div>

I will only be able to experiment further on Monday. Thanks for the information.

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [February 25, 2019, 7:51am UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/24 "2019-02-25T07:51:05Z")

</div>

I am not winning. In the example the data sources are used in an inverse manner from what I have done with the Africa data and I have been unsuccessful in my efforts to do it the other way round. I simply do not really understand the background processes well enough and the documentation (and lack of examples) does not help.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [February 25, 2019, 12:16pm UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/25 "2019-02-25T12:16:51Z")

</div>

I opened an issue on the VegaLite.jl GitHub repo, so hopefully the package authors can work this issue out.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [February 27, 2019, 5:25pm UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/26 "2019-02-27T17:25:18Z")

</div>

There is also:

> **[GitHub - Wedg/PlotShapefiles.jl: A package to plot shapefiles with Julia.](https://github.com/Wedg/PlotShapefiles.jl)**
>
> A package to plot shapefiles with Julia. Contribute to Wedg/PlotShapefiles.jl development by creating an account on GitHub.

> **[GitHub - penntaylor/ChoroplethMaps.jl: Easy thematic mapping using Gadfly and...](https://github.com/penntaylor/ChoroplethMaps.jl)**
>
> Easy thematic mapping using Gadfly and datasets such as US Census Bureau's TIGER data. - GitHub - penntaylor/ChoroplethMaps.jl: Easy thematic mapping using Gadfly and datasets such as US Census...

> **[GitHub - GenericMappingTools/GMT.jl: Generic Mapping Tools Library Wrapper...](https://github.com/GenericMappingTools/GMT.jl)**
>
> Generic Mapping Tools Library Wrapper for Julia. Contribute to GenericMappingTools/GMT.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![seospinale](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seospinale/32/18013_2.png) [@seospinale](https://discourse.julialang.org/u/seospinale)\
**Post date:** [September 18, 2020, 3:57am UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/27 "2020-09-18T03:57:55Z")

</div>

I know it’s been a while since this question was posted, but it took me a while to plot a choropleth from a DataFrame so I’ll post the way I do it by using Shapefile.jl and Plots.jl

I hope this could help someone out there. The resulting map without working so much on the aesthetics is the following:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f10fbdcdeaf52b77b240a2651f76cd69463abb5.png)

```julia
using Plots
using Shapefile
using DataFrames
using Colors

#building a custom categorical colorbar
colorbar = cgrad(["#FFFFFF", "#ffffcc", "#a1dab4", "#41b6c4", "#253494"], categorical = true)

#loading the shapefile 
shape = Shapefile.Table("AMVA/AreaMetropol.shp");

#Building the DataFrame from the shapefile polygons and names
data = DataFrame("zone" => shape.Name, "geometry" => Shapefile.shapes(shape));

#The trick is to transform each of the polygons to Julia Shapes
plot_shapes = []

#so each polygon is iterated to extect its vertices x and y coordinates
for poly in data.geometry
    
    vertices_x = [point.x for point in poly.points]
    vertices_y = [point.y for point in poly.points]
    
    #Then those x and y coordinates are transformed into Shapes
    push!(plot_shapes, Shape(vertices_x, vertices_y))
    
end

#Add the new Shapes to the DataFrame
data[:, "plots_shapes"] .= plot_shapes;

#Assign the values for each feature to the DataFrame, in this case random integers are used
data[:, "value"] = rand(0:10, 10);

#Finally the array of shapes are given to the plot function and its values are used in the fill_z argument
plot(data.plots_shapes, fill_z = data.value, size = (600, 650), lw = .1, xtickfontsize = 15, 
    ytickfontsize = 15, titlefontsize = 20, xlabel = "Longitud [°]", ylabel = "Latitud [°]", guidefontsize = 15, 
    color = colorbar, vmin = 1, dpi = 200, label = "")

```

---

<div class="post-metadata">

**Author:** ![lwhitefox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lwhitefox/32/4363_2.png) [@lwhitefox](https://discourse.julialang.org/u/lwhitefox)\
**Post date:** [October 13, 2020, 1:43pm UTC](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/28 "2020-10-13T13:43:40Z")

</div>

Thanks for posting this, it should me out!

[Previous page](https://discourse.julialang.org/t/u-s-counties-choropleth/20845.md?page=1)
