Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create ggplot2 Map in R using count by country

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading