R Markdown

This is my first G4D Assignment

library(rgdal)
## Loading required package: sp
## rgdal: version: 1.3-6, (SVN revision 773)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /usr/share/gdal/2.1
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.3-1

Loading required package: sp

library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.3, PROJ 4.9.2
library(sp)
library(cartography)
library(rgdal)

##Getting Vector data

download.file("http://data.biogeo.ucdavis.edu/data/diva/adm/TZA_adm.zip", 
destfile = "./data/TZA_adm.zip" , mode='wb')
unzip("./data/TZA_adm.zip", exdir = "./data")

##Creating an object sp color

spcol <- readOGR(dsn = "./data/TZA_adm1.shp", verbose = FALSE)
class(spcol)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
spcol@data$COLOUR <- "#FFFFFF"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 0] <- "#006837"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 1] <- "#52BE80"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 2] <- "#28B463"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 3] <- "#D4EFDF"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 4] <- "#138D75"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 5] <- "#76D7C4"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 6] <- "#A3E4D7"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 7] <- "#D0ECE7"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 8] <- "#FAE5D3"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 9] <- "#A9CCE3"

plot(spcol, col=spcol$COLOUR, main = "Tanzania's Departments")

### And for Library sf

library(sf)

sfcol <- st_read(dsn = "./data/TZA_adm1.shp", quiet = TRUE)
class(sfcol)
## [1] "sf"         "data.frame"
sfcol$COLOUR <- spcol@data$COLOUR

plot(st_geometry(sfcol), col=sfcol$COLOUR, main = "Tanzania's Departments")

library(cartography)
data(nuts2006)

##For color plots

plot(nuts0.spdf, border = NA, col = NA, bg = "#A6CAE0")
# Plot non european space
plot(world.spdf, col  = "#E3DEBF", border=NA, add=TRUE)
# Plot a layer of countries borders
plot(nuts0.spdf, border = "grey20", lwd = 3, add = TRUE)
# Plot a layer of NUTS1
plot(nuts1.spdf, border = "grey30", lwd = 2, add = TRUE)
# Plot a layer of NUTS2
plot(nuts2.spdf, border = "grey40", lwd = 0.5, add = TRUE)
# Plot a layer of NUTS3
plot(nuts3.spdf, border = "grey20", lwd = 0.1, add = TRUE)

####Plot 10 Most polulated countries in Europe

# Layout plot
layoutLayer(title = "Most Populated Countries in Europe", # title of the map
            author = "Author:Miriam Kasebele",  # 
            sources = "Sources: Eurostat", # 
            scale = NULL, # no scale
            col = NA, # no color for the title box 
            coltitle = "black", # color of the title
            frame = TRUE, 
            bg = "#A6CAE0", # background of the map
            extent = nuts0.spdf) # set the extent of the map

# Non European space
plot(world.spdf, col = "#AAB7B8", border = NA, add = TRUE)
# European (EU28) countries
plot(nuts0.spdf, col = "#F12BD0",border = "white", lwd = 1, add = TRUE)

# Selection of the 10 most populated countries of Europe
dflab <- nuts0.df[order(nuts0.df$pop2008, decreasing = TRUE),][1:10,]
# Label creation
dflab$lab <- paste(dflab$id, "\n", round(dflab$pop2008/1000000,0), "M", sep ="")

# Label plot of the 10 most populated countries
labelLayer(spdf = nuts0.spdf, # SpatialPolygonsDataFrame used to plot he labels
           df = dflab, # data frame containing the lables
           txt = "lab", # label field in df
           col = "#690409", # color of the labels
           cex = 0.6, # size of the labels
           font = 2) # label font

