Search code examples
juliavega-lite

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


I got the dataset like following: enter image description here

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: enter image description here

then I realized this data have country name like this: https:

enter image description here

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:)

I just wanna say thank you to all people that offered me suggestions!!!!!! I have successfully change my country name, but I don't know how to make a map for each country and show which country have higher mean value, Can someone give me some advices please? enter image description here


Solution

  • These abbreviations resemble ISO 3166 alpha-2 codes. The Julia package Countries.jl is great for converting ISO standards:

    julia> using Countries, DataFrames
    
    julia> ds = DataFrame(company_location=["RU", "US", "NZ"], Mean=[1580000, 142000, 122000])
    3×2 DataFrame
     Row │ company_location  Mean
         │ String            Int64
    ─────┼───────────────────────────
       1 │ RU                1580000
       2 │ US                 142000
       3 │ NZ                 122000
    
    julia> ds.country_names = [x.name for x in get_country.(ds.company_location)];
    
    julia> ds
    3×3 DataFrame
     Row │ company_location  Mean     country_names
         │ String            Int64    String
    ─────┼───────────────────────────────────────────────
       1 │ RU                1580000  Russian Federation
       2 │ US                 142000  United States
       3 │ NZ                 122000  New Zealand
    
    

    Alternatively, you could make your own dictionary that maps codes to full names. This could be useful if the abbreviations are non-standard, or if you're working with something else in general:

    julia> using DataFrames
    
    julia> ds = DataFrame(company_location=["RU", "US", "NZ"], Mean=[1580000, 142000, 122000])
    3×2 DataFrame
     Row │ company_location  Mean
         │ String            Int64
    ─────┼───────────────────────────
       1 │ RU                1580000
       2 │ US                 142000
       3 │ NZ                 122000
    
    julia> country_codes = Dict("RU" => "Russia", "US" => "United States", "NZ" => "New Zealand");
    
    julia> ds.country_names = getindex.(Ref(country_codes), ds.company_location);
    
    julia> ds
    3×3 DataFrame
     Row │ company_location  Mean     country_names
         │ String            Int64    String
    ─────┼──────────────────────────────────────────
       1 │ RU                1580000  Russia
       2 │ US                 142000  United States
       3 │ NZ                 122000  New Zealand
    

    (I couldn't find an obvious way to pass multiple indices to a dictionary, but this post on the Julia Discourse shows a working method, which I've used in my example)