Khushi Thakkar
Khushi Thakkar

Reputation: 41

Query on how to make world heat map using ggplot in R?

I want to make a world heat map of a particular disease. The dataset contains the list of countries, latitude, longitude and cases in each country. The data is available here. I want to represent it using a world heat map i.e. countries which have higher cases should have a darker color and it should lighten with the countries having lesser number of cases. I have used ggplot and geom_map to create the same. Code is as shown below:

library(maps)
library(plyr)
library(gridExtra)

h2 <- read_excel("mapdata1.xlsx")

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

gg <- ggplot(h2)
gg <- gg + geom_map(dat=world_map, map = world_map, aes(map_id=region), 
                    fill="white", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(map = world_map, aes(map_id = Country, fill = Cases), size=0.25)
gg <- gg + scale_fill_gradient(low="#fff7bc", high="#cc4c02", name="Total Cases")
gg <- gg + expand_limits(x = world_map$long, y = world_map$lat)
gg <- gg + labs(x="", y="", title="World Hotspots")
gg <- gg + theme(panel.grid=element_blank(), panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank(), axis.text=element_blank())
gg <- gg + theme(legend.position="top")
gg

When I run the code, it shows the heat map but certain countries like USA are white in color even though it has higher number of cases. I am not able to figure out why so?

Upvotes: 2

Views: 4251

Answers (1)

tjebo
tjebo

Reputation: 23807

The reason is simply that the USA has different names in your data and in the world map.

library(maps)
library(ggplot2)

mydata <- readxl::read_excel("your_path")

mydata$Country[mydata$Country == "United States"] <- "USA"

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 = Cases), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases") +
  expand_limits(x = world_map$long, y = world_map$lat)

Created on 2020-05-16 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions