---
output: 
  html_document:
    toc: no
    toc_float: yes
    theme: sandstone
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,message = FALSE,warning = FALSE)
```

```{r}
library(giscourse)
library(DT)
library(shiny)
library(RColorBrewer)


library(leaflet)
library(leaflet.extras)

library(rgdal)


library(dplyr)
library(ggplot2)
library(plotly)
library(RPostgreSQL)
library(rpostgis)
library(sf)
library(mapview)
library(WDI)

# drv <- dbDriver("PostgreSQL")

# con<-dbConnect(drv, dbname = "clima2",host="postgis",user="docker",password="docker")



mapviewOptions(basemaps =c("Esri.WorldImagery",
                             "OpenStreetMap",
                            "NASAGIBS.ViirsEarthAtNight2012",
                             "Esri.WorldStreetMap",
                             "CartoDB.Positron",
                             "OpenTopoMap",
                             "OpenStreetMap.BlackAndWhite",
                             "OpenStreetMap.HOT",
                             "Stamen.TonerLite",
                             "Esri.WorldShadedRelief",
                             "Stamen.Terrain", "OpenRailwayMap"))

```

```{r}
# countries<-st_read(con,query="select names, iso3166 iso2c, geom from countries")
# d <- WDI(country="all", indicator=c("NY.GDP.PCAP.CD", "SP.POP.TOTL", "SP.DYN.LE00.IN","EG.EGY.PRIM.PP.KD","NV.AGR.TOTL.ZS"), start=1900, end=2018, extra = TRUE)
# names(d)[4:8]<- c("GDP_per_capita", "Population_total", "Life_expectancy","Energy_intensity_MJ_GDP","Agriculture_percent_GDP") ## Set the names
# write.csv(d,file="wdi.csv")

```

# Analysing global development {.tabset}

## Raw data


```{r,results='hide'}
countries<-st_read("countries.shp")
library(RColorBrewer)
library(cartogram)

```


```{r}
#mapviewOptions(basemaps =c("OpenStreetMap","Esri.WorldImagery", "CartoDB.DarkMatter","OpenTopoMap", "OpenStreetMap.BlackAndWhite", "OpenStreetMap.HOT","Stamen.TonerLite"))

d<-read.csv("wdi.csv")
d$Population_total<-round(d$Population_total/1000000,0)
d$GDP_per_capita<-round(d$GDP_per_capita,0)
d$Life_expectancy <-round(d$Life_expectancy,0)
d %>% filter(region !="Aggregates") -> d 
d %>% filter(year%in% c(2015)) ->dd

pal1<-brewer.pal(8, "YlOrRd")
pal2<-brewer.pal(8, "YlGnBu")[8:1]
pal3<-brewer.pal(8, "YlOrRd")[8:1]

st_as_sf(merge(dd,countries)) %>% select("names","Population_total","GDP_per_capita","Life_expectancy","geometry") %>%filter(!is.na(Population_total))->Country


```

```{r}
library(aqm)
d %>% filter(Population_total> 10) %>% arrange(country,year) %>% select("country","year", "Population_total","GDP_per_capita","Life_expectancy") %>% dt()
```

## 2015 map


```{r}



mapview(Country,zcol="Population_total", at =c(0, 1,10, 50,100,200,500,1000,2000),legend=TRUE,col.regions = pal1) + mapview(Country,zcol="GDP_per_capita",at=c(0,100,500,1000,5000,10000,30000,100000,200000),col.regions=pal2,legend=TRUE) + mapview(Country,zcol="Life_expectancy",at=c(0,40,50,60,65,70,75,80,90),col.regions=pal3,legend=TRUE)-> mp



mp@map %>% addFullscreenControl() %>% addWMSTiles(group="Population centers",
                "http://r.bournemouth.ac.uk:8083/AG/wms",
                layers = "AG:GHS_POB",
                options = WMSTileOptions(format = "image/png", transparent = TRUE)) %>% mapview:::mapViewLayersControl (names = c("Population centers")) %>% hideGroup("Population centers") %>% hideGroup("Country - GDP_per_capita") %>% hideGroup("Country - Life_expectancy")
