Home > front end >  Create ggplot2 Map in R using count by country
Create ggplot2 Map in R using count by country

Time:04-14

I have dataset like below where I only have the country names and count of a specific metric.

Am I able to create a heat map showing the results as is or do I need more information?

countries <- c('Afghanistan', 'Algeria', 'Azerbaijan', 'Burundi', 'Central African Republic', 'Chad', 'Colombia', 'Colombia-ELN', 'Egypt', 'Ethiopia', 'India', 'Iran', 'Iraq', 'Israel', 'Lebanon', 'Libya', 'Mali', 'Mauritania', 'Mozambique', 'Nigeria', 'Pakistan', 'Peru', 'Philippines', 'Russia (Soviet Union)', 'Rwanda', 'Senegal', 'Somalia', 'Sudan', 'Syria', 'SyrianArabRepublic', 'Tajikistan', 'Thailand', 'Turkey', 'Uganda', 'Ukraine', 'Yemen (North Yemen)')

metric <- c(5, 5, 1, 1, 3, 1, 4, 1, 1, 5, 5, 2, 5, 4, 1, 3, 3, 1, 2, 4, 5, 1, 5, 5, 3, 1, 5, 5, 4, 1, 2, 5, 4, 4, 1, 5)

country_df <- data.frame(countries, metric)

head(country_df)

                 countries metric
1              Afghanistan      5
2                  Algeria      5
3               Azerbaijan      1
4                  Burundi      1
5 Central African Republic      3
6                     Chad      1

This was my attempt:

library(maps)
library(RColorBrewer)
library(forcats)

world_map <- map_data(map = "world")

ggplot(country_df)  
  geom_map(aes(map_id = countries, fill = fct_rev(as.factor(metric))), map = world_map)  
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), colour = 'black', fill = NA)  
  expand_limits(x = world_map$long, y = world_map$lat)  
  scale_fill_manual(name = "Counts", values = rev(brewer.pal(5, name = "Reds")))  
  theme_void()  
  coord_fixed()

Error in unit(x, default.units) : 'x' and 'units' must have length > 0

CodePudding user response:

I cannot reproduce this. Below my code - I have slightly modified the fill scales, but does not have an effect on your error.

Try running this in a completely empty session.

countries <- c('Afghanistan', 'Algeria', 'Azerbaijan', 'Burundi', 'Central African Republic', 'Chad', 'Colombia', 'Colombia-ELN', 'Egypt', 'Ethiopia', 'India', 'Iran', 'Iraq', 'Israel', 'Lebanon', 'Libya', 'Mali', 'Mauritania', 'Mozambique', 'Nigeria', 'Pakistan', 'Peru', 'Philippines', 'Russia (Soviet Union)', 'Rwanda', 'Senegal', 'Somalia', 'Sudan', 'Syria', 'SyrianArabRepublic', 'Tajikistan', 'Thailand', 'Turkey', 'Uganda', 'Ukraine', 'Yemen (North Yemen)')

metric <- c(5, 5, 1, 1, 3, 1, 4, 1, 1, 5, 5, 2, 5, 4, 1, 3, 3, 1, 2, 4, 5, 1, 5, 5, 3, 1, 5, 5, 4, 1, 2, 5, 4, 4, 1, 5)

country_df <- data.frame(countries, metric)

library(maps)
library(ggplot2)

world_map <- map_data(map = "world")

ggplot(country_df)  
  geom_map(aes(map_id = countries, fill = as.factor(metric)), map = world_map)  
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), colour = 'black', fill = NA)  
  expand_limits(x = world_map$long, y = world_map$lat)  
  scale_fill_brewer(name = "Counts", palette = "Reds")  
  theme_void()  
  coord_fixed()

Created on 2022-04-13 by the reprex package (v2.0.1)

  • Related