U.S. Counties Choropleth

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 and 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:

In the topo.json I have

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?

So I took a look at the Vega docs 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 that demonstrates a basic structure for computing and using centroids with Vega.

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

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.

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

1 Like

There is also:

3 Likes

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:

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 = "")


11 Likes

Thanks for posting this, it should me out!