0

I have a dataframe with several columns in which i'd like to visualize some information. I want to display the information using a world map. Like this:

enter image description here

In my dataframe ,i have a column with the countries names, but i don't have the latitude/longitude informations. How can i make this plot using only the country names?

  • Does this answer help? https://stackoverflow.com/questions/54349988/accessing-latitude-and-longitude-coordinates-in-geocode – Prasanna S Jul 13 '20 at 00:56

1 Answers1

1

Many options. One is the rworldmap package.

library(rworldmap)

We need some data to map.

COVID <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", na.strings = "", fileEncoding = "UTF-8-BOM")

Aggregate to get the total cases.

library(dplyr)

CASES <- COVID %>% group_by(countriesAndTerritories) %>%
  summarise(`Total cases` = sum(cases)) %>%
  mutate(countriesAndTerritories=gsub("_", " ", countriesAndTerritories))        

If you've already got your data, then you can start from here. Just two steps.

Step 1. Join the map with your own data using "NAME" for joinCode and the name of the variable in your data that represents the country name for nameJoinColumn.

COVID.map <- joinCountryData2Map(CASES, joinCode = "NAME", nameJoinColumn = "countriesAndTerritories")

Step 2. Plot this object.

par(mar=c(0,0,1,0))
mapCountryData(COVID.map, nameColumnToPlot="Total cases")

enter image description here

It's not a particularly useful map because the data are highly skewed. But you can see how easy it is. The most difficult part is to ensure your country names match those from the package. You can see these from:

countryRegions$ADMIN
[1] "Afghanistan"                  "Akrotiri Sovereign Base Area" "Aland"                       
[4] "Albania"                      "Algeria"                      "American Samoa"

There's also a country synonym data base:

countrySynonyms 

A ggplot version:

library(ggplot2)
library(scales)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale = "medium", returnclass = "sf")

COVID.world <- merge(world, CASES, by.x="admin", by.y="countriesAndTerritories")

ggplot(data = COVID.world) + 
  geom_sf(aes(fill=Total)) +
  scale_fill_gradient(label=comma) +
  theme_void()

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26