Reputation: 1
I'm trying to build a world heat map and I'm stumped as to why it looked this:
My data is super simple:
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
Reputation: 66480
I get a plausible result with three changes:
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.
add coord_sf()
to convert the coordinates to a projection, in this case using "WGS 84 / World Equidistant Cylindrical"
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()
Upvotes: 1