title: “R Notebook” output: html_notebook —
As a first step we load the required librarys which should be installed in advance:
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)
Downloading, extracting and reading the vector data for the selected country:
download.file ("http://biogeo.ucdavis.edu/data/diva/adm/LKA_adm.zip", destfile = ".//LKA_adm.zip" , mode='wb')
unzip (".//LKA_adm.zip", exdir = "C://Users//david//Desktop//geomatics")
spcol <- readOGR(dsn = "C://Users//david//Desktop//geomatics//LKA_adm1.shp", verbose = FALSE)
class(spcol)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
Choosing html colors and adding a title for the plot:
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 = "Sri Lanka´s Departments")
library(sf)
sfcol <- st_read(dsn = "C://Users//david//Desktop//geomatics//LKA_adm1.shp", quiet = TRUE)
class(sfcol)
## [1] "sf" "data.frame"
Using the cartography package with geospatial data for europe:
data(nuts2006)
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)
# Layout plot
layoutLayer(title = "3 Least 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 = "#A0B27B",border = "white", lwd = 1, add = TRUE)
# Selection of the 3 least populated countries of Europe
dflab <- nuts0.df[order(nuts0.df$pop2008, decreasing = FALSE),][1:3,]
# Label creation
dflab$lab <- paste(dflab$id, "\n", round(dflab$pop2008/100000,1), "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.9, # size of the labels
font = 2) # label font
# Add an explanation text
text(x = 5477360, y = 4177311, labels = "The 3 least populated countries of Europe
Total population 2008 [millions]", cex = 0.7, adj = 0)
# 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
library(bitops)
urlfile <- "https://raw.github.com/bhaskarvk/user2017.geodataviz/master/inst/extdata/africa-internet_usage-2015.csv"
internet_usage <- read.csv(urlfile)
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')
africa2= 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
africa2$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
#the joint was successful
africa3=africa2 %>% st_transform(crs="+proj=laea +lon_0=18.984375") ## add the right crs from spatialreference.org(for example for asia)
africa.centers <- st_centroid(africa3)
africa.spdf <- methods::as(africa3, 'Spatial')
africa.spdf@data$id <- row.names(africa.spdf@data)
africa.tidy <- broom::tidy(africa.spdf)
library(htmlwidgets)
library(hrbrthemes)
hrbrthemes::import_roboto_condensed()
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
Sys.setenv("plotly_username"="david.sena")
Sys.setenv("plotly_api_key"="7MjIFPXAp5F6ftToXRxt")
options(browser = 'false')
library(plotly)
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
h <- 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 = h
)
# 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
p
chart_link
library(leaflet)
plt <- leaflet() %>%
setView(lat = 50.935173, lng = 6.953101, zoom=13) %>%
addTiles(group="OSM") %>%
addProviderTiles(providers$CartoDB.DarkMatter, group="Dark") %>%
addProviderTiles(providers$CartoDB.Positron, group="Light") %>%
addLayersControl(baseGroups=c('OSM','Dark','Light'))
plt
urlfile2 <- "https://raw.github.com/amcrisan/EpiDesignPattern/master/data/ebola_metadata.RDS"
myfile <- download.file(urlfile2,"/cloud/project/C:/Users/david/Desktop/geomatics/ebola_metadata.RDS", method="auto")
metadata <- readRDS("/cloud/project/C:/Users/david/Desktop/geomatics/ebola_metadata.RDS")
library(ape)
#library(ggtree)
library(lubridate)
library(tidyr)
library(dplyr)
library(ggmap)
library(RColorBrewer)
library(dygraphs)
library(xts)
library(leaflet)
# First, we need to create some new data
# Counting number cases per country
aggDat<-metadata %>%
filter(Country !="?") %>%
group_by(Country,country_lon,country_lat) %>%
dplyr::count()%>%
mutate(popup=sprintf("%s = %d cases",Country,n)) #create a popup for the map
# Here's a very quick look at what this command generates for us:
aggDat
## # A tibble: 3 x 5
## # Groups: Country, country_lon, country_lat [3]
## Country country_lon country_lat n popup
## <fct> <dbl> <dbl> <int> <chr>
## 1 GIN -9.70 9.95 367 GIN = 367 cases
## 2 LBR -9.43 6.43 45 LBR = 45 cases
## 3 SLE -11.8 8.46 313 SLE = 313 cases
# Now, we'll define the color palette
pal <- colorFactor(
palette = c('red', 'blue', 'green', 'purple', 'orange'),
domain = aggDat$Country)
# Now, we'll create the Map
# This first command will creat an empty map
m<-leaflet(aggDat)
m %>%
addTiles()%>%
addCircleMarkers(
lng=~country_lon,
lat= ~country_lat,
radius=~sqrt(n)*2,
color = ~pal(Country),
stroke = FALSE, fillOpacity = 0.7,
label=~as.character(popup),
labelOptions = labelOptions(noHide = T),
options = leafletOptions(minZoom = 0, maxZoom = 10,scroolWheelZoom=FALSE))
aggDat<-metadata %>%
filter(Country !="?") %>%
group_by(Country,Region,region_lon,region_lat) %>%
dplyr::count()%>%
mutate(popup=sprintf("%s (%s) = %d cases",Region,Country,n))
m<-leaflet(aggDat)
m %>%
addTiles()%>%
addCircleMarkers(
lng=~region_lon,
lat= ~region_lat,
radius=~sqrt(n)*2,
color = ~pal(Country), #we actually colour the points by country here
stroke = FALSE, fillOpacity = 0.7,
label=~as.character(popup),
labelOptions = labelOptions(noHide = F))
m<-leaflet(metadata)
# by providing the region latitude and longtitude co-ordinates we allow clustering of regional samples
m %>%
addTiles()%>%
addCircleMarkers(
lng=~region_lon,
lat= ~region_lat,
stroke = FALSE, fillOpacity = 0.5,
clusterOptions= markerClusterOptions(titile="regional clusters") #cluster options
)
Additional Content:
library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
library(bitops)
file <- "/cloud/project/C:/Users/david/Desktop/geomatics/economic-inequality-gini-index.csv"
economic_inequality <- read.csv(file)
unique(economic_inequality$Code)
## [1] ALB DZA AGO ARG ARM AUS AUT
## [8] AZE BGD BLR BEL BLZ BEN BTN
## [15] BOL BIH BWA BRA BGR BFA BDI
## [22] CMR CAN CPV CAF TCD CHL CHN
## [29] COL COM COG CRI CIV HRV CYP
## [36] CZE COD DNK DJI DOM ECU EGY
## [43] SLV EST ETH FJI FIN FRA GAB
## [50] GMB GEO DEU GHA GRC GTM GIN
## [57] GNB GUY HTI HND HUN ISL IND
## [64] IDN IRN IRQ IRL ISR ITA JAM
## [71] JPN JOR KAZ KEN KIR OWID_KOS KGZ
## [78] LAO LVA LBN LSO LBR LTU LUX
## [85] MKD MDG MWI MYS MDV MLI MRT
## [92] MUS MEX FSM MDA MNG MNE MAR
## [99] MOZ MMR NAM NPL NLD NIC NER
## [106] NGA NOR PAK PSE PAN PNG PRY
## [113] PER PHL POL PRT ROU RUS RWA
## [120] LCA WSM STP SEN SRB SYC SLE
## [127] SVK SVN SLB ZAF KOR SSD ESP
## [134] LKA SDN SUR SWZ SWE CHE SYR
## [141] TJK TZA THA TLS TGO TON TTO
## [148] TUN TUR TKM TUV UGA UKR GBR
## [155] USA URY UZB VUT VEN VNM YEM
## [162] ZMB ZWE
## 163 Levels: AGO ALB ARG ARM AUS AUT AZE BDI BEL BEN BFA BGD BGR BIH ... ZWE
length(unique(economic_inequality$Code))
## [1] 163
library(dplyr)
fpov <- filter(economic_inequality, Year==2014, Entity=='Argentina' )
names(economic_inequality) <- c("Entity", "Code", "Year", "GINI")
head(economic_inequality)
## Entity Code Year GINI
## 1 Albania ALB 1996 27.0
## 2 Albania ALB 2002 31.7
## 3 Albania ALB 2005 30.6
## 4 Albania ALB 2008 30.0
## 5 Albania ALB 2012 29.0
## 6 Algeria DZA 1988 40.2
world2 <- filter(world, admin=='Argentina')
fworld <- left_join(world2, economic_inequality, by = c('iso_a3' = 'Code'))
fworld %>% st_transform(crs="+proj=laea +lon_0=18.984375")
## Simple feature collection with 27 features and 66 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -8055781 ymin: -7262960 xmax: -5003690 ymax: -3161631
## 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 adm0_dif level
## 1 1 Admin-0 country 2 Argentina ARG 0 2
## 2 1 Admin-0 country 2 Argentina ARG 0 2
## 3 1 Admin-0 country 2 Argentina ARG 0 2
## 4 1 Admin-0 country 2 Argentina ARG 0 2
## 5 1 Admin-0 country 2 Argentina ARG 0 2
## 6 1 Admin-0 country 2 Argentina ARG 0 2
## 7 1 Admin-0 country 2 Argentina ARG 0 2
## 8 1 Admin-0 country 2 Argentina ARG 0 2
## 9 1 Admin-0 country 2 Argentina ARG 0 2
## 10 1 Admin-0 country 2 Argentina ARG 0 2
## type admin adm0_a3 geou_dif geounit gu_a3 su_dif
## 1 Sovereign country Argentina ARG 0 Argentina ARG 0
## 2 Sovereign country Argentina ARG 0 Argentina ARG 0
## 3 Sovereign country Argentina ARG 0 Argentina ARG 0
## 4 Sovereign country Argentina ARG 0 Argentina ARG 0
## 5 Sovereign country Argentina ARG 0 Argentina ARG 0
## 6 Sovereign country Argentina ARG 0 Argentina ARG 0
## 7 Sovereign country Argentina ARG 0 Argentina ARG 0
## 8 Sovereign country Argentina ARG 0 Argentina ARG 0
## 9 Sovereign country Argentina ARG 0 Argentina ARG 0
## 10 Sovereign country Argentina ARG 0 Argentina ARG 0
## subunit su_a3 brk_diff name name_long brk_a3 brk_name brk_group
## 1 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 2 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 3 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 4 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 5 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 6 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 7 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 8 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 9 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## 10 Argentina ARG 0 Argentina Argentina ARG Argentina <NA>
## abbrev postal formal_en formal_fr note_adm0 note_brk name_sort
## 1 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 2 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 3 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 4 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 5 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 6 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 7 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 8 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 9 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## 10 Arg. AR Argentine Republic <NA> <NA> <NA> Argentina
## name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est gdp_md_est
## 1 <NA> 3 1 3 13 40913584 573900
## 2 <NA> 3 1 3 13 40913584 573900
## 3 <NA> 3 1 3 13 40913584 573900
## 4 <NA> 3 1 3 13 40913584 573900
## 5 <NA> 3 1 3 13 40913584 573900
## 6 <NA> 3 1 3 13 40913584 573900
## 7 <NA> 3 1 3 13 40913584 573900
## 8 <NA> 3 1 3 13 40913584 573900
## 9 <NA> 3 1 3 13 40913584 573900
## 10 <NA> 3 1 3 13 40913584 573900
## pop_year lastcensus gdp_year economy
## 1 NA 2010 NA 5. Emerging region: G20
## 2 NA 2010 NA 5. Emerging region: G20
## 3 NA 2010 NA 5. Emerging region: G20
## 4 NA 2010 NA 5. Emerging region: G20
## 5 NA 2010 NA 5. Emerging region: G20
## 6 NA 2010 NA 5. Emerging region: G20
## 7 NA 2010 NA 5. Emerging region: G20
## 8 NA 2010 NA 5. Emerging region: G20
## 9 NA 2010 NA 5. Emerging region: G20
## 10 NA 2010 NA 5. Emerging region: G20
## income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3
## 1 3. Upper middle income NA <NA> AR ARG 032 032
## 2 3. Upper middle income NA <NA> AR ARG 032 032
## 3 3. Upper middle income NA <NA> AR ARG 032 032
## 4 3. Upper middle income NA <NA> AR ARG 032 032
## 5 3. Upper middle income NA <NA> AR ARG 032 032
## 6 3. Upper middle income NA <NA> AR ARG 032 032
## 7 3. Upper middle income NA <NA> AR ARG 032 032
## 8 3. Upper middle income NA <NA> AR ARG 032 032
## 9 3. Upper middle income NA <NA> AR ARG 032 032
## 10 3. Upper middle income NA <NA> AR ARG 032 032
## wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb
## 1 AR ARG NA ARG ARG NA NA
## 2 AR ARG NA ARG ARG NA NA
## 3 AR ARG NA ARG ARG NA NA
## 4 AR ARG NA ARG ARG NA NA
## 5 AR ARG NA ARG ARG NA NA
## 6 AR ARG NA ARG ARG NA NA
## 7 AR ARG NA ARG ARG NA NA
## 8 AR ARG NA ARG ARG NA NA
## 9 AR ARG NA ARG ARG NA NA
## 10 AR ARG NA ARG ARG NA NA
## continent region_un subregion region_wb
## 1 South America Americas South America Latin America & Caribbean
## 2 South America Americas South America Latin America & Caribbean
## 3 South America Americas South America Latin America & Caribbean
## 4 South America Americas South America Latin America & Caribbean
## 5 South America Americas South America Latin America & Caribbean
## 6 South America Americas South America Latin America & Caribbean
## 7 South America Americas South America Latin America & Caribbean
## 8 South America Americas South America Latin America & Caribbean
## 9 South America Americas South America Latin America & Caribbean
## 10 South America Americas South America Latin America & Caribbean
## name_len long_len abbrev_len tiny homepart Entity Year GINI
## 1 9 9 4 NA 1 Argentina 1980 40.8
## 2 9 9 4 NA 1 Argentina 1986 42.8
## 3 9 9 4 NA 1 Argentina 1987 45.3
## 4 9 9 4 NA 1 Argentina 1991 46.8
## 5 9 9 4 NA 1 Argentina 1992 45.5
## 6 9 9 4 NA 1 Argentina 1993 44.9
## 7 9 9 4 NA 1 Argentina 1994 45.9
## 8 9 9 4 NA 1 Argentina 1995 48.9
## 9 9 9 4 NA 1 Argentina 1996 49.5
## 10 9 9 4 NA 1 Argentina 1997 49.1
## geometry
## 1 MULTIPOLYGON (((-5003690 -7...
## 2 MULTIPOLYGON (((-5003690 -7...
## 3 MULTIPOLYGON (((-5003690 -7...
## 4 MULTIPOLYGON (((-5003690 -7...
## 5 MULTIPOLYGON (((-5003690 -7...
## 6 MULTIPOLYGON (((-5003690 -7...
## 7 MULTIPOLYGON (((-5003690 -7...
## 8 MULTIPOLYGON (((-5003690 -7...
## 9 MULTIPOLYGON (((-5003690 -7...
## 10 MULTIPOLYGON (((-5003690 -7...
fworld$economic_inequality
## NULL
fworld$economic_inequality
## NULL
fworld$GINI[is.na(fworld$GINI)] <- 0
world.centers <- st_centroid(fworld)
world.spdf <- methods::as(fworld, 'Spatial')
world.spdf@data$id <- row.names(world.spdf@data)
world.tidy <- broom::tidy(world.spdf)
world.tidy <- dplyr::left_join(world.tidy, world.spdf@data, by='id')
summary(world.tidy$GINI)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 40.80 44.50 46.80 47.19 49.80 53.80
f <- ggplot(world.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=(GINI),
tooltip=sprintf("%s<br/>%s",iso_a3,GINI))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$freesurface_red, reverse = T) +
labs(title='Economic inequality in Argentina 2014', subtitle='GINI index',
caption='Source: World Bank Open Data.')
f