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

library("pander")
library("gapminder")
library("tidyverse")
library("ggthemes")
library("ggalt")
library("directlabels")
library("forcats")

```

#### Graphic 1: Population in Southeast Asia from 1952 to 2002

SEA is a region dear to my heart so I chose to subset and filter the data to just focus on SEA countries. Since 1952 to 2002 is a period of time where there was a lot of peril in Southeast Asia, I wondered if this had an effect on population, life expectancy and GDP. 

```{r graph 1}
# Prepare data
seac <- c("Cambodia", "Thailand", "Singapore", "Vietnam", "Philippines", "Myanmar",
          "Malaysia", "Indonesia") 

sea <- gapminder %>% 
  filter(year > 1951 & year < 2005 & country %in% seac
         )
seapop <- ggplot(data = sea, aes(x = year, y = pop, color = country)) +
    geom_point() +
    xlab("Year") + ylab("Population") +
    ggtitle("Population over time for Southeast Asian Countries") + 
    scale_color_manual(name = "SEA nations",
                       values = c("Cambodia" = "#2ca25f", "Indonesia" = "#8856a7", 
                                  "Malaysia" = "#2b8cbe", "Myanmar" = "#feb24c", 
                                  "Philippines" = "#c994c7", "Singapore" = "#dd1c77", 
                                  "Thailand" = "#999999", "Vietnam" = "#5ab4ac" )) +
    theme_bw()

seapop    

ggsave("SEA populations.pdf", plot = seapop, height = 3, width = 5, units = "in")

```

#### Graphic 2: GDP per capita in Southeast Asia from 1950 to early 2000s
For Southeast asian nations, I used log scales to show the growth in GDP over time. I used a smooth curve to model the growth, instead of straight lines. The gray bar around it shows confidence intevals. While all the countries were experiencing GDP growth at relatively constant rates that time, it is interesting that the Philippines sincethe 1980s has experienced slower growth in relation to other countries.

```{r graph 2}
seagdp <- ggplot(data = sea, aes(x = year, y = gdpPercap, color = country)) +
    geom_smooth() +
    xlab("Year") + ylab("GDP") +
    ggtitle("GDP over time for Southeast Asian Countries") + 
    scale_y_log10() +
    scale_color_manual(name = "SEA nations",
                       values = c("Cambodia" = "#2ca25f", "Indonesia" = "#8856a7", 
                                  "Malaysia" = "#2b8cbe", "Myanmar" = "#feb24c", 
                                  "Philippines" = "#c994c7", "Singapore" = "#dd1c77", 
                                  "Thailand" = "#999999", "Vietnam" = "#5ab4ac")) +
    xlim(1950,2015) +
    geom_dl(aes(label = country), cex = 0.5, method = list(dl.trans(x = x + .3), 
                                                           "last.bumpup")) +
    theme_classic() 
    
seagdp

```

####Graphic 3: A correlational exploration between GDP levels and life expectancy in Southeast asian countries over the time period 1952 to 2002. 
This graphic uses small multiples to show changes in life expectancy as it correlates to GDP. Each dot represents a point in time, over 5 year intevals. 

```{r graph 3}

seapt1 <- ggplot(data = sea, aes(x = lifeExp, y = gdpPercap, color = country)) +
    geom_point() +
    xlab("Life expectancy") + ylab("GDP") +
    ggtitle("GDP and Life Expectancy in Southeast Asian Countries") + 
    scale_color_manual(name = "SEA nations",
                       values = c("Cambodia" = "#2ca25f", "Myanmar" = "#feb24c", 
                                  "Indonesia" = "#8856a7", 
                                  "Malaysia" = "#2b8cbe",  
                                  "Philippines" = "#c994c7", "Singapore" = "#dd1c77", 
                                  "Thailand" = "#999999", "Vietnam" = "#5ab4ac")) +
    facet_grid(country ~ ., scales = "free") + 
    scale_y_continuous(expand = c(0.3, 0)) +
    theme_minimal() + 
    theme(strip.background = element_blank(), 
          strip.text = element_blank()
          )

seapt1  

#How do you ORDER these comparison by life expectancy/ GDP? 
#scales #pretty breaks
```

#### Graphic 4: GDP and Population correlations in Southeast Asia, 1952-2002
This is a population and GDP correlation scatterplot using log scales due to the immense Indonesian population and the rapid growth in Singapore between 1952 and 2002. Between Indonesia and Singapore, the plots are overall also inverse of each other, with Singapore having a tiny population and incredibly high GDP growth over the years as well as Indonesia having a huge population growth but not in term of GDP. 

```{r graph 4}
seapop <- ggplot(data = sea, aes(x =gdpPercap , y = pop , color = country)) +
    geom_point() +
    xlab("GDP") + ylab("pop") +
    ggtitle("GDP and Population in Southeast Asian Countries") + 
    scale_color_manual(name = "SEA nations",
                       values = c("Cambodia" = "#2ca25f", "Myanmar" = "#feb24c", 
                                  "Indonesia" = "#8856a7", 
                                  "Malaysia" = "#2b8cbe",  
                                  "Philippines" = "#c994c7", "Singapore" = "#dd1c77", 
                                  "Thailand" = "#999999", "Vietnam" = "#5ab4ac")) +
    facet_wrap(~country) +
    scale_y_log10() +
    scale_x_log10()+
    theme_minimal()

seapop
```

#### Graphic 5: GDP growth in Southeast Asia between years 1952-2002
I used a geom_dumpbell to try to show the change in growth over time with the x-axis in log scales because Singapore is such a big outlier. 

One of the things I am struggling with is how to order the factor data so that it offers better visuals for comparison. 

```{r graph 5}

sea2 <- gapminder %>% 
        filter(year %in% c(1952, 2002) & country %in% seac) %>%
        mutate(country = fct_reorder(country, gdpPercap, last)) %>%
        group_by(country) %>%
        mutate(gdpgrowth = (gdpPercap - lag(gdpPercap)) / lag(gdpPercap),
               gdp52 = first(gdpPercap),
               gdp02 = last(gdpPercap)) %>%
        slice(2) %>% select(-year)

seagdpD <- ggplot(sea2, aes(x= gdp52, xend = gdp02, y= country, yend = country, color = country)) +   
          geom_dumbbell(colour_x = "black", colour_xend = "black", dot_guide_size = 0.4) +
          scale_color_manual(name = "SEA nations", 
                             values = c("Cambodia" = "#2ca25f", "Myanmar" = "#feb24c", 
                                        "Indonesia" = "#8856a7", "Malaysia" = 
                                          "#2b8cbe","Philippines" = "#c994c7", 
                                        "Singapore" = "#dd1c77", "Thailand" = 
                                          "#999999", "Vietnam" = "#5ab4ac")) +
          geom_text(label="1952", vjust = 2, size=3, aes(x=gdp52, y=country)) +
          geom_text(label="2002", vjust = 2, size=3, aes(x=gdp02, y=country)) +
          labs(title ="GDP growth from 1952 to 2002", y = "Country in SEA", x = "GDP growth from 1952 to 2002") +
          scale_x_log10() +
          theme_minimal() +
          theme(legend.position = "none")
seagdpD  

  
```
