U.S. Counties Choropleth

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