I would like to plot a thematic map (choropleth) using
Plots.jl. The DataFrame
df I use has 3 columns:
value2. The column
geometry is of element type
Polygon (data was read using the
Shapefile package). The columns
value2 are of element type
Int64 with values >= 0.
plot(df.geometry, color = :lightblue) creates a map with
lightblue polygons without issues (and without colorbar to the side).
However, I would like the
value2 column to color the polygons (shapes) based on a colormap/colorscheme like
viridis with a corresponding colorbar to the side as legend.
Any suggestions how to do this?
plot(shp.geometry, palette = ColorSchemes.viridis) does plot a coloured map, however it is not clear on which variable this colours are based. I would like to specify that manually (
value2 column) and have the colorbar added.
What happens if you add the plot argument:
colorbar=true? Also, try argument:
line_z = df.value2
Thanks for your reply. However, adding
colorbar = true does not show a colorbar. And adding
line_z = df.value2 does throw an error.
fill_z = df.value2 ?
If these standard Plots.jl arguments do not work, I do not know nothing about
Shapefile.jl objects and associated Plots’ recipes.
zfill it does not plot anything (but gives no error message).
Here’s the full code for reproducibility and possible soltuions.
# Load modules
# Create directorties
for dir in ["data", "downloads", "shapefiles"]
path = joinpath(pwd(), dir)
# Download shapefiles
zip_url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2021_v1.zip"
zip_loc = joinpath(pwd(), "downloads", split(zip_url, "/")[end])
# Extract shape files
r = ZipFile.Reader(zip_loc)
for f in r.files
file_name = split(f.name, "/")[end]
if startswith(file_name, "gemeente")
open(joinpath(pwd(), "shapefiles", file_name), "w") do io
# Read shapefiles
name_shapefile = "gemeente_2021_v1.shp"
path_shapefile = joinpath(pwd(), "shapefiles", name_shapefile)
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame
row_filter = df.H2O .== "NEE"
df = df[row_filter, [:geometry]]
# Add columns value1 and value2
df.value1 = rand(1:100, nrow(df))
df.value2 = rand(1:5000, nrow(df))
# Plot map
plot(df.geometry, palette = ColorSchemes.viridis, axis=false, ticks=false, size=(500, 600))
I think you need to pass to the
fill_z a transposed vector of values:
fill_z = permutedims(df.value1)
Another possibility, would be to define a customized vector of colors
colorarray and pass it as a fill color argument:
fc = permutedims(colorarray)
fill_z = permutedims(df.value1) works for me and does show a nice colorbar
fill_z = permutedims(df.value1) does work, however, I does not seem to use the specified colorscheme. Any suggestion on how to fix that?
I think you can use the
color together with the palette of your choice. For example: