# When plotting a choropleth map, how to "read" the appropriate value of the topojson file and the dataframe?

**URL:** <https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605>\
**Category:** General Usage\
**Tags:** plotting, first-steps\
**Created:** [February 13, 2020, 10:19pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605 "2020-02-13T22:19:26Z")\
**Posts on this page:** 20\
**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 13, 2020, 10:19pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/1 "2020-02-13T22:19:26Z")

</div>

Hi

@oheil  
@davidanthoff

I want to plot a choropleth map using a topojson (I converted the .shp file to a topojson) and a dataframe.

- The topojson has the “polygons”, coordinates", and the names of the districts.

- The dataframe has many columns. But the useful ones for this case are: “count” (this is the # of IPs) and “featureId” (this has the names of the districts).

What these files have in common is the name of districts.

Here some images for you to understand:

TOPOJSON FILE

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

DATAFRAME

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

This is my code:

```julia
@vlplot(width=1000, height=800) +
@vlplot(
    mark={
        :geoshape
    },
    data={
        values=JSON.parsefile("/home/juliana/roc/Data/Topojson/disa_ONT_region.topojson"),
        format={
            type=:topojson,
            feature=:disa_ONT_region
        }
    },
    transform=[{
        lookup=:featureId,
        from={
            data=df_splunk,
            key=:featureId,
            fields=["count"]
        }
    }],
    color={
        "rate:q",
        scale={domain=[0, 0.15], scheme=:reds},
        legend={title="IPs Rate in Ontario - BST"}
    },
    projection={
        type=:albersUsa
    }
)

```

In the “transform” block, I am calling my dataframe (df\_splunk).  
I want “VegaLite” to use the column “featureId” (names of districts) and the “count” (number of IPs) to visualize a choropleth map. But I have not succeded yet.

I know that in “lookup” I have to put the field to look for in the dataframe. And that “field” is what both files have in common (in this case the names of the districts).

But in what part am I telling VegaLite to use the names of districts of the topojson file? I guess it is : “properties” or “DA\_ID” of the topojson file.

So, what am I doing wrong?

Thank you

Here some of the documentation and tutorials that I have checked:

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

[[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)]

---

<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 14, 2020, 12:46pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/2 "2020-02-14T12:46:45Z")

</div>

Could you try the following:

```julia
df_splunk2=DataFrame(DA_ID=df_splunk[!,:featureid],count=Int.(df_splunk[!,count]))

@vlplot(
    width=1000,
    height=800,
    :geoshape,
    data={
        values=JSON.parsefile("/home/juliana/roc/Data/Topojson/disa_ONT_region.topojson"),
        format={
            type=:topojson,
            feature=:disa_ONT_region
        }
    },
    transform=[{
        lookup=:DA_ID,
        from={
            data=df_splunk2,
            key=:DA_ID,
            fields=["count"]
        }
    }],
    color={
        "rate:q",
        scale={domain=[0, 0.15], scheme=:reds},
        legend={title="IPs Rate in Ontario - BST"}
    },
    projection={
        type=:albersUsa
    }
)

```

the idea is to create and use a new DataFrame where the column names match to the topojson data and count to be an Integer and not a String.

---

<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 14, 2020, 2:55pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/3 "2020-02-14T14:55:01Z")

</div>

@oheil

When using your code to change the name and convert to INT I get an error. So I modified it a bit:

```
#This is to change column 2 from "string" to "int"
df_splunk[!, 2].= parse.(Int ,df_splunk[!,2])
eltype.(eachcol(df_splunk))

#I am renaming column "featureId" to "DA_ID"
rename!(df_splunk, Symbol("featureId")=> Symbol("DA_ID"))

```

But the plot is empty. I can only see the colorbar.

---

<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 14, 2020, 3:13pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/4 "2020-02-14T15:13:15Z")

</div>

Ah, yes, I see my error now. Your code to modify the dataframe is fine.

Have you ever seen your map data?  
(Actually I have some major problems to create just a simple map from topojson data as a file.)  
What do you see if you just plot the map with:

```julia
@vlplot(
    width=500, height=300,
    mark={
        :geoshape,
        fill=:lightgray,
        stroke=:white
    },
    data={
        values=JSON.parsefile("/home/juliana/roc/Data/Topojson/disa_ONT_region.topojson"),
        format={
            type=:topojson,
            feature=:disa_ONT_region
        }
    },
    projection={type=:albersUsa}
)

```

I want to be sure, that this part of your plot is working.

---

<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 14, 2020, 3:32pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/5 "2020-02-14T15:32:57Z")

</div>

And another typo, color must be:

