Aarav Badani
Aarav Badani

Reputation: 3

How do I create a world map with a heat map on top of it

I have been trying to create a world map in R with a heat map overlaid on top of it. I have a dataframe called mydata in which there are 2 columns, the first column is country_code_author1 with ISO3 country codes, and the second column called "n" with the count of how many publications each country has. I attached the code I am using below, but it is only producing a heat patch on the United States when I run it. I attached a picture of my data frame below.

library(maps)
library(ggplot2)

mydata <- df_country_count_auth1

world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")

ggplot(mydata) +
  geom_map(
    dat = world_map, map = world_map, aes(map_id = region),
    fill = "white", color = "#7f7f7f", size = 0.25
  ) +
  geom_map(map = world_map, aes(map_id = country_code_author1, fill = n), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Worldwide Publications") +
  expand_limits(x = world_map$long, y = world_map$lat)

Code/Structure for a mini version of my dataframe

myData <-
  structure(
    list(
      country_code_author1 = c(
        "AGO",
        "AIA",
        "ALB",
        "ARE",
        "ARG",
        "ARM",
        "ATG",
        "AUS",
        "AUT",
        "AZE"
      ),
      n = c(3L, 1L,
            11L, 3L, 38L, 1L, 4L, 240L, 98L, 23L)
    ),
    row.names = c(NA,-10L),
    groups = structure(
      list(
        country_code_author1 = c(
          "AGO",
          "AIA",
          "ALB",
          "ARE",
          "ARG",
          "ARM",
          "ATG",
          "AUS",
          "AUT",
          "AZE"
        ),
        .rows = structure(
          list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
          ptype = integer(0),
          class = c("vctrs_list_of",
                    "vctrs_vctr", "list")
        )
      ),
      row.names = c(NA,-10L),
      class = c("tbl_df",
                "tbl", "data.frame"),
      .drop = TRUE
    ),
    class = c("grouped_df",
              "tbl_df", "tbl", "data.frame")
  )

What I am trying to accomplish

Upvotes: 0

Views: 1955

Answers (1)

Vishal A.
Vishal A.

Reputation: 1391

First of all, I can see that you have copied the above code from here without even understanding what's going on in it.

Anyways, inside the second geom_map you have made mistakes.

If you had printed the world_map in console you would have found out this:

> head(world_map)
       long      lat group order region subregion
1 -69.89912 12.45200     1     1  Aruba      <NA>
2 -69.89571 12.42300     1     2  Aruba      <NA>
3 -69.94219 12.43853     1     3  Aruba      <NA>
4 -70.00415 12.50049     1     4  Aruba      <NA>
5 -70.06612 12.54697     1     5  Aruba      <NA>
6 -70.05088 12.59707     1     6  Aruba      <NA>

From it, it is pretty evident that you need country names and not their abbreviations in order to get the geographical heat map.

So, first, you need to get country names from the country codes using the package countrycode.

library(countrycode)
countryName <- countrycode(myCodes, "iso3c", "country.name")
countryName

Which will give you output like this:

> countryName
 [1] "Angola"               "Anguilla"             "Albania"             
 [4] "United Arab Emirates" "Argentina"            "Armenia"             
 [7] "Antigua & Barbuda"    "Australia"            "Austria"             
[10] "Azerbaijan"      

Now, you need to add this to your original data frame.

myData <- cbind(myData, country = countryName)

The last mistake inside geom_map is the map_id that you passed which should always be the country name. So, you'll need to change it to country.

The final code looks likes this:

library(maps)
library(ggplot2)
library(countrycode)

myCodes <- myData$country_code_author1
countryName <- countrycode(myCodes, "iso3c", "country.name")
myData <- cbind(myData, country = countryName)

#mydata <- df_country_count_auth1

world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")

ggplot(myData) +
  geom_map(
    dat = world_map, map = world_map, aes(map_id = region),
    fill = "white", color = "#7f7f7f", size = 0.25
) +
  geom_map(map = world_map, aes(map_id = country, fill = n), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Worldwide Publications") +
  expand_limits(x = world_map$long, y = world_map$lat)

And the output looks like this:

enter image description here

Upvotes: 1

Related Questions