How can I plot a choropleth map from a dataframe?

Hi

I have a dataframe with 4 columns:

  • DA_ID (these are districts)
  • MBR
  • points (these are the coordenates)
  • count (these are the number of times an incident happened. it is just an integer from 0 to 8094).

It looks like this:

How can I plot a choropleth map from that information?
I have already tested this library: PlotShapefiles.jl. It did not work for me.

In python I suceeded in plotting using Geopandas. But I want to use a Julia library.

Currently I am checking these:

https://github.com/penntaylor/ChoroplethMaps.jl

http://gadflyjl.org/stable/gallery/shapes/

https://nbviewer.jupyter.org/github/JuliaX/iap2014/blob/master/GeoData.ipynb

Thank you

I did some chloropeths using Vega-Lite, which like a year back it was the only library I could find to do those. I didn’t use dataframes, as my mapping data was in TopoJSON and the counts (and other info) were in an CSV file.

I followed this example:
https://www.flirtwithjulia.com/2019/02/18/Choropleth-Map-With-VegaLite.jl.html
And @mthelm85 helped me a lot with figuring this out.

But, using my dataframe…would it be possible to use VegaLite?
I did not used it as soon as I read that it could not ingest a .shp file.

I don’t know, to be honest. I did change my shapefile to TopoJSON using this free online tool SHP to TopoJSON Converter Online - MyGeodata Cloud

Maybe other people with more experience with Vega-Lite can help.

1 Like

Choropleth example:
https://www.queryverse.org/VegaLite.jl/dev/examples/examples_maps/

Using DataFrames example:
https://www.queryverse.org/VegaLite.jl/dev/examples/examples_barcharts/

The main problem is probably not VegaLite but your data format. I’m afraid that there has to be some reformating before it can be digested by VegaLite. Start with some smaller data set (for us to understand).

With VegaLite, I need to convert the .shp file to topojson.

In this notebook https://nbviewer.jupyter.org/github/JuliaX/iap2014/blob/master/GeoData.ipynb there is an example using a dataframe.

I do not understand what is happening here:

This is a function definition. The function is used in the next step. It seems that the function transforms Polygons between two different formats. ESRI seems to be a company and probably this is some kind of proprietary data format for polygins (just guessing here).

It would be better you find some small dataset to experiment with, maybe a subset of your data. With that you could create a MWE (Please read: make it easier to help you) and from this I could help with real VegaLite code.

How can I test that function in this small dataframe?

image 1
image

image 2

I want to know how it is changing the column “x1”. Because as I inserted the .shp file in Julia I can see it in 2 ways:

a) like the one you see in image 1 (“x1”). Lets call it df_x

b) like the one you see in image 2 (“MBR”, “parts”, “points”).
Here, the columns “land_area” and “water_area” do not appear. But they are in the dataframe. Lets call it df_y

I want to know what it is doing because I think I do not need it. Why? Because I can see the dataframe in 2 ways: like df_x or df_y.

Can you help me there?

Maybe something like this:

# Going from one kind of polygon to another
ESRI2Compose(poly::Shapefile.Polygon) =  Compose.FormTree(Compose.Polygon([Compose.Point(point.x,point.y) for point in poly.points]))

for row in EachRow(df_x)
     c=ESRI2Compose(row["x1"][1]),
end
println(c)

I get this error:

image

After modifying the code to this:

for row in df_x
    c = compose(c,compose(canvas(template),ESRI2Compose(row["x1"][1]),
    linewidth(0.05mm),stroke(color),fill(nothing)))
end

I am getting this other error:

For me to try something and come to a reliable conclusion and suggestion for you, I would have to write your screenshots off.
First advice: Read

thoroughly.
But don’t expect me to be able to help you. I just can’t tell yet because I hesitate to dive into this screenshots.

1 Like

I do not know how to send or create a dataframe with columns of type “shapefile”. So I cannot create a MWE.

Now I have started working with VegaLite.jl. It uses topojson. So I converted my .shp file to a topojson. Now I am working on ingesting that file into julia. Do you know how to do that?
I have the topojson file in my computer.

I am reading this right now: Data sources · VegaLite.jl

GeoInterface.jl (which Shapefile.jl uses) has Plots.jl recipes, so you should be able to plot it using using Plots; plot(dataframe[:points]).

1 Like

I am using VegaLite now. This is the new link: https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/10

I changed my shapefile to a topojson. I am no longer using a dataframe.
I guess I will need to parse the dataframe before sending it to VegaLite. I have parsed the topojson before sending it to VegaLite.