How Do I Add a Colorbar to a Choropleth Map?

Hi all,

I have a choropleth plot that I have made using the following code snippet:

choropleth = plot(states.geometry, axis = ([], false); fillcolor = permutedims(random_values))

It comes out looking like this:

image

How do I add a colorbar to this graphic?

I saw this stack exchange post from a couple months ago so I see that it is still an open question: dataframe - How to add a colorbar to a thematic map plot in Julia? - Stack Overflow

Thanks all!

~ tcp :deciduous_tree:

Maybe you can make use of the KW fill_z

using DataFrames
using Plots
using Shapefile
using ZipFile

# make directory downloads
downloads = joinpath(pwd(), "downloads")
if ~ispath(downloads)
    mkpath(downloads)
end

# make directory shapefiles
shapefiles = joinpath(pwd(), "shapefiles")
if ~ispath(shapefiles)
    mkpath(shapefiles)
end

# download shapefiles
url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2020_v1.zip"
name_zipfile = split(url, "/")[end]
path_zipfile = joinpath(pwd(), "downloads", name_zipfile)
if ~isfile(path_zipfile)
    download(url, path_zipfile)
end

# extract shapefiles
path_shapefile = joinpath(pwd(), "shapefiles", "gemeente_2020_v1.shp")
if ~isfile(path_shapefile)
    r = ZipFile.Reader(path_zipfile)
    for file in r.files
        open(joinpath(pwd(), "shapefiles", file.name), "w") do io
            write(io, read(file))
        end
    end
end

# read shapefile
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame

# filter for land (i.e. not water)
row_filter = df.H2O .== "NEE"

# filter data and shapes
municipality_data = df[row_filter, :]
municipality_shape = Shapefile.shapes(table)[row_filter]

function normalize(array)
    """
    Normalize array to values between 0 and 1
    """
    return [(x - minimum(array))/(maximum(array) - minimum(array)) for x in array]
end

# select variable to plot
var = "BEV_DICHTH" # population density

# values to plot
values = municipality_data[:, var]
normalized_values = normalize(values)

# colors
colormap = :heat
colors = Array([cgrad(colormap)[value] for value in normalized_values])

# plot thematic map
p = plot(size=(500, 600), axis=false, ticks=false)
for i = 1:nrow(municipality_data)
    plot!(municipality_shape[i], fill_z=[normalized_values[i] for x in 1:length(municipality_shape[i].points)])
end
p

The coloring is not nice but this displays a colorbar.

2 Likes

Hey @lungd! That works beautifully! However, the colors aren’t used anywhere in your above example - I tried a different colormap gist_yarg for example - and I cannot get the correct colormap. Any thoughts on what to do to get the right colormap to propagate?

Happy to report after some tweaking, I figured this out @lungd ! Here is a solution to make everything work and nicely put together:

choropleth = plot(size = (1000, 600), axis = false, ticks = false)
for i in 1:length(states.geometry)
	plot!(states.geometry[i], fill = :gist_yarg, fill_z = [rand_vals[i] for x in 1:length(states.geometry[i].points)])
end

y = ones(3)
title = Plots.scatter(
    y,
    marker = 0,
    markeralpha = 0,
    annotations = (2, y[2], Plots.text("Rando Plot")),
    axis = ([], false),
    leg = false,
)

plot(
    title,
    choropleth,
    layout = grid(2, 1, heights = [0.10, 0.85]),
)

png(plotsdir("rando.png"))

Which produces this plot:

As a note, I used DataFrames.jl, Shapefile.jl, and Plots.jl to get this all done. :smiley: Hope this helps folks out!

1 Like

I’ll crosspost my response in the Plots issue (https://github.com/JuliaPlots/Plots.jl/issues/3368). The intended way to do this is fill_z instead of fillcolor.

using DataFrames
using Plots
using Shapefile

states = Shapefile.Table("cb_2018_us_state_5m.shp") |> DataFrame
skip = ["02", "15", "60", "66", "69", "72", "78"]
filter!(row -> !(row[:STATEFP] in skip), states)
sort!(states, [:GEOID])

n = size(states, 1)
plot(states.geometry, fill_z=rand(1, n), title="My title", cbar=true, color=:seismic)

shapes
Colors are chosen automatically based on the corresponding fill_z values from the provided color gradient (ColorSchemes · Plots) - in this case :seismic, the default is :inferno as in @lungd’s example. If you only provide colors as fillcolor without corresponding “z” values, Plots can not know how to arrange these colors in a colorbar.

3 Likes

You may also want to explore the method exemplified in this GMT example and be able to plot beautiful maps with true map projections.

2 Likes