# How can I plot a choropleth map from a dataframe?

**URL:** <https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479>\
**Category:** General Usage\
**Tags:** first-steps\
**Created:** [February 11, 2020, 6:27pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479 "2020-02-11T18:27:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 11, 2020, 6:27pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/1 "2020-02-11T18:27:29Z")

</div>

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:

 ![image](https://global.discourse-cdn.com/julialang/original/2X/5/5b67bdb55310742d279c82fe6716e77ee1dcb653.png)

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 11, 2020, 6:35pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/2 "2020-02-11T18:35:07Z")

</div>

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](https://www.flirtwithjulia.com/2019/02/18/Choropleth-Map-With-VegaLite.jl.html)  
And @[mthelm85](https://discourse.julialang.org/u/mthelm85) helped me a lot with figuring this out.

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 11, 2020, 6:37pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/3 "2020-02-11T18:37:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 11, 2020, 6:41pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/4 "2020-02-11T18:41:10Z")

</div>

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](https://mygeodata.cloud/converter/shp-to-topojson)

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

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 12, 2020, 8:34am UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/5 "2020-02-12T08:34:50Z")

</div>

Choropleth example:  
[https://www.queryverse.org/VegaLite.jl/dev/examples/examples\_maps/](https://www.queryverse.org/VegaLite.jl/dev/examples/examples_maps/)

Using DataFrames example:  
[https://www.queryverse.org/VegaLite.jl/dev/examples/examples\_barcharts/](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).

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 12, 2020, 3:36pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/6 "2020-02-12T15:36:15Z")

</div>

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](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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae06f2ecc667cf840d05415a10ed2e35cb940212.png)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 12, 2020, 3:43pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/7 "2020-02-12T15:43:22Z")

</div>

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](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)) and from this I could help with real VegaLite code.

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 12, 2020, 4:17pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/8 "2020-02-12T16:17:26Z")

</div>

How can I test that function in this small dataframe?

image 1  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f1af9cba40838f9a37ab4fb07db306e5ec4f5a09.png)

image 2

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/4/34748d689797ac9bc6ffc578ec261ab4d09aa7c8.png)

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](https://global.discourse-cdn.com/julialang/original/3X/7/6/76abee4d9b42c7bc22522570d56ccc480d6e4a76.png)

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/39175126c9fe61ac7d0278997eaeeed38ee21762.png)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 12, 2020, 7:16pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/9 "2020-02-12T19:16:19Z")

</div>

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

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

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.

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 12, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/10 "2020-02-12T19:22:55Z")

</div>

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](https://www.queryverse.org/VegaLite.jl/stable/userguide/data/)

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [February 12, 2020, 7:29pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/11 "2020-02-12T19:29:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)\
**Post date:** [February 13, 2020, 3:29pm UTC](https://discourse.julialang.org/t/how-can-i-plot-a-choropleth-map-from-a-dataframe/34479/12 "2020-02-13T15:29:03Z")

</div>

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](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.
