---
title: "Intro ggplot2: Where to write data and aesthetics; scale functions vs. other scale-editing functions; color palettes"
output: html_notebook
---

```{r message = FALSE}
library(tidyverse)
library(gapminder)
```

```{r}
glimpse(gapminder)
```

# Data

```{r}
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) + 
  geom_point() 
```
## synonyms - where to put data
```{r}
gapminder %>% 
ggplot(mapping = aes(x = gdpPercap, y = lifeExp)) + 
  geom_point() 
```

```{r}
ggplot( mapping = aes(x = gdpPercap, y = lifeExp)) + 
  geom_point(data = gapminder) 
```

## filter in pipe and saving plot to variable
```{r}
p <- gapminder %>% filter(continent == "Oceania", year == 1952) %>%
  ggplot(mapping = aes(x = gdpPercap, y = lifeExp)) + 
  geom_point()
```

```{r}
p
```

```{r}
summary(p)
```
```{r}
str(p)
```


# Aesthetic Scales


```{r}
ggplot(data = gapminder, mapping = aes(y = lifeExp, x = gdpPercap, color = continent)) + 
  geom_point()
```
## synonym where to put aesthetic mapping

```{r}
ggplot(data = gapminder) + 
  geom_point(mapping = aes(y = lifeExp, x = gdpPercap, color = continent))
```
```{r}
ggplot(data = gapminder, mapping = aes(y = lifeExp, x = gdpPercap, 
                                       color = continent, size = pop)) + 
  geom_point( )
```

When aesthetic scales are not mapped to data

```{r}
ggplot(data = gapminder, mapping = aes(y = lifeExp, x = gdpPercap)) + 
  geom_point( color = "red", alpha = 0.1)
```

## Why move data and aesthetics around

We are particularly interested in American countries
```{r}
Americas <- gapminder %>% filter(continent == "Americas", year == 2007)
world <- gapminder %>% filter(continent != "Americas",  year == 2007)
AmericasPlot <- ggplot() + 
  geom_point(data = world, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_label(data = Americas, mapping = aes(x = gdpPercap, y = lifeExp, label = country), size = 3, alpha = 0.5 ) 
AmericasPlot  
```
SAVE YOUR PLOT!
```{r}
ggsave(filename = "Americas_plotted_by_me.png", AmericasPlot)
ggsave(filename = "Americas_plotted_by_me.pdf") # default plot to be saved is the last plot produced
```


## Scale functions
Each scale has a few parameters, which are accessible through the corresponding function

```{r fig.width=10, fig.height=10}
AmeriPlot2 <- AmericasPlot + 
  scale_x_continuous(name = "GDP per Capita",
                     breaks = seq(from=0, to = max(gapminder$gdpPercap), by = 5000),
                     minor_breaks = seq(from=0, to = 10000, by = 2500)) +
  scale_y_continuous(name = "Life Expectancy",
                     breaks = seq(from = round(min(gapminder$lifeExp)), to = round(max(gapminder$lifeExp)), by = 2)
                     #,limits = c(0, 85)
                    ) +
  scale_color_discrete(labels = c("AFRICA", "ASIA", "EUROPE", "AUSTRALIA & NEW ZEALAND"), name = "Names of Continents") #+
# scale_color_brewer(type = "qual", palette = "Dark2") 
                     
AmeriPlot2  
                     
                     
```


```{r}
library(RColorBrewer)
RColorBrewer::brewer.pal.info
```
```{r}
RColorBrewer::display.brewer.all()
```


## from ?ggplot::shape
```{r}
# A look at all 25 symbols
df2 <- data.frame(x = 1:5 , y = 1:25, z = 1:25)
p <- ggplot(df2, aes(x, y))
p + geom_point(aes(shape = z), size = 4) +
  scale_shape_identity()
```


### What we (possibly) cannot do with `scale_` functions:
There are some additional functions for that. 

- Add title to the entire plot
- Edit layout of legend(s)


```{r}
AmeriPlot3 <- AmeriPlot2 +
ggtitle(label = "Americas' Life Expectancy by GDP per Capita", 
        subtitle = "A comparison with the rest of the world") 
AmeriPlot3
```


```{r}
AmeriPlot4 <- AmeriPlot3 +
guides(color = guide_legend(title = "CONTINENT NAME", #we had edited it before!
                              title.position = "bottom",
                            nrow = 2))
AmeriPlot4
```















