# How to change the country name and then draw the global map?

**URL:** <https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076>\
**Category:** Data\
**Tags:** question, vegalite, inmemorydatasets\
**Created:** [October 1, 2022, 9:19am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076 "2022-10-01T09:19:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 1, 2022, 9:19am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/1 "2022-10-01T09:19:08Z")

</div>

Hi, I got the dataset like following:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed057f392da07089ec62af3bd2e4a03bfbce17c6.png)  
I want to make a world map and see which country have higher mean salary, maybe represent through density or sth else, like density higher means the mean salary is higher, I tried do that with vegalite but I always got the error:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/1055c79a720c8e332cd9a6bdefc3e77ea6655b4e.png)  
then I realized this data have country name like this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/6/169dd5ba3b88123735f8e976e248741a0d0e8690.png)  
Ru means russia, NZ means new zealand …Is there any way that I can covert these into the complete country name? and where have I got wrong on this map code?  
Can someone help me with that please?  
Thanks for any help:)

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 1, 2022, 10:20am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/2 "2022-10-01T10:20:58Z")

</div>

You can find a mapping between country codes and country names here [List of country codes by alpha-2, alpha-3 code (ISO 3166)](https://www.iban.com/country-codes) (assuming your country codes follow the standard pattern). You can add a column to your data frame using `leftjoin` to add a column with a full country names based on the `on` column that are country codes.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 1, 2022, 11:54am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/3 "2022-10-01T11:54:59Z")

</div>

Adding to @bkamins 's answer, you can then use the complete country code to download the geometries into a geospatial table:

```julia
using GeoTables

BRA = GeoTables.gadm("BRA", depth = 1)

```

These tables can be plotted directly with either MeshViz.jl (or GeoStatsViz.jl):

```julia
using MeshViz

import GLMakie as Mke

viz(BRA, variable=:column_with_color)

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 1, 2022, 12:55pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/4 "2022-10-01T12:55:25Z")

</div>

Very nice!

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 2, 2022, 9:23am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/5 "2022-10-02T09:23:20Z")

</div>

For the country lookup part, you can use [`Countries.jl`](https://github.com/cjdoris/Countries.jl):

```julia
julia> using Countries

julia> get_country("NZ").common_name
"New Zealand"

```

You can get a dataframe of all countries like so, so that you can use @bkamins’s suggested join:

```julia
julia> using Countries, DataFrames

julia> DataFrame(all_countries())
249×7 DataFrame
 Row │ alpha2 alpha3 name numeric official_name ⋯
     │ String String String Int16 String ⋯
─────┼──────────────────────────────────────────────────────────────────
   1 │ AW ABW Aruba 533 Aruba ⋯
   2 │ AF AFG Afghanistan 4 Islamic Republic
   3 │ AO AGO Angola 24 Republic of Ango
   4 │ AI AIA Anguilla 660 Anguilla
   5 │ AX ALA Åland Islands 248 Åland Islands ⋯
  ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
 246 │ YE YEM Yemen 887 Republic of Yeme
 247 │ ZA ZAF South Africa 710 Republic of Sout
 248 │ ZM ZMB Zambia 894 Republic of Zamb
 249 │ ZW ZWE Zimbabwe 716 Republic of Zimb ⋯
                                          3 columns and 240 rows omitted

```

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 4, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/6 "2022-10-04T14:26:44Z")

</div>

Thank you soooooooo much!!! I finally got this out!!!

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 4, 2022, 3:19pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/7 "2022-10-04T15:19:35Z")

</div>

Hi, Thank you so much for the guide!! but I have met some problem, I got the full data table like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/6/26c60f76527e1dee14b59745cc864dcc6d5c459f.png)  
I have googled gadm it says it loads data from the GADM project, I tried put my data in there but it appeared to be some errors, same error still appear even I make a new dataset contain only the alpha3 column, can you please give me some advices about it?  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/0/20bd6c8eb79fb3092ac324be4b42ae52c22f1ddb.png)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 4, 2022, 3:55pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/8 "2022-10-04T15:55:36Z")

</div>

@akshdfyehd you misunderstood the gadm function. You need to pass the name of the country to it. “final” is a table.

Try

```julia
geotable = GeoTables.gadm("BRA")

```

and will see a special table object that has a set of attribute columns and a special column called “geometry”. You can access the attribute table with `values(geotable)` and the underlying geometries with `domain(geotable)`. Watch my JuliaCon talk to understand these concepts:

[![](https://global.discourse-cdn.com/julialang/original/3X/8/6/86a1676a1da38b4db999abd19ef80e6819092e21.jpeg "Geostatistical Learning | Júlio Hoffimann | JuliaCon 2021") ](https://www.youtube.com/watch?v=75A6zyn5pIE)

You can also access the columns of the `geotable` with familiar syntax like `geotable.geometry` or `geotable.NAME` etc. If you want the geometries I recommend using `domain(geotable)` instead of `geotable.geometry` because the former is what we call “type-stable” in Julia.

Please feel free to ask more questions if something is not working or is not clear.

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 5, 2022, 8:08am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/9 "2022-10-05T08:08:21Z")

</div>

Thank you for that video link and reply!! 😆  
but my goal is to present the comparison of the salary(which is the ‘Mean’ column) by differentiate the densities or colors for different countries and produce a world map, but these functions in the video seems only be able to visualize only one country? Am I right? If my understanding is correct, I need to pass all 50 country code into that function manually and combine and compare them, so can you please tell me an easier way to achieve that? If my understanding is wrong, please give me some advices 😀

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

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 5, 2022, 10:09am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/10 "2022-10-05T10:09:01Z")

</div>

The GADM dataset is organized on a country level. It is not possible to download the full world map with a single call (it would be a huge dataset going all the way down to states, counties, etc.).

The simplest method to produce a world map with a given collection of countries is the following. Suppose you have a vector with country codes:

```julia
countries = ["BRA", "ARG", "PER"]

```

You can download the geotables for these specific countries with:

```julia
geotables = [GeoTables.gadm(country, depth=0) for country in countries]

```

Notice the option `depth=0`. It is used to download the geometry of the country itself instead of the geometries of the states and counties inside the country. You can pass `depth=0,1,2,..` all the way down to zoom into specific polygons.

After you have the geospatial tables, you can then extract the polygon of the country for plotting:

```julia
polygons = [first(geotable.geometry) for geotable in geotables]

```

Plotting a vector of polygons is super easy:

```julia
using MeshViz

import GLMakie as Mke

viz(polygons)

```

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

You can pass a vector of colors and transparencies with the options `color` and `alpha`:

```julia
viz(polygons, color = 1:3)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/75b36050362fa139fafe4c2a411c7c51b77d940b.png)

