The extract used for this project is from IPUMS-International and relates to the 2010 Panamanian Census. Here, I use “Previous District of Residence” (PA2010A_PRVDISRC) and the “District of Permanent Residence” (PA2010A_RESDISRC) to determine whether the respondent participated in an internal migration event. I do this among Black and non-Black Panamanians using the (PA2010A_BLACK) variable, which consists of 5 options where 1-4 delineate various Black identities and 5 reflects those who do not identify as Black.

library(dplyr)#filter
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tmap)#mapping
library(rgdal)#shapefile
## Loading required package: sp
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/lower/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/lower/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/lower/Documents/R/win-library/4.0/rgdal/proj
work_dir <- "C:/Users/lower/Desktop/rstudio/panama"
setwd(work_dir)



#Panama IPUMS
if (!require("ipumsr")) stop("Reading IPUMS data into R requires the ipumsr package. It can be installed using the following command: install.packages('ipumsr')")
## Loading required package: ipumsr
ddip <- read_ipums_ddi("ipumsi_00017.xml")
panta <- read_ipums_micro(ddip);ipums_view(panta)
## Use of data from IPUMS-International is subject to conditions including that
## users should cite the data appropriately. Use command `ipums_conditions()` for
## more details.

Here, I restrict the migration variables to thsoe internal migrants - excluding out-of-state migrants.

panta$PRVI      <- ifelse(panta$PA2010A_PRVDISRC <= 1207, panta$PA2010A_PRVDISRC, NA)
panta$CRES      <- ifelse(panta$PA2010A_RESDISRC <= 1207, panta$PA2010A_RESDISRC, NA)
panta$CRESi <- as.numeric(panta$CRES)
panta$PRVIi <- as.numeric(panta$PRVI)
panta$PRV <- ifelse(panta$PRVIi == panta$CRESi, 0, 1)

I create % variables of internal migrants by race.

#https://tlorusso.github.io/geodata_workshop/tmap_package
#http://www.wvview.org/spatial_analytics/Visualizing_Spatial_Data/_site/Visualize_Spatial_Data.html

#install.packages("rgeos")
#library(rgeos)

shp = readOGR(dsn = "C:\\Users\\lower\\Desktop\\gis\\panama\\ipumsgeo", layer = "geo2_pa1960_2010") 
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\lower\Desktop\gis\panama\ipumsgeo", layer: "geo2_pa1960_2010"
## with 36 features
## It has 5 fields
shp.df <- as(shp, "data.frame")


GIS <- panta %>% filter(PRV >= 0) %>% group_by(GEOLEV2)%>% summarise(Total=sum(PERWT), 
                                                                     PRVW=sum(PERWT[PRV & PA2010A_BLACK >= 4]),
                                                                     PRVB=sum(PERWT[PRV & PA2010A_BLACK <= 4 ]))

GIS$PERb <- GIS$PRVB/GIS$Total
GIS$PERw <- GIS$PRVW/GIS$Total


GIS
## # A tibble: 35 x 6
##      GEOLEV2  Total  PRVW  PRVB    PERb   PERw
##        <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl>
##  1 591002001 103070 22000  1360 0.0132  0.213 
##  2 591002002  51370  8900   920 0.0179  0.173 
##  3 591002003  42290 11840   610 0.0144  0.280 
##  4 591002004  31550  5280   130 0.00412 0.167 
##  5 591003001 208020 33610  8150 0.0392  0.162 
##  6 591003002  23690  5390   280 0.0118  0.228 
##  7 591003003  31690  2070    40 0.00126 0.0653
##  8 591004001 151900 32900  2600 0.0171  0.217 
##  9 591004002 142690 28330  1440 0.0101  0.199 
## 10 591004003  98030 33950   940 0.00959 0.346 
## # ... with 25 more rows
shp2 = merge(shp, GIS, by.x = "GEOLEVEL2", by.y = "GEOLEV2", all.x = TRUE)
shp2.df <- as(shp2, "data.frame")

tmap_options(bg.color="lightblue")  
#tmaptools:: palette_explorer() 

tm_shape(shp2)+
tm_polygons("PERw", palette="YlOrRd", contrast=.7, id="name", title="Figure 2. Percent of Non-Black Internal Migrants, 2010 (%)",legend.is.portrait=TRUE) +
tm_compass(position = c("left", "bottom"))+
tm_scale_bar(position = c("left", "bottom"))+
tm_layout(title = "", title.size = 15.5, legend.outside=TRUE)
## Warning in sp::proj4string(obj): CRS object has comment, which is lost in output
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1

tm_shape(shp2)+
tm_polygons("PERb", palette="YlOrRd", contrast=.7, id="name", title="Figure 2. Percent of Black Internal Migrants, 2010 (%)",legend.is.portrait=TRUE) +
tm_compass(position = c("left", "bottom"))+
tm_scale_bar(position = c("left", "bottom"))+
tm_layout(title = "", title.size = 15.5, legend.outside=TRUE)
## Warning in sp::proj4string(obj): CRS object has comment, which is lost in output