---
title: "Preparing plots for presentation"
output: html_notebook
---

```{r}
library(tidyverse)
library(gapminder)
```


#Labeling 
Do you remember ``scale_x_....(name = ....)``? Here is a shortcut.


```{r}
gapminder %>% ggplot(mapping = aes(x = gdpPercap, y = lifeExp, color = continent, alpha = year)) + 
  geom_point() +
  labs(
    title = paste("Life Expectancy by GDP per Capita","BLABLA belongs on a new line", sep = "\n"),
    subtitle = "roughly observing time",
    caption = "Data from gapminder.org",
    x = "GDP per Capita",
    y = "Life Expectancy", 
    color = "Continent",
    alpha = "decades since 1960"
  )
```
## Synonyms for naming axes

```{r}
gapminder %>% ggplot(mapping = aes(x = gdpPercap, y = lifeExp, color = continent, alpha = year)) + 
  geom_point() +
  xlab(label = "GDP per Capita") +
  ylab("Life Expectancy") +
  ggtitle(label = paste("Life Expectancy by GDP per Capita",
                        "BLABLA belongs on a new line", sep = "\n"),
          subtitle = "roughly observing time") # but you cannot make caption 

```



# Add label wherever in the plot

Get the four observations in gapminder with the highest GDP per capita.

```{r}
richest <- gapminder %>% top_n(n = 4, wt = gdpPercap)  
```


```{r}
gapminder %>% ggplot(mapping = aes(x = gdpPercap, y = lifeExp, 
                                   color = continent, 
                                   alpha = year)) + 
  geom_point() +
  annotate(geom = "text", label = "These are the richest", 
           #x = 95000,
           #y = 42,
           x = max(gapminder$gdpPercap) - 6000
           ,
           y = max(gapminder$lifeExp) - 10 
           ,
           size =  2 )

```

Now label the richest countries

```{r}
gapminder %>% ggplot(mapping = aes(x = gdpPercap, y = lifeExp, 
                                   color = continent, 
                                   alpha = year)) + 
  geom_point() +
  annotate(geom = "text", label = "These are the richest", 
           x = max(gapminder$gdpPercap) - 6000,
           y = max(gapminder$lifeExp) - 10 , 
           size = 2) +
  geom_label(data = richest, 
             aes(label = paste(country, year, sep = "-"), 
                 alpha = year), 
             position = position_nudge(x = -5000, y = -4 )
             ) 
```
## The `ggrepel` library
To create labels

```{r}
library(ggrepel)
gapminder %>% ggplot(mapping = aes(x = gdpPercap, y = lifeExp, 
                                   color = continent 
                                  )) + 
  geom_point(aes(alpha = year)) +
  annotate(geom = "text", label = "These are the richest", 
           x = max(gapminder$gdpPercap) - 18000,
           y = max(gapminder$lifeExp) - 5 , 
           size = 4) +
  ggrepel::geom_label_repel(data = richest, 
             aes(label = paste(country, year, sep = "-")
                 ) 
  )
```

# Add other geoms

```{r}
gapminder %>% filter(country %in% c("France", "Germany")) %>%
    ggplot(mapping = aes(x = year, y = lifeExp, color = country)) + 
   geom_point()  +
  annotate(geom = "rect",
           xmin = c(1960, 2004),
           xmax = c(1975, 2010),
           ymin = c(70, 78),
           ymax = c(73, 82),
           color = c("red","blue"),
           linetype = c(1,3),
           size = c(3, 0.5),
           fill = c("red", "blue") ,
           alpha = c(0.2, 0.2)) #+
  # annotate(geom = "segment", x = 1995, xend = 2003, y = 80, yend = 80,
  #          arrow = arrow(length = unit(0.3, "cm"), 
  #                        type = "closed",
  #                        ends = "last"
  #                        #,ends = "first"
  #                        ),
  #          lineend = "round",
  #          color = "blue", 
  #          linetype = 3) +
  # annotate(geom = "segment", x = 1990, xend = 1975.5, y = 70, yend = 71.5, 
  #          arrow = arrow(length = unit(0.3, "cm"), type = "closed", 
  #                        ends = "both"),
  #          color = "red", 
  #          linetype = 1) +
  # annotate(geom = "text", label = "a big difference", color = "red", 
  #          x = 1990, y = 70,
  #          hjust = "left", # this means that the x and y values will be 
  #          #to the left 
  #          vjust = "bottom") + #and bottom of the label
  # annotate(geom = "text", label = "a small difference", color = "blue",
  #          x = 1995, y = 80,
  #          hjust = "right", # this means that the x and y values will be
  #          #to the right 
  #          vjust = "bottom") #and bottom of the label

```

# Theme

## Built-in themes
functions starting with `theme_`: `dark`, `bw`, `minimal`, `classic`,...

```{r}
gapminder %>% filter(country %in% c("France", "Germany")) %>%
    ggplot(mapping = aes(x = year, y = lifeExp, color = country)) + 
  geom_point() +
  theme_dark()
```
## Theme elements & functions
NB: much has changed since the Ggplot2 book in hardcopy! (Elegant Graphics for 
Data Analysis by H. Wickham, Springer, 2009)


What they affect:

  - axis:
    - line, text, ticks, title, 
  - legend:
    - background, key, text, title
  - panel
    - background, border, grid
  - plot 
    - background, title
  - strip 
    - text

Functions:
  - `element_text`
  - `element_line`
  - `element_rect`
  - `element_blank`
  - (`element_render`, `element_grob` )
  
```{r}
gapminder %>% filter(country %in% c("France", "Germany")) %>%
    ggplot(mapping = aes(x = year, y = lifeExp, color = country)) + 
  geom_point() +
  scale_x_continuous(breaks = gapminder$year) +
   theme(text = element_text(family = "garamond", face = "bold.italic"),
        axis.title.x = element_text(family = "courier", #titles of all axes and the plot title
                             size = rel(1.2), face = "plain"),
        axis.text.x = element_text(angle = 50
                                    , vjust =  0.3  #help says between 0 and 1 but it's not true
                               ),

        )+
  ggtitle(label = "Life Expectancy in Germany and France 1950-2007") 
```


# Coordinates 

## `coord_flip()`

```{r}
gapminder %>% filter(country %in% c("France", "Germany")) %>%
    ggplot(mapping = aes(x = year, y = lifeExp, fill = country)) + 
  geom_bar(stat="identity") + 
  scale_x_continuous(breaks = gapminder$year) +
  theme(axis.title.x = element_text(color = "green"))
```


```{r}
gapminder %>% filter(country %in% c("France", "Germany")) %>%
    ggplot(mapping = aes(x = year, y = lifeExp, fill = country)) + 
  geom_bar(stat="identity") + 
  scale_x_continuous(breaks = gapminder$year) + coord_flip() +
  theme(axis.title.x = element_text(color = "green"))
```