and pick any color scheme from the ColorSchemes.jl package:

```julia
viz(polygons, color = 1:3, colorscheme = :tab10)

```

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

Let me know if something is not clear. We designed an ecosystem for geospatial data in GeoStats.jl that is very intuitive for first-time users. Everything should work as you would expect.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 5, 2022, 6:01pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/11 "2022-10-05T18:01:17Z")

</div>

Another issue with using GDAM for global maps, other than having to make a collection from individual countries, is that the country polygons have a too high resolution. It’s simpler to use a low resolution dataset, like for example [https://github.com/datasets/geo-countries/blob/master/data/countries.geojson](https://github.com/datasets/geo-countries/blob/master/data/countries.geojson)

I have updated the GMT.jl [choropleth examples](https://www.generic-mapping-tools.org/GMTjl_doc/examples/choropleths/choro_examples/) where one does something very similar to what the OP seems to want. Adapting this example to the _Mean salary_ data should be straightforward.

```julia
Using GMT, CSV, HTTP, DataFrames

# Read the country polygons directly into a GMTdadset
countries = gmtread("/vsicurl/https://github.com/datasets/geo-countries/raw/master/data/countries.geojson");

# Read a file with the population density into a DataFrame
pop = CSV.File(HTTP.get(https://raw.githubusercontent.com/tillnagel/unfolding/master/data/data/countries-population-density.csv).body, delim=';') |> DataFrame;

```

Both the `countries` and `pop` share a common _attribute_, the `Country code`

```julia
julia> countries
Vector{GMTdataset} with 4264 segments
Show first segment. To see other segments just type its element number. E.g. D[7]

Attributes: Dict("ISO_A2" => "AW", "ISO_A3" => "ABW", "ADMIN" => "Aruba")
BoundingBox: [-70.06240800699987, -69.87682044199994, 12.417669989000046, 12.632147528000104]
PROJ: +proj=longlat +datum=WGS84 +no_defs

```

and

```julia
julia> pop
248×3 DataFrame
 Row │ Country Name Country Code 2010
     │ String String3 Float64?
─────┼───────────────────────────────────────────────────────────
   1 │ Arab World ARB 26.2725
   2 │ Caribbean small states CSS 16.9939
   3 │ East Asia & Pacific (all income … EAS 90.5122
   4 │ East Asia & Pacific (developing … EAP 123.729

```

Now create a numeric vector linking the population density and the country polygons via the attribute “ISO\_A3”

```julia
zvals = polygonlevels(countries, string.(pop[!,2]), pop[!,3], att="ISO_A3");

```

and use it to create the colors in the map.

```julia
# First create the colormap. Limit the maximum range to 500 otherwise states
# like Monaco would the all the `reds` and the rest of the world would be all blues.
C = makecpt(range=(0,500,10));
plot(countries, region=(-180,180,-60,85), level=zvals, cmap=C, proj=:guess,
     title="World population density", colorbar=true, show=true)

```

 ![choro2_wpop](https://global.discourse-cdn.com/julialang/original/3X/6/1/6190e3781a2e3b3715b1a517bd810fd4daebf8fc.png)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 5, 2022, 6:21pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/12 "2022-10-05T18:21:27Z")

</div>

> [@joa-quim](#):
>
> Another issue with using GDAM for global maps, other than having to make a collection from individual countries, is that the country polygons have a too high resolution. It’s simpler to use a low resolution dataset,

That is why GeoTables.jl downloads the GADM data and performs decimation of the polygons behind the scenes for the end-user:

```julia
help?> GeoTables.gadm
  gadm(country, subregions...; depth=0, decimation=0.04)

  (Down)load GADM table using GADM.get and convert the geometry column to Meshes.jl geometries.

  The depth option can be used to return tables for subregions at a given depth starting from the given region specification.

  If decimation is greater than zero, decimate the geometries to reduce the number of vertices. The greater is the decimation value, the more aggressive is the
  reduction.

```

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 6, 2022, 11:16am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/13 "2022-10-06T11:16:19Z")

</div>

Thanks for the reply!! I have take the top5 countries:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/977ad60b3973bcf501ff2ea7ccc14eaa1c8c0da8.png)  
I was follow the code you have showed, they all goes well until the last step in viz, after I press enter I thought there should be a web pop out with the graphs, but it just stays like this for about an hour (its not crash), I have tried restart julia and repeat the process but nothing change, was it supposed to be wait for long time or something goes wrong?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4bbcb57aa2fe69da044a215fe12b0aabf9b0c581.jpeg)  
Thanks for the help!!! I really appreciate it!!!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 6, 2022, 11:32am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/14 "2022-10-06T11:32:09Z")

