COVID-19 visualizations with Stata Part 4: Maps
By: Asjad Naqvi
Tags: Maps, Covid 19, Automation, Color Scheme, Stata

Those, who have read and followed earlier guides, can see that here we add an additional folder, called GIS, to keep all the spatial files. If you are reading this guide for the first time, then just create a root folder, and within it, the following six subfolders shown above. The description of each folder is given in the figure.

Task 2: Get the map files

The generic map files for the world can be downloaded from different places on the internet. For this guide, we will use the ArcGIS 2020 world country boundaries file given at the link here: https://hub.arcgis.com/datasets/UIA::uia-world-countries-boundaries

The page looks like the image below. Since there are different versions of the files, make sure that the date of the file is somewhere in 2020. If not, then one can scroll down the page and select more recent versions of the same dataset.

On this page, just click on the Download drop-down menu and then click on the Shapefile, which will download a zipped file. Extract the contents of the zipped file in the GIS folder:

Here we get five files from the compressed zip file. In general, shapefiles can be directly easily viewed in softwares like ArcGIS (needs a license), QGIS (free) and GeoDa (free) just by opening the .shp file.

Task 3: Prepare the data

For the maps, we will use the Oxford COVID-19 Government Response Tracker (OxCGRT), that we also used in Guide 3. Guide 3, also included a detailed description on how to extract data from a Github repository. For the sake of completeness, the code required to set up the data set is repeated below:

clear

cd "D:/Programs/Dropbox/Dropbox/PROJECT COVID - MEDIUM"

*** import the data from Github

insheet using " *** import the data from Githubinsheet using " https://raw.githubusercontent.com/OxCGRT/covid-policy-tracker/master/data/OxCGRT_latest.csv ", clear *** new data has been added for USA and UK for regions so we drop the regions:

drop if regionname!="" *** keep only the variables we need

keep date countryname stringencyindexfordisplay ren stringencyindexfordisplay Index

ren countryname country *** fix the date tostring date, gen(date2) // string the date variable gen year = substr(date2,1,4) // extract the date components

gen month = substr(date2,5,2)

gen day = substr(date2,7,2) destring year month day, replace

drop date date2 gen date = mdy(month,day,year)

format date %tdDD-Mon-yyyy drop month day year

order country date

sort country date drop if date < 21915 // 21915 = 1st January ** save the files

compress

save ./master/COVID_policies.dta, replace

In the code above, we keep the overall policy stringency index, country name, and the date variables. The date variable is fixed in the middle part of the code above and, in the last step, we save the cleaned data in the master subfolder with the name COVID_policies.dta.

Task 4: Using Shapefiles in Stata

In order to use the shapefiles, we need to install the following Stata packages:

*** install the core packages ssc install spmap // for the maps package

ssc install shp2dta // shapefiles to dta. For versions < v15.

ssc install geo2xy // for fixing the coordinate system

For Stata 15 or higher, the custom spmap can also be replaced with the Stata’s official grmap command. Since grmap is a wrapper for spmap , I will stick to the original command. Both can be used interchangeably after minor tweaks to the syntax. An official Stata guide on importing JHU COVID-19 data, and setting it up using the grmap is given here.

shp2dta is only necessary if you are using Stata 14 or earlier versions. shp2dta should work with most of the shapefiles but there is a chance it might not read all shapefiles correctly and give an error. For Stata 15 or higher, the built-in command spshape2dta can be used. geo2xy is used for changing the map projections in Stata.

In the first step, we read the shapefile in Stata and covert it to Stata format:

********* get the shapefiles in order in order cd GIS // switch to the GIS folder

spshape2dta "World_Countries__Generalized_.shp", replace saving(world)

This will show the following screen output:

Here we can see that two datasets are created: world.dta, which contains the attributes of the shapes (or countries in our case), and world_shp.dta which contains the outlines of the shapes.

For Stata 14 and earlier versions, the following shp2dta can be used:

*** For Stata 14 or earlier versions ONLY: shp2dta using World_Countries__Generalized_.shp, database(world) coordinates(world_shp) genid(_ID) gencentroids(c) replace

We can also explore the world.dta and the world_shp.dta files:

*** explore the outline file use world_shp, clear

scatter _Y _X, msize(tiny) msymbol(point)

In the file above, each countries are defined by the variable _ID and are stored as a series of points. This is a standard structure of map files (spatial JSON files also have a similar concept of connecting dots to form a shape). From the command above, we get the following scatter: