AMCG
AMCG

Reputation: 1

Building a heat world map

I'm trying to build a world heat map and I'm stumped as to why it looked this:

enter image description here

My data is super simple:

enter image description here

Why is it not showing a normal map? Furthermore, the UK has the highest number, but it is grey?!

Please advise. Thanks

data <- data.frame(
  country = c("UK", "Ghana", "USA", "Thailand", "Columbia", "Brazil"),
  value = c(12, 2, 1, 2, 1, 1)
)

world <- map_data("world")
data <- merge(world, data, by.x = "region", by.y = "country")

ggplot(data, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = value), color = "grey") +
  scale_fill_gradient(low = "blue", high = "red") +
  theme_void()

I've tried various different packages but haven't been able to solve the problem.

Upvotes: 0

Views: 122

Answers (1)

Jon Spring
Jon Spring

Reputation: 66480

I get a plausible result with three changes:

  1. replace merge with dplyr::left_join. I suspect the merge is not preserving the proper ordering of the polygon points, leading to crisscrossing between perimeter points.

  2. add coord_sf() to convert the coordinates to a projection, in this case using "WGS 84 / World Equidistant Cylindrical"

  3. remove the gray outline, default of which obscures most UK silhouette because its too thick.


library(dplyr); library(ggplot2)
data %>%
  left_join(world, by = c("country" = "region")) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = value)) +
  coord_sf(crs = 4087) +
  scale_fill_gradient(low = "blue", high = "red") +
  theme_void()

enter image description here

Upvotes: 1

Related Questions