</div>

Can you please confirm the versions of the packages you have installed?

Type the following:

```julia
] status

```

If you are using an old version of GeoTables.jl (\<1.0.3), you may need to explicitly use the option `decimation` explained above to reduce the number of vertices in the polygons. You can seee that they currently have 5703, 8437 and 1670 that need to be triangulated in the visualization.

Another issue is that GLMakie.jl itself is slow to compile. So assuming you solved the issue above regarding decimation, you can see if the plot shows up. For me it shows in a few seconds.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 6, 2022, 11:38am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/15 "2022-10-06T11:38:01Z")

</div>

@akshdfyehd you can first try to reproduce my example with the countries I tried. If it works, then you are in the right path. The countries you chose like Russia, are very big geographically, so the number of points needed to represent them in detail is large. You may need to specify a more aggressive decimation option.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [October 6, 2022, 11:55am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/16 "2022-10-06T11:55:21Z")

</div>

> [@juliohm](#):
>
> You can seee that they currently have 5703, 8437 and 1670 that need to be triangulated in the visualization.

Sorry, a correction here. These number refer to the sub-polygons of the polygon loaded.

If you are having trouble with the GADM data set, try to use any other geojson file with countries that you can find online like the one others shared above. You can use `GeoTables.load("foo.geojson")` to load it in the same format.

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 6, 2022, 3:48pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/17 "2022-10-06T15:48:54Z")

</div>

Hi,  
I got the countries like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/b/db9978babc5824b6c6b9d2cbe34543f7f319f32d.png)  
and I got the dataframe like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b2bfba912a5ea9c7f8198ba1a45f9b3742a283c.png)  
for the next step which is zval using polygonlevels, my result comes out like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/4/44368275b8f82ba0d306e2d5068243dd94d56ac5.png)  
its all NA values returned, Do you know what caused this? really appreciate any advices!!!

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [October 6, 2022, 5:09pm UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/18 "2022-10-06T17:09:07Z")

</div>

Hmm, a few things.

- First just a side note. It’s easier to read if you paste plain text, wrapped under triple back ticks, then screen-shots.
- In you case you don’t need to use `string.(final[!,2])` because your second column is already a string (that was not the case in my example)
- I have fixed the above and several other cases in the GMT.jl master version. Unfortunately I’m very limited to release new GMT.jl versions due to a Julia libs dependency conflict.

I assume that your dataset is a public one, so the best is that you show us (or just me) a download link so I can debug why is `polygonlevels` failing to cross-link both data and return only NaNs.

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 7, 2022, 2:15am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/19 "2022-10-07T02:15:04Z")

</div>

Hi, this is the link:  
[https://raw.githubusercontent.com/akshdfyehd/salary/main/ds\_salaries.csv](https://raw.githubusercontent.com/akshdfyehd/salary/main/ds_salaries.csv)

Thanks!!!

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [October 7, 2022, 4:31am UTC](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076/20 "2022-10-07T04:31:23Z")

</div>

Hey, I have found the solution, which comes from my tutor:  
This is the final data:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/3/f30111517172c099e8b87a540f3a2b2ff72c59fa.png)  
code is like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/12cb36d142d29dd975e1138c34f5c0ee22e5efb7.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3e7d8776bddab083da304da550264cfe69a1925.png)  
and get the result like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c995d0b48d5a0042ee0cd52163910238b37eee9.png)

[Next page](https://discourse.julialang.org/t/how-to-change-the-country-name-and-then-draw-the-global-map/88076.md?page=2)
