R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.3, PROJ 4.9.2
library(sp)
library(cartography)
library(rgdal)
library(rgeos)
## rgeos version: 0.4-2, (SVN revision (unknown))
##  GEOS runtime version: 3.5.1-CAPI-1.9.1 
##  Linking to sp version: 1.3-1 
##  Polygon checking: TRUE
library(rgdal)
download.file("http://biogeo.ucdavis.edu/data/diva/adm/DEU_adm.zip",
              destfile = "./data/DEU_adm.zip" , mode='wb')
unzip("./data/DEU_adm.zip", exdir = "./data")
spcol <- readOGR(dsn = "./data/DEU_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 = "Colombia's Departments")

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

Load data

library(cartography)
# Load data
data(nuts2006)

# 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 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 a layer with the extent of the EU28 countries with only a background color

##population map

# Layout plot
layoutLayer(title = "Most Populated Countries in Europe", # title of the map
            author = "Author: Your name here",  # 
            sources = "Sources: Please give credit", # 
            scale = NULL, # no scale
            col = NA, # no color for the title box 
            coltitle = "black", # color of the title
            frame = FALSE,  # no frame around the map
            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 = "#F0B27A",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)

###demographics trend:

# 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

###3. Interactive Mapping

library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
## Loading required package: 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
library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
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
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
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:rgeos':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
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') 
## Warning: Column `iso_a3` joining character vector and factor, coercing into
## character vector
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
head(africa)
## Simple feature collection with 6 features and 64 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -2664406 ymin: -2941948 xmax: 1306017 ymax: 1690992
## 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
##   scalerank      featurecla labelrank               sovereignt sov_a3
## 1         1 Admin-0 country         3                   Angola    AGO
## 2         1 Admin-0 country         6                  Burundi    BDI
## 3         1 Admin-0 country         5                    Benin    BEN
## 4         1 Admin-0 country         3             Burkina Faso    BFA
## 5         1 Admin-0 country         4                 Botswana    BWA
## 6         1 Admin-0 country         4 Central African Republic    CAF
##   adm0_dif level              type                    admin adm0_a3
## 1        0     2 Sovereign country                   Angola     AGO
## 2        0     2 Sovereign country                  Burundi     BDI
## 3        0     2 Sovereign country                    Benin     BEN
## 4        0     2 Sovereign country             Burkina Faso     BFA
## 5        0     2 Sovereign country                 Botswana     BWA
## 6        0     2 Sovereign country Central African Republic     CAF
##   geou_dif                  geounit gu_a3 su_dif                  subunit
## 1        0                   Angola   AGO      0                   Angola
## 2        0                  Burundi   BDI      0                  Burundi
## 3        0                    Benin   BEN      0                    Benin
## 4        0             Burkina Faso   BFA      0             Burkina Faso
## 5        0                 Botswana   BWA      0                 Botswana
## 6        0 Central African Republic   CAF      0 Central African Republic
##   su_a3 brk_diff                 name                name_long brk_a3
## 1   AGO        0               Angola                   Angola    AGO
## 2   BDI        0              Burundi                  Burundi    BDI
## 3   BEN        0                Benin                    Benin    BEN
## 4   BFA        0         Burkina Faso             Burkina Faso    BFA
## 5   BWA        0             Botswana                 Botswana    BWA
## 6   CAF        0 Central African Rep. Central African Republic    CAF
##               brk_name brk_group abbrev postal                   formal_en
## 1               Angola      <NA>   Ang.     AO People's Republic of Angola
## 2              Burundi      <NA>   Bur.     BI         Republic of Burundi
## 3                Benin      <NA>  Benin     BJ           Republic of Benin
## 4         Burkina Faso      <NA>   B.F.     BF                Burkina Faso
## 5             Botswana      <NA>   Bwa.     BW        Republic of Botswana
## 6 Central African Rep.      <NA> C.A.R.     CF    Central African Republic
##   formal_fr note_adm0 note_brk                name_sort name_alt mapcolor7
## 1      <NA>      <NA>     <NA>                   Angola     <NA>         3
## 2      <NA>      <NA>     <NA>                  Burundi     <NA>         2
## 3      <NA>      <NA>     <NA>                    Benin     <NA>         1
## 4      <NA>      <NA>     <NA>             Burkina Faso     <NA>         2
## 5      <NA>      <NA>     <NA>                 Botswana     <NA>         6
## 6      <NA>      <NA>     <NA> Central African Republic     <NA>         5
##   mapcolor8 mapcolor9 mapcolor13  pop_est gdp_md_est pop_year lastcensus
## 1         2         6          1 12799293     110300       NA       1970
## 2         2         5          8  8988091       3102       NA       2008
## 3         2         2         12  8791832      12830       NA       2002
## 4         1         5         11 15746232      17820       NA       2006
## 5         5         7          3  1990876      27060       NA       2011
## 6         6         6          9  4511488       3198       NA       2003
##   gdp_year                   economy             income_grp wikipedia
## 1       NA 7. Least developed region 3. Upper middle income        NA
## 2       NA 7. Least developed region          5. Low income        NA
## 3       NA 7. Least developed region          5. Low income        NA
## 4       NA 7. Least developed region          5. Low income        NA
## 5       NA      6. Developing region 3. Upper middle income        NA
## 6       NA 7. Least developed region          5. Low income        NA
##   fips_10 iso_a2 iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is
## 1    <NA>     AO    AGO    024   024    AO   AGO     NA        AGO
## 2    <NA>     BI    BDI    108   108    BI   BDI     NA        BDI
## 3    <NA>     BJ    BEN    204   204    BJ   BEN     NA        BEN
## 4    <NA>     BF    BFA    854   854    BF   BFA     NA        BFA
## 5    <NA>     BW    BWA    072   072    BW   BWA     NA        BWA
## 6    <NA>     CF    CAF    140   140    CF   CAF     NA        CAF
##   adm0_a3_us adm0_a3_un adm0_a3_wb continent region_un       subregion
## 1        AGO         NA         NA    Africa    Africa   Middle Africa
## 2        BDI         NA         NA    Africa    Africa  Eastern Africa
## 3        BEN         NA         NA    Africa    Africa  Western Africa
## 4        BFA         NA         NA    Africa    Africa  Western Africa
## 5        BWA         NA         NA    Africa    Africa Southern Africa
## 6        CAF         NA         NA    Africa    Africa   Middle Africa
##            region_wb name_len long_len abbrev_len tiny homepart
## 1 Sub-Saharan Africa        6        6          4   NA        1
## 2 Sub-Saharan Africa        7        7          4   NA        1
## 3 Sub-Saharan Africa        5        5          5   NA        1
## 4 Sub-Saharan Africa       12       12          4   NA        1
## 5 Sub-Saharan Africa        8        8          4   NA        1
## 6 Sub-Saharan Africa       20       24          6   NA        1
##   internet.usage.2015                       geometry
## 1           12.400000 MULTIPOLYGON (((-294686.1 -...
## 2            4.866224 MULTIPOLYGON (((1148567 -49...
## 3            6.787703 MULTIPOLYGON (((-1799514 69...
## 4           11.387646 MULTIPOLYGON (((-2387715 10...
## 5           27.500000 MULTIPOLYGON (((712546.6 -2...
## 6            4.563264 MULTIPOLYGON (((-409783.7 8...
africa.spdf <- methods::as(africa, 'Spatial')
africa.spdf@data$id <- row.names(africa.spdf@data)

africa.tidy <- broom::tidy(africa.spdf)
library(htmlwidgets)
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are *required* to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(colormap)
library(widgetframe)

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

##Our World in Data `

poverty_gap <- read.csv("/cloud/project/poverty-gap-index-at-190-int--per-day.csv")

#unique(poverty_gap$Code)

length(unique(poverty_gap$Code))
## [1] 133
library(dplyr)
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
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...
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
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
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
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

Sys.setenv("plotly_username"="charaf21")
Sys.setenv("plotly_api_key"="0rNBd7HDHp8WglsUTqdf")
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
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_link3 = api_create(p, filename="nchoropleth-ag3")
## Found a grid already named: 'nchoropleth-ag3 Grid'. Since fileopt='overwrite', I'll try to update it
## Found a plot already named: 'nchoropleth-ag3'. Since fileopt='overwrite', I'll try to update it
chart_link3
urlfile2 <- "https://raw.github.com/amcrisan/EpiDesignPattern/master/data/ebola_metadata.RDS"
myfile <- download.file(urlfile2,"./datos/ebola_metadata.RDS", method="auto")
## Warning in download.file(urlfile2, "./datos/ebola_metadata.RDS", method =
## "auto"): URL https://raw.github.com/amcrisan/EpiDesignPattern/master/data/
## ebola_metadata.RDS: cannot open destfile './datos/ebola_metadata.RDS',
## reason 'No such file or directory'
## Warning in download.file(urlfile2, "./datos/ebola_metadata.RDS", method =
## "auto"): download had nonzero exit status