```



## 1975

```{r}
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(1975)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy)) +
  geom_point(aes(size = Population_total, fill = region, label=country),shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(40, 85)) + scale_x_continuous(limits = c(100, 125000, breaks=c(100,1000,10000,100000)))+
  scale_size(range = c(1, 10)) + geom_smooth(method="lm",se=FALSE,col="black") +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10() + theme_bw()+ theme(axis.text.x=element_text(angle=45,hjust=1)) -> g2
ggplotly(g2)

```

## 1990

```{r}
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(1990)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy)) +
  geom_point(aes(size = Population_total, fill = region, label=country),shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(40, 85)) + scale_x_continuous(limits = c(100, 125000), breaks=c(100,1000,10000,100000)) +
  scale_size(range = c(1, 10)) + geom_smooth(method="lm",se=FALSE,col="black") +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10() + theme_bw()+  theme(axis.text.x=element_text(angle=45,hjust=1)) -> g3

ggplotly(g3)

```

## 2000

```{r}
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(2000)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy)) +
  geom_point(aes(size = Population_total, fill = region, label=country),shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(40, 85)) + scale_x_continuous(limits = c(100, 125000), breaks=c(100,1000,10000,100000)) +
  scale_size(range = c(1, 10)) + geom_smooth(method="lm",se=FALSE,col="black") +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10() + theme_bw()+  theme(axis.text.x=element_text(angle=45,hjust=1)) -> g4
ggplotly(g4)

```

## 2010

```{r}
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(2010)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy)) +
  geom_point(aes(size = Population_total, fill = region, label=country),shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(40, 85)) + scale_x_continuous(limits = c(100, 125000), breaks=c(100,1000,10000,100000)) +
  scale_size(range = c(1, 10)) + geom_smooth(method="lm",se=FALSE,col="black") +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10() + theme_bw()+  theme(axis.text.x=element_text(angle=45,hjust=1)) -> g5
ggplotly(g5)

```



## 2015

```{r}
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(2015)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy)) +
  geom_point(aes(size = Population_total, fill = region, label=country),shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(40, 85)) + scale_x_continuous(limits = c(100, 125000), breaks=c(100,1000,10000,100000))+
  scale_size(range = c(1, 10)) + geom_smooth(method="lm",se=FALSE,col="black") +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10() + theme_bw()+  theme(axis.text.x=element_text(angle=45,hjust=1)) -> g6
ggplotly(g6)

```


## Combined figure

```{r}
library(plotly)
d %>% filter(region !="Aggregates") -> d ## Filter out the aggregated totals
d %>% filter(year%in% c(1975,1990,2015)) %>% ggplot( aes(x = GDP_per_capita, y = Life_expectancy, size = Population_total, fill = region, label=country)) +
  geom_point(shape = 21) +
  labs(x = "GDP per Capita(USD)", y = "Life Expectancy(years)") +
  scale_y_continuous(limits = c(50, 90)) +
  scale_size(range = c(1, 10)) +scale_x_continuous(limits = c(100, 125000), breaks=c(100,1000,10000,100000)) +
  labs(size = "Population(Millions)", fill = "Region") + scale_x_log10()  + theme_bw() +facet_wrap("year") +  theme(axis.text.x=element_text(angle=45,hjust=1)) -> g1
ggplotly(g1)
```



## Google map

### Find all the countries with populations above 75 million

```{r}
library(googleway)

key <- "AIzaSyA3yXWsYuxhN62iHM3w-1sf_OGalbaoz0s"


set_key( key )
library(sf)
dd<-st_transform(Country,4326)
dd %>% filter(Population_total>75) -> dd
dd$Population_total<-paste(round(dd$Population_total,0),"million")
google_map(data=st_centroid(dd)) %>% add_markers(opacity=0.1, mouse_over="Population_total", title="names") 
```

## Attribution

The idea for the data presentation was provided by the work of Hans Roslin, who died in 2017. Professor Roslin was considered to be one of the greatest data communicator of all time. Roslin's skill was an ability to select informative ways of displaying large data sets in order to engage the audience. There are many videos of his talks on You Tube and Ted talks.

https://www.youtube.com/watch?v=jbkSRLYSojo

Roslin's original figures were constructed by a team of data analysts, programmers and analysts working with the professor. The data used by the professor is loaded on our BU geoserver and can be analysed by students. This is a demonstration of how large global data sets can be analysed visually using R code.  