# Add an explanation text
text(x = 5477360, y = 4177311, labels = "The 10 most populated countries of Europe
Total population 2008 [millions]", cex = 0.7, adj = 0)

##Computation of growth rate

# Compute the compound annual growth rate
nuts2.df$cagr <- (((nuts2.df$pop2008 / nuts2.df$pop1999)^(1/9)) - 1) * 100

# Set a custom color palette
cols <- carto.pal(pal1 = "green.pal", # first color gradient
                  n1 = 2, # number of colors in the first gradiant
                  pal2 = "red.pal", # second color gradient
                  n2 = 4) # number of colors in the second gradiant

# Plot a layer with the extent of the EU28 countries with only a background color
plot(nuts0.spdf, border = NA, col = NA, bg = "#A6CAE0")
# Plot non european space
plot(world.spdf, col  = "#E3DEBF", border=NA, add=TRUE)

# Plot the compound annual growth rate
choroLayer(spdf = nuts2.spdf, # SpatialPolygonsDataFrame of the regions
           df = nuts2.df, # data frame with compound annual growth rate
           var = "cagr", # compound annual growth rate field in df
           breaks = c(-2.43,-1,0,0.5,1,2,3.1), # list of breaks
           col = cols, # colors 
           border = "grey40", # color of the polygons borders
           lwd = 0.5, # width of the borders
           legend.pos = "right", # position of the legend
           legend.title.txt = "Compound Annual\nGrowth Rate", # title of the legend
           legend.values.rnd = 2, # number of decimal in the legend values
           add = TRUE) # add the layer to the current plot

# Plot a layer of countries borders
plot(nuts0.spdf,border = "grey20", lwd=0.75, add=TRUE)

# Layout plot
layoutLayer(title = "Demographic Trends", author = "cartography", 
            sources = "Eurostat, 2008", frame = TRUE, col = NA, 
            scale = NULL,coltitle = "black",
            south = TRUE) # add a south arrow

library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
## Loading required package: bitops

####Loading required bitops

urlfile <- "https://raw.github.com/bhaskarvk/user2017.geodataviz/master/inst/extdata/africa-internet_usage-2015.csv"

internet_usage <- read.csv(urlfile)

head(internet_usage)
##   Country.Name Country.Code
## 1      Algeria          DZA
## 2       Angola          AGO
## 3      Bahrain          BHR
## 4        Benin          BEN
## 5     Botswana          BWA
## 6 Burkina Faso          BFA
##                                        Series.Name    Series.Code
## 1 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 2 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 3 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 4 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 5 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 6 Individuals using the Internet (% of population) IT.NET.USER.ZS
##   X2014..YR2014. X2015..YR2015. X2016..YR2016.
## 1       25.00000      38.200000             ..
## 2       10.20000      12.400000             ..
## 3       90.50313      93.478301             ..
## 4        6.00000       6.787703             ..
## 5       18.50000      27.500000             ..
## 6        9.40000      11.387646             ..

names(internet_usage) <- c("Country Name",  "Country Code",   "Series Name",  "Series Code", 
"2014 [YR2014]", "2015 [YR2015]", "2015 [YR20156]")


names(internet_usage)
## [1] "Country Name"   "Country Code"   "Series Name"    "Series Code"   
## [5] "2014 [YR2014]"  "2015 [YR2015]"  "2015 [YR20156]"

world <- sf::st_as_sf(rnaturalearth::countries110)
##str(world)
length(unique(world$iso_a3))
## [1] 175
print(world)
## Simple feature collection with 177 features and 63 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## First 10 features:
##   scalerank      featurecla labelrank           sovereignt sov_a3 adm0_dif
## 0         1 Admin-0 country         3          Afghanistan    AFG        0
## 1         1 Admin-0 country         3               Angola    AGO        0
## 2         1 Admin-0 country         6              Albania    ALB        0
## 3         1 Admin-0 country         4 United Arab Emirates    ARE        0
## 4         1 Admin-0 country         2            Argentina    ARG        0
## 5         1 Admin-0 country         6              Armenia    ARM        0
## 6         1 Admin-0 country         4           Antarctica    ATA        0
## 7         3 Admin-0 country         6               France    FR1        1
## 8         1 Admin-0 country         2            Australia    AU1        1
## 9         1 Admin-0 country         4              Austria    AUT        0
##   level              type                               admin adm0_a3
## 0     2 Sovereign country                         Afghanistan     AFG
## 1     2 Sovereign country                              Angola     AGO
## 2     2 Sovereign country                             Albania     ALB
## 3     2 Sovereign country                United Arab Emirates     ARE
## 4     2 Sovereign country                           Argentina     ARG
## 5     2 Sovereign country                             Armenia     ARM
## 6     2     Indeterminate                          Antarctica     ATA
## 7     2        Dependency French Southern and Antarctic Lands     ATF
## 8     2           Country                           Australia     AUS
## 9     2 Sovereign country                             Austria     AUT
##   geou_dif                             geounit gu_a3 su_dif
## 0        0                         Afghanistan   AFG      0
## 1        0                              Angola   AGO      0
## 2        0                             Albania   ALB      0
## 3        0                United Arab Emirates   ARE      0
## 4        0                           Argentina   ARG      0
## 5        0                             Armenia   ARM      0
## 6        0                          Antarctica   ATA      0
## 7        0 French Southern and Antarctic Lands   ATF      0
## 8        0                           Australia   AUS      0
## 9        0                             Austria   AUT      0
##                               subunit su_a3 brk_diff
## 0                         Afghanistan   AFG        0
## 1                              Angola   AGO        0
## 2                             Albania   ALB        0
## 3                United Arab Emirates   ARE        0
## 4                           Argentina   ARG        0
## 5                             Armenia   ARM        0
## 6                          Antarctica   ATA        0
## 7 French Southern and Antarctic Lands   ATF        0
## 8                           Australia   AUS        0
## 9                             Austria   AUT        0
##                     name                           name_long brk_a3
## 0            Afghanistan                         Afghanistan    AFG
## 1                 Angola                              Angola    AGO
## 2                Albania                             Albania    ALB
## 3   United Arab Emirates                United Arab Emirates    ARE
## 4              Argentina                           Argentina    ARG
## 5                Armenia                             Armenia    ARM
## 6             Antarctica                          Antarctica    ATA
## 7 Fr. S. Antarctic Lands French Southern and Antarctic Lands    ATF
## 8              Australia                           Australia    AUS
## 9                Austria                             Austria    AUT
##                     brk_name brk_group     abbrev postal
## 0                Afghanistan      <NA>       Afg.     AF
## 1                     Angola      <NA>       Ang.     AO
## 2                    Albania      <NA>       Alb.     AL
## 3       United Arab Emirates      <NA>     U.A.E.     AE
## 4                  Argentina      <NA>       Arg.     AR
## 5                    Armenia      <NA>       Arm.    ARM
## 6                 Antarctica      <NA>       Ant.     AQ
## 7 Fr. S. and Antarctic Lands      <NA> Fr. S.A.L.     TF
## 8                  Australia      <NA>       Auz.     AU
## 9                    Austria      <NA>      Aust.      A
##                                              formal_en formal_fr note_adm0
## 0                         Islamic State of Afghanistan      <NA>      <NA>
## 1                          People's Republic of Angola      <NA>      <NA>
## 2                                  Republic of Albania      <NA>      <NA>
## 3                                 United Arab Emirates      <NA>      <NA>
## 4                                   Argentine Republic      <NA>      <NA>
## 5                                  Republic of Armenia      <NA>      <NA>
## 6                                                 <NA>      <NA>      <NA>
## 7 Territory of the French Southern and Antarctic Lands      <NA>       Fr.
## 8                            Commonwealth of Australia      <NA>      <NA>
## 9                                  Republic of Austria      <NA>      <NA>
##                           note_brk                           name_sort
## 0                             <NA>                         Afghanistan
## 1                             <NA>                              Angola
## 2                             <NA>                             Albania
## 3                             <NA>                United Arab Emirates
## 4                             <NA>                           Argentina
## 5                             <NA>                             Armenia
## 6 Multiple claims held in abeyance                          Antarctica
## 7                             <NA> French Southern and Antarctic Lands
## 8                             <NA>                           Australia
## 9                             <NA>                             Austria
##   name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13  pop_est gdp_md_est
## 0     <NA>         5         6         8          7 28400000    22270.0
## 1     <NA>         3         2         6          1 12799293   110300.0
## 2     <NA>         1         4         1          6  3639453    21810.0
## 3     <NA>         2         1         3          3  4798491   184300.0
## 4     <NA>         3         1         3         13 40913584   573900.0
## 5     <NA>         3         1         2         10  2967004    18770.0
## 6     <NA>         4         5         1         NA     3802      760.4
## 7     <NA>         7         5         9         11      140       16.0
## 8     <NA>         1         2         2          7 21262641   800200.0
## 9     <NA>         3         1         3          4  8210281   329500.0
##   pop_year lastcensus gdp_year                    economy
## 0       NA       1979       NA  7. Least developed region
## 1       NA       1970       NA  7. Least developed region
## 2       NA       2001       NA       6. Developing region
## 3       NA       2010       NA       6. Developing region
## 4       NA       2010       NA    5. Emerging region: G20
## 5       NA       2001       NA       6. Developing region
## 6       NA         NA       NA       6. Developing region
## 7       NA         NA       NA       6. Developing region
## 8       NA       2006       NA 2. Developed region: nonG7
## 9       NA       2011       NA 2. Developed region: nonG7
##                income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3
## 0           5. Low income        NA    <NA>     AF    AFG    004   004
## 1  3. Upper middle income        NA    <NA>     AO    AGO    024   024
## 2  4. Lower middle income        NA    <NA>     AL    ALB    008   008
## 3 2. High income: nonOECD        NA    <NA>     AE    ARE    784   784
## 4  3. Upper middle income        NA    <NA>     AR    ARG    032   032
## 5  4. Lower middle income        NA    <NA>     AM    ARM    051   051
## 6 2. High income: nonOECD        NA    <NA>     AQ    ATA    010  <NA>
## 7 2. High income: nonOECD        NA    <NA>     TF    ATF    260  <NA>
## 8    1. High income: OECD        NA    <NA>     AU    AUS    036   036
## 9    1. High income: OECD        NA    <NA>     AT    AUT    040   040
##   wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb
## 0    AF   AFG     NA        AFG        AFG         NA         NA
## 1    AO   AGO     NA        AGO        AGO         NA         NA
## 2    AL   ALB     NA        ALB        ALB         NA         NA
## 3    AE   ARE     NA        ARE        ARE         NA         NA
## 4    AR   ARG     NA        ARG        ARG         NA         NA
## 5    AM   ARM     NA        ARM        ARM         NA         NA
## 6  <NA>  <NA>     NA        ATA        ATA         NA         NA
## 7  <NA>  <NA>     NA        ATF        ATF         NA         NA
## 8    AU   AUS     NA        AUS        AUS         NA         NA
## 9    AT   AUT     NA        AUT        AUT         NA         NA
##                 continent               region_un
## 0                    Asia                    Asia
## 1                  Africa                  Africa
## 2                  Europe                  Europe
## 3                    Asia                    Asia
## 4           South America                Americas
## 5                    Asia                    Asia
## 6              Antarctica              Antarctica
## 7 Seven seas (open ocean) Seven seas (open ocean)
## 8                 Oceania                 Oceania
## 9                  Europe                  Europe
##                   subregion                  region_wb name_len long_len
## 0             Southern Asia                 South Asia       11       11
## 1             Middle Africa         Sub-Saharan Africa        6        6
## 2           Southern Europe      Europe & Central Asia        7        7
## 3              Western Asia Middle East & North Africa       20       20
## 4             South America  Latin America & Caribbean        9        9
## 5              Western Asia      Europe & Central Asia        7        7
## 6                Antarctica                 Antarctica       10       10
## 7   Seven seas (open ocean)         Sub-Saharan Africa       22       35
## 8 Australia and New Zealand        East Asia & Pacific        9        9
## 9            Western Europe      Europe & Central Asia        7        7
##   abbrev_len tiny homepart                       geometry
## 0          4   NA        1 MULTIPOLYGON (((61.21082 35...
## 1          4   NA        1 MULTIPOLYGON (((16.32653 -5...
## 2          4   NA        1 MULTIPOLYGON (((20.59025 41...
## 3          6   NA        1 MULTIPOLYGON (((51.57952 24...
## 4          4   NA        1 MULTIPOLYGON (((-65.5 -55.2...
## 5          4   NA        1 MULTIPOLYGON (((43.58275 41...
## 6          4   NA        1 MULTIPOLYGON (((-59.57209 -...
## 7         10    2       NA MULTIPOLYGON (((68.935 -48....
## 8          4   NA        1 MULTIPOLYGON (((145.398 -40...
## 9          5   NA        1 MULTIPOLYGON (((16.97967 48...

africa <- dplyr::filter(world, region_un=='Africa') %>%
  dplyr::left_join(internet_usage %>% dplyr::select(
    `Country Code`, `2015 [YR2015]`
  ) %>% dplyr::rename(iso_a3=`Country Code`, internet.usage.2015=`2015 [YR2015]`),
  by = 'iso_a3') %>%
  st_transform(crs="+proj=laea +lon_0=18.984375")
## Warning: Column `iso_a3` joining character vector and factor, coercing into
## character vector

africa$internet.usage.2015
##  [1] 12.400000  4.866224  6.787703 11.387646 27.500000  4.563264 21.000000
##  [8] 20.680148  3.800000  7.615975 11.922431 38.200000 37.819383  1.083733
## [15] 11.600000 23.500000 23.478128  4.700000 17.119821  3.540707 21.320000
## [22] 45.622801  5.903868 19.016080 16.071708 57.080000  4.173972 10.336925
## [29]  9.000000 15.199127  9.298148 22.307015  2.220165 47.442550 18.000000
## [36]        NA 26.614929 17.929787 21.690264  2.500000        NA  1.760000
## [43] 30.382701  2.700000  7.120000 48.519836  5.355144 19.221100 51.919116
## [50] 21.000000 16.360000

africa.centers <- st_centroid(africa)

africa.spdf <- methods::as(africa, 'Spatial')
africa.spdf@data$id <- row.names(africa.spdf@data)

africa.tidy <- broom::tidy(africa.spdf)
## Regions defined for each Polygons
library(colormap)
library(widgetframe)
## Loading required package: htmlwidgets

Now we plot Internet usage

africa.tidy <- dplyr::left_join(africa.tidy, africa.spdf@data, by='id')

g <- ggplot(africa.tidy) +
  geom_polygon_interactive(
    color='black',
    aes(long, lat, group=group, fill=internet.usage.2015,
        tooltip=sprintf("%s<br/>%s",iso_a3,internet.usage.2015))) +
 hrbrthemes::theme_ipsum() +
  colormap::scale_fill_colormap(
    colormap=colormap::colormaps$copper, reverse = T) +
  labs(title='Internet Usage in Africa in 2015', subtitle='As Percent of Population',
       caption='Source: World Bank Open Data.')

g

###Reproduction of Poverty gap

poverty_gap <- read.csv("./data/poverty-gap-index.csv")

#unique(poverty_gap$Code)

length(unique(poverty_gap$Code))
## [1] 133

#For selection of one year we employ dplyr library

library(dplyr)
## 
## 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

npov <- filter(poverty_gap, Year==1992)

names(npov) <-   c("Entity", "Code",   "Year",   "Pov_1992")  

head(npov)
##                     Entity Code Year Pov_1992
## 1                Argentina  ARG 1992     1.18
## 2                  Bolivia  BOL 1992     4.81
## 3                   Brazil  BRA 1992     9.09
## 4                 Bulgaria  BGR 1992     0.00
## 5                  Burundi  BDI 1992    36.79
## 6 Central African Republic  CAF 1992    58.85

###Joining our world geospatial object with the poverty_gap non-spatial object

nworld <- left_join(world, npov, by = c('iso_a3' = 'Code'))
## Warning: Column `iso_a3`/`Code` joining character vector and factor,
## coercing into character vector
nworld %>%  st_transform(crs="+proj=laea +lon_0=18.984375")
## Simple feature collection with 177 features and 66 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -10326570 ymin: -10821730 xmax: 11718260 ymax: 12476540
## epsg (SRID):    NA
## proj4string:    +proj=laea +lat_0=0 +lon_0=18.984375 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs
## First 10 features:
##    scalerank      featurecla labelrank           sovereignt sov_a3
## 1          1 Admin-0 country         3          Afghanistan    AFG
## 2          1 Admin-0 country         3               Angola    AGO
## 3          1 Admin-0 country         6              Albania    ALB
## 4          1 Admin-0 country         4 United Arab Emirates    ARE
## 5          1 Admin-0 country         2            Argentina    ARG
## 6          1 Admin-0 country         6              Armenia    ARM
## 7          1 Admin-0 country         4           Antarctica    ATA
## 8          3 Admin-0 country         6               France    FR1
## 9          1 Admin-0 country         2            Australia    AU1
## 10         1 Admin-0 country         4              Austria    AUT
##    adm0_dif level              type                               admin
## 1         0     2 Sovereign country                         Afghanistan
## 2         0     2 Sovereign country                              Angola
## 3         0     2 Sovereign country                             Albania
## 4         0     2 Sovereign country                United Arab Emirates
## 5         0     2 Sovereign country                           Argentina
## 6         0     2 Sovereign country                             Armenia
## 7         0     2     Indeterminate                          Antarctica
## 8         1     2        Dependency French Southern and Antarctic Lands
## 9         1     2           Country                           Australia
## 10        0     2 Sovereign country                             Austria
##    adm0_a3 geou_dif                             geounit gu_a3 su_dif
## 1      AFG        0                         Afghanistan   AFG      0
## 2      AGO        0                              Angola   AGO      0
## 3      ALB        0                             Albania   ALB      0
## 4      ARE        0                United Arab Emirates   ARE      0
## 5      ARG        0                           Argentina   ARG      0
## 6      ARM        0                             Armenia   ARM      0
## 7      ATA        0                          Antarctica   ATA      0
## 8      ATF        0 French Southern and Antarctic Lands   ATF      0
## 9      AUS        0                           Australia   AUS      0
## 10     AUT        0                             Austria   AUT      0
##                                subunit su_a3 brk_diff
## 1                          Afghanistan   AFG        0
## 2                               Angola   AGO        0
## 3                              Albania   ALB        0
## 4                 United Arab Emirates   ARE        0
## 5                            Argentina   ARG        0
## 6                              Armenia   ARM        0
## 7                           Antarctica   ATA        0
## 8  French Southern and Antarctic Lands   ATF        0
## 9                            Australia   AUS        0
## 10                             Austria   AUT        0
##                      name                           name_long brk_a3
## 1             Afghanistan                         Afghanistan    AFG
## 2                  Angola                              Angola    AGO
## 3                 Albania                             Albania    ALB
## 4    United Arab Emirates                United Arab Emirates    ARE
## 5               Argentina                           Argentina    ARG
## 6                 Armenia                             Armenia    ARM
## 7              Antarctica                          Antarctica    ATA
## 8  Fr. S. Antarctic Lands French Southern and Antarctic Lands    ATF
## 9               Australia                           Australia    AUS
## 10                Austria                             Austria    AUT
##                      brk_name brk_group     abbrev postal
## 1                 Afghanistan      <NA>       Afg.     AF
## 2                      Angola      <NA>       Ang.     AO
## 3                     Albania      <NA>       Alb.     AL
## 4        United Arab Emirates      <NA>     U.A.E.     AE
## 5                   Argentina      <NA>       Arg.     AR
## 6                     Armenia      <NA>       Arm.    ARM
## 7                  Antarctica      <NA>       Ant.     AQ
## 8  Fr. S. and Antarctic Lands      <NA> Fr. S.A.L.     TF
## 9                   Australia      <NA>       Auz.     AU
## 10                    Austria      <NA>      Aust.      A
##                                               formal_en formal_fr
## 1                          Islamic State of Afghanistan      <NA>
## 2                           People's Republic of Angola      <NA>
## 3                                   Republic of Albania      <NA>
## 4                                  United Arab Emirates      <NA>
## 5                                    Argentine Republic      <NA>
## 6                                   Republic of Armenia      <NA>
## 7                                                  <NA>      <NA>
## 8  Territory of the French Southern and Antarctic Lands      <NA>
## 9                             Commonwealth of Australia      <NA>
## 10                                  Republic of Austria      <NA>
##    note_adm0                         note_brk
## 1       <NA>                             <NA>
## 2       <NA>                             <NA>
## 3       <NA>                             <NA>
## 4       <NA>                             <NA>
## 5       <NA>                             <NA>
## 6       <NA>                             <NA>
## 7       <NA> Multiple claims held in abeyance
## 8        Fr.                             <NA>
## 9       <NA>                             <NA>
## 10      <NA>                             <NA>
##                              name_sort name_alt mapcolor7 mapcolor8
## 1                          Afghanistan     <NA>         5         6
## 2                               Angola     <NA>         3         2
## 3                              Albania     <NA>         1         4
## 4                 United Arab Emirates     <NA>         2         1
## 5                            Argentina     <NA>         3         1
## 6                              Armenia     <NA>         3         1
## 7                           Antarctica     <NA>         4         5
## 8  French Southern and Antarctic Lands     <NA>         7         5
## 9                            Australia     <NA>         1         2
## 10                             Austria     <NA>         3         1
##    mapcolor9 mapcolor13  pop_est gdp_md_est pop_year lastcensus gdp_year
## 1          8          7 28400000    22270.0       NA       1979       NA
## 2          6          1 12799293   110300.0       NA       1970       NA
## 3          1          6  3639453    21810.0       NA       2001       NA
## 4          3          3  4798491   184300.0       NA       2010       NA
## 5          3         13 40913584   573900.0       NA       2010       NA
## 6          2         10  2967004    18770.0       NA       2001       NA
## 7          1         NA     3802      760.4       NA         NA       NA
## 8          9         11      140       16.0       NA         NA       NA
## 9          2          7 21262641   800200.0       NA       2006       NA
## 10         3          4  8210281   329500.0       NA       2011       NA
##                       economy              income_grp wikipedia fips_10
## 1   7. Least developed region           5. Low income        NA    <NA>
## 2   7. Least developed region  3. Upper middle income        NA    <NA>
## 3        6. Developing region  4. Lower middle income        NA    <NA>
## 4        6. Developing region 2. High income: nonOECD        NA    <NA>
## 5     5. Emerging region: G20  3. Upper middle income        NA    <NA>
## 6        6. Developing region  4. Lower middle income        NA    <NA>
## 7        6. Developing region 2. High income: nonOECD        NA    <NA>
## 8        6. Developing region 2. High income: nonOECD        NA    <NA>
## 9  2. Developed region: nonG7    1. High income: OECD        NA    <NA>
## 10 2. Developed region: nonG7    1. High income: OECD        NA    <NA>
##    iso_a2 iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us
## 1      AF    AFG    004   004    AF   AFG     NA        AFG        AFG
## 2      AO    AGO    024   024    AO   AGO     NA        AGO        AGO
## 3      AL    ALB    008   008    AL   ALB     NA        ALB        ALB
## 4      AE    ARE    784   784    AE   ARE     NA        ARE        ARE
## 5      AR    ARG    032   032    AR   ARG     NA        ARG        ARG
## 6      AM    ARM    051   051    AM   ARM     NA        ARM        ARM
## 7      AQ    ATA    010  <NA>  <NA>  <NA>     NA        ATA        ATA
## 8      TF    ATF    260  <NA>  <NA>  <NA>     NA        ATF        ATF
## 9      AU    AUS    036   036    AU   AUS     NA        AUS        AUS
## 10     AT    AUT    040   040    AT   AUT     NA        AUT        AUT
##    adm0_a3_un adm0_a3_wb               continent               region_un
## 1          NA         NA                    Asia                    Asia
## 2          NA         NA                  Africa                  Africa
## 3          NA         NA                  Europe                  Europe
## 4          NA         NA                    Asia                    Asia
## 5          NA         NA           South America                Americas
## 6          NA         NA                    Asia                    Asia
## 7          NA         NA              Antarctica              Antarctica
## 8          NA         NA Seven seas (open ocean) Seven seas (open ocean)
## 9          NA         NA                 Oceania                 Oceania
## 10         NA         NA                  Europe                  Europe
##                    subregion                  region_wb name_len long_len
## 1              Southern Asia                 South Asia       11       11
## 2              Middle Africa         Sub-Saharan Africa        6        6
## 3            Southern Europe      Europe & Central Asia        7        7
## 4               Western Asia Middle East & North Africa       20       20
## 5              South America  Latin America & Caribbean        9        9
## 6               Western Asia      Europe & Central Asia        7        7
## 7                 Antarctica                 Antarctica       10       10
## 8    Seven seas (open ocean)         Sub-Saharan Africa       22       35
## 9  Australia and New Zealand        East Asia & Pacific        9        9
## 10            Western Europe      Europe & Central Asia        7        7
##    abbrev_len tiny homepart    Entity Year Pov_1992
## 1           4   NA        1      <NA>   NA       NA
## 2           4   NA        1      <NA>   NA       NA
## 3           4   NA        1      <NA>   NA       NA
## 4           6   NA        1      <NA>   NA       NA
## 5           4   NA        1 Argentina 1992     1.18
## 6           4   NA        1      <NA>   NA       NA
## 7           4   NA        1      <NA>   NA       NA
## 8          10    2       NA      <NA>   NA       NA
## 9           4   NA        1      <NA>   NA       NA
## 10          5   NA        1      <NA>   NA       NA
##                          geometry
## 1  MULTIPOLYGON (((3897019 413...
## 2  MULTIPOLYGON (((-294686.1 -...
## 3  MULTIPOLYGON (((142770.1 45...
## 4  MULTIPOLYGON (((3333836 276...
## 5  MULTIPOLYGON (((-5003690 -7...
## 6  MULTIPOLYGON (((2183104 454...
## 7  MULTIPOLYGON (((-1509927 -8...
## 8  MULTIPOLYGON (((3831026 -56...
## 9  MULTIPOLYGON (((7426184 -79...
## 10 MULTIPOLYGON (((-163453.8 5...
head(nworld)
## Simple feature collection with 6 features and 66 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -73.41544 ymin: -55.25 xmax: 75.15803 ymax: 42.68825
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   scalerank      featurecla labelrank           sovereignt sov_a3 adm0_dif
## 1         1 Admin-0 country         3          Afghanistan    AFG        0
## 2         1 Admin-0 country         3               Angola    AGO        0
## 3         1 Admin-0 country         6              Albania    ALB        0
## 4         1 Admin-0 country         4 United Arab Emirates    ARE        0
## 5         1 Admin-0 country         2            Argentina    ARG        0
## 6         1 Admin-0 country         6              Armenia    ARM        0
##   level              type                admin adm0_a3 geou_dif
## 1     2 Sovereign country          Afghanistan     AFG        0
## 2     2 Sovereign country               Angola     AGO        0
## 3     2 Sovereign country              Albania     ALB        0
## 4     2 Sovereign country United Arab Emirates     ARE        0
## 5     2 Sovereign country            Argentina     ARG        0
## 6     2 Sovereign country              Armenia     ARM        0
##                geounit gu_a3 su_dif              subunit su_a3 brk_diff
## 1          Afghanistan   AFG      0          Afghanistan   AFG        0
## 2               Angola   AGO      0               Angola   AGO        0
## 3              Albania   ALB      0              Albania   ALB        0
## 4 United Arab Emirates   ARE      0 United Arab Emirates   ARE        0
## 5            Argentina   ARG      0            Argentina   ARG        0
## 6              Armenia   ARM      0              Armenia   ARM        0
##                   name            name_long brk_a3             brk_name
## 1          Afghanistan          Afghanistan    AFG          Afghanistan
## 2               Angola               Angola    AGO               Angola
## 3              Albania              Albania    ALB              Albania
## 4 United Arab Emirates United Arab Emirates    ARE United Arab Emirates
## 5            Argentina            Argentina    ARG            Argentina
## 6              Armenia              Armenia    ARM              Armenia
##   brk_group abbrev postal                    formal_en formal_fr note_adm0
## 1      <NA>   Afg.     AF Islamic State of Afghanistan      <NA>      <NA>
## 2      <NA>   Ang.     AO  People's Republic of Angola      <NA>      <NA>
## 3      <NA>   Alb.     AL          Republic of Albania      <NA>      <NA>
## 4      <NA> U.A.E.     AE         United Arab Emirates      <NA>      <NA>
## 5      <NA>   Arg.     AR           Argentine Republic      <NA>      <NA>
## 6      <NA>   Arm.    ARM          Republic of Armenia      <NA>      <NA>
##   note_brk            name_sort name_alt mapcolor7 mapcolor8 mapcolor9
## 1     <NA>          Afghanistan     <NA>         5         6         8
## 2     <NA>               Angola     <NA>         3         2         6
## 3     <NA>              Albania     <NA>         1         4         1
## 4     <NA> United Arab Emirates     <NA>         2         1         3
## 5     <NA>            Argentina     <NA>         3         1         3
## 6     <NA>              Armenia     <NA>         3         1         2
##   mapcolor13  pop_est gdp_md_est pop_year lastcensus gdp_year
## 1          7 28400000      22270       NA       1979       NA
## 2          1 12799293     110300       NA       1970       NA
## 3          6  3639453      21810       NA       2001       NA
## 4          3  4798491     184300       NA       2010       NA
## 5         13 40913584     573900       NA       2010       NA
## 6         10  2967004      18770       NA       2001       NA
##                     economy              income_grp wikipedia fips_10
## 1 7. Least developed region           5. Low income        NA    <NA>
## 2 7. Least developed region  3. Upper middle income        NA    <NA>
## 3      6. Developing region  4. Lower middle income        NA    <NA>
## 4      6. Developing region 2. High income: nonOECD        NA    <NA>
## 5   5. Emerging region: G20  3. Upper middle income        NA    <NA>
## 6      6. Developing region  4. Lower middle income        NA    <NA>
##   iso_a2 iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us
## 1     AF    AFG    004   004    AF   AFG     NA        AFG        AFG
## 2     AO    AGO    024   024    AO   AGO     NA        AGO        AGO
## 3     AL    ALB    008   008    AL   ALB     NA        ALB        ALB
## 4     AE    ARE    784   784    AE   ARE     NA        ARE        ARE
## 5     AR    ARG    032   032    AR   ARG     NA        ARG        ARG
## 6     AM    ARM    051   051    AM   ARM     NA        ARM        ARM
##   adm0_a3_un adm0_a3_wb     continent region_un       subregion
## 1         NA         NA          Asia      Asia   Southern Asia
## 2         NA         NA        Africa    Africa   Middle Africa
## 3         NA         NA        Europe    Europe Southern Europe
## 4         NA         NA          Asia      Asia    Western Asia
## 5         NA         NA South America  Americas   South America
## 6         NA         NA          Asia      Asia    Western Asia
##                    region_wb name_len long_len abbrev_len tiny homepart
## 1                 South Asia       11       11          4   NA        1
## 2         Sub-Saharan Africa        6        6          4   NA        1
## 3      Europe & Central Asia        7        7          4   NA        1
## 4 Middle East & North Africa       20       20          6   NA        1
## 5  Latin America & Caribbean        9        9          4   NA        1
## 6      Europe & Central Asia        7        7          4   NA        1
##      Entity Year Pov_1992                       geometry
## 1      <NA>   NA       NA MULTIPOLYGON (((61.21082 35...
## 2      <NA>   NA       NA MULTIPOLYGON (((16.32653 -5...
## 3      <NA>   NA       NA MULTIPOLYGON (((20.59025 41...
## 4      <NA>   NA       NA MULTIPOLYGON (((51.57952 24...
## 5 Argentina 1992     1.18 MULTIPOLYGON (((-65.5 -55.2...
## 6      <NA>   NA       NA MULTIPOLYGON (((43.58275 41...

Checking(and solve) if there are missing values (denoted as NA in R language Checking

nworld$Pov_1992
##   [1]    NA    NA    NA    NA  1.18    NA    NA    NA    NA    NA    NA
##  [12] 36.79    NA    NA    NA    NA  0.00    NA    NA    NA    NA  4.81
##  [23]  9.09    NA    NA    NA 58.85    NA    NA  1.48    NA  6.64    NA
##  [34]    NA    NA  4.48  4.83    NA    NA    NA    NA    NA    NA    NA
##  [45]  1.50    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
##  [56]    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
##  [67]    NA 12.48 10.80    NA    NA    NA    NA    NA    NA    NA    NA
##  [78]    NA    NA    NA    NA    NA    NA    NA  7.93    NA    NA    NA
##  [89]    NA    NA  4.83    NA    NA    NA    NA    NA    NA    NA    NA
## [100]    NA  3.28    NA  2.88    NA    NA    NA    NA    NA    NA    NA
## [111]    NA  0.10    NA    NA 34.12 27.37    NA    NA    NA    NA    NA
## [122]    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## [133]    NA    NA  0.30    NA    NA    NA    NA    NA    NA    NA    NA
## [144]    NA    NA    NA    NA    NA    NA  0.00    NA    NA    NA    NA
## [155]    NA    NA  1.13    NA    NA    NA  0.86    NA    NA    NA    NA
## [166] 28.61  0.68  0.15    NA    NA  2.79 14.95    NA    NA    NA    NA
## [177]    NA
nworld$Pov_1992[is.na(nworld$Pov_1992)] <- 0

###More processing on the spatial objects. Type ?sf::st_centroid in the console if you need help.

world.centers <- st_centroid(nworld)

world.spdf <- methods::as(nworld, 'Spatial')
world.spdf@data$id <- row.names(world.spdf@data)

world.tidy <- broom::tidy(world.spdf)
## Regions defined for each Polygons

####Joining tables, again:

world.tidy <- dplyr::left_join(world.tidy, world.spdf@data, by='id')
summary(world.tidy$Pov_1992)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   1.429   0.000  58.850

Now we Map

g <- ggplot(world.tidy) +
  geom_polygon_interactive(
    color='black',
    aes(long, lat, group=group, fill=(Pov_1992),
        tooltip=sprintf("%s<br/>%s",iso_a3,Pov_1992))) +
 hrbrthemes::theme_ipsum() +
  colormap::scale_fill_colormap(
    colormap=colormap::colormaps$freesurface_red, reverse = T) +
  labs(title='Poverty Gap in the World in 1992', subtitle='As Percent of Population',
       caption='Source: World Bank Open Data.')

g

USING PLOTLY

Getting started

Sys.setenv("plotly_username"="Mirii")
Sys.setenv("plotly_api_key"="hm0QKixOC2HPJDeKbzgQ")

options(browser = 'false')
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

plotting

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)

# specify map projection/options
g <- list(
  showframe = FALSE,
  showcoastlines = FALSE,
  projection = list(type = 'Mercator')
)

p <- plot_geo(df) %>%
  add_trace(
    z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., colors = 'Blues',
    text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
  ) %>%
  colorbar(title = 'GDP Billions US$', tickprefix = '$') %>%
  layout(
    title = '2014 Global GDP<br>Source:<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">CIA World Factbook</a>',
    geo = g
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="nchoropleth-ag")
## Found a grid already named: 'nchoropleth-ag Grid'. Since fileopt='overwrite', I'll try to update it
## Found a plot already named: 'nchoropleth-ag'. Since fileopt='overwrite', I'll try to update it

######creating chart link

chart_link