> [@Aizzaac](#):
>
> color={ “count:q”,

---

<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 14, 2020, 3:33pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/6 "2020-02-14T15:33:59Z")

</div>

> [@oheil](#):
>
> @vlplot( width=500, height=300, mark={ :geoshape, fill=:lightgray, stroke=:white }, data={ values=JSON.parsefile(“/home/juliana/roc/Data/Topojson/disa\_ONT\_region.topojson”), format={ type=:topojson, feature=:disa\_ONT\_region } }, projection={type=:albersUsa} )

It works. There is no problem with that. Check!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89bedab68096d086c65e085f70c059bbbcef0abd.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 14, 2020, 3:34pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/7 "2020-02-14T15:34:36Z")

</div>

Great, now check for the color line, see my post above

---

<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 14, 2020, 3:39pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/8 "2020-02-14T15:39:45Z")

</div>

I changed the typo. I missed that completely!  
But the map is just red. Probably I have to change the scale of the “count” column (check the image I get when using python).

Do you know how to multiply the whole column “count” by a log?

JULIA

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

PYTHON

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

---

<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 14, 2020, 3:42pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/9 "2020-02-14T15:42:16Z")

</div>

Okey, I do not think is the scale. Because when I move the mouse over the image It always says: 0

---

<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 14, 2020, 3:49pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/10 "2020-02-14T15:49:19Z")

</div>

What happens if you just say:

```julia
color="count:q"

```

no scale and no title?

---

<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 14, 2020, 3:50pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/11 "2020-02-14T15:50:22Z")

</div>

I guess it is using a default color.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0f87f37a8edf36bd3ddb125b58172b8d9457c76.png)

---

<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 14, 2020, 3:54pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/12 "2020-02-14T15:54:11Z")

</div>

In Python, DA\_ID is of type “object”.

---

<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 14, 2020, 3:55pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/13 "2020-02-14T15:55:53Z")

</div>

“count” can be a bad name, because it is an aggregate method in VegaLite.  
Could you try by renaming “count” to something arbitrary like e.g. “myIPcounts” ?

---

<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 14, 2020, 4:25pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/14 "2020-02-14T16:25:34Z")

</div>

I did. Nothing happens. But the color bar displays NaN.

---

<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 14, 2020, 6:28pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/15 "2020-02-14T18:28:13Z")

</div>

Is there some chance that you could provide us with the json file?

---

<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 14, 2020, 6:48pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/16 "2020-02-14T18:48:56Z")

</div>

the topojson? yes. I have to compress it. It is big! 492.0kB

---

<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 14, 2020, 6:49pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/17 "2020-02-14T18:49:42Z")

</div>

Better some download service like dropbox.

---

<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 14, 2020, 7:06pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/20 "2020-02-14T19:06:00Z")

</div>

Arrived, could you please delete your above post? My email must not be available anymore. Thanks.

---

<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 14, 2020, 7:06pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/21 "2020-02-14T19:06:43Z")

</div>

okey. it is deleted.

---

<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 14, 2020, 8:19pm UTC](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605/22 "2020-02-14T20:19:05Z")

</div>

I didn’t found a elegant transformation for the problem, that DA\_ID is nested one level down into properties. So I manipulated the topojson data:

```julia
using VegaLite, DataFrames, JSON

#I renamed the file to .json => I can open in a browser and get it formated and clickable view on the data
#filename="C:\\Users\\oheil\\Desktop\\disa_ONT_region.json"
filename=
"/home/juliana/roc/Data/Topojson/disa_ONT_region.topojson"
json=JSON.parsefile(filename)

```

The following code just creates a dummy DataFrame with the counts as Integers up to 1000 and the DA\_IDs as column id, this is your `df_splunk`:

```julia
DA_IDs=[json["objects"]["disa_ONT_region"]["geometries"][key]["properties"]["DA_ID"] for key in keys(json["objects"]["disa_ONT_region"]["geometries"]) ]
counts=Int.(floor.(1000*rand(Float64,length(DA_IDs))))
df_splunk=DataFrame(id=DA_IDs,count=counts)

```

The following code manipulates the already read in json file by adding a field `id` at the proper level:

```julia
for key in keys(json["objects"]["disa_ONT_region"]["geometries"])
	json["objects"]["disa_ONT_region"]["geometries"][key]["id"]=json["objects"]["disa_ONT_region"]["geometries"][key]["properties"]["DA_ID"]
end

```

Now the choropleth is working as expected by connecting the two `id` fields in the two data sets:

```julia
@vlplot(
    :geoshape,
    width=500, height=300,
    data={
        values=json,
        format={
            type=:topojson,
            feature=:disa_ONT_region
        }
    },
    transform=[{
        lookup=:id,
        from={
            data=df_splunk,
            key=:id,
            fields=["count"]
        }
    }],
    color={
        "count:q",
        scale={scheme=:reds},
        legend={title="IPs Rate in Ontario - BST"}
    },
    projection={
        type=:albersUsa
    }
)

```

I omitted the `domain` option so that the color range is scaled automatically.

![visualization](https://global.discourse-cdn.com/julialang/original/3X/7/0/7062fde09e3b95dcf1c8c03607cf631c771b47a7.png)

[Next page](https://discourse.julialang.org/t/when-plotting-a-choropleth-map-how-to-read-the-appropriate-value-of-the-topojson-file-and-the-dataframe/34605.md?page=2)
