---
title: "Getting Started with ggplot2"
author: "Ahmed Alawami"
output:
  word_document:
    toc: yes
    toc_depth: '2'
  html_notebook:
    toc: yes
    toc_depth: 2
    toc_float:
      collapsed: no
  html_document:
    toc: yes
    toc_depth: '2'
---

# Overview

In this document we will go over some of the basics to get up and running with ggplot2.

# Setup

## Load libraries

```{r, message=FALSE, warning=FALSE}
library(ggplot2)
library(gapminder)
```

## Check out the data

```{r}
head(gapminder)
```

```{r}
str(gapminder)
```

```{r}
summary(gapminder)
```

# Construct a data scatter plot

So let us start out by plotting the life expectance per year for the united states.

## Using base R
```{r}
dat <- subset(gapminder, country == "United States")
plot(lifeExp ~ year, dat)
```

## Using ggplot2
```{r}
ggplot(dat, aes(year, lifeExp)) +
  geom_point()
```

Now we will add a line to the plot.

## Using base R
```{r}
plot(lifeExp ~ year, dat, type = "b")
```

or

```{r}
plot(lifeExp ~ year, dat, type = "l")
points(dat$year, dat$lifeExp)
```


## Using ggplot2
```{r}
ggplot(dat, aes(year, lifeExp)) +
  geom_point() +
  geom_line()
```

# Working with a more countries

```{r}
dat <- subset(gapminder, country %in% c("United States", "Canada", "Mexico", "Germany", "Norway", "China", "Russia"))

ggplot(dat, aes(year, lifeExp, color = country)) +
  geom_line()
```

## Fix labels

```{r}
ggplot(dat, aes(year, lifeExp, color = country)) +
  geom_line() +
  geom_point() +
  xlab("Year") +
  ylab("Life Expectancy")
```

## Add Title

```{r}
ggplot(dat, aes(year, lifeExp, color = country)) +
  geom_line() +
  geom_point() +
  xlab("Year") +
  ylab("Life Expectancy") +
  ggtitle("Life Expectancy from 1952 to 2007")
```

## Alternatively

```{r}
ggplot(dat, aes(year, lifeExp, color = country)) +
  geom_line() +
  geom_point() +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy")
```

## Change the plot limits

```{r, warning = FALSE}
ggplot(dat, aes(year, lifeExp, color = country)) +
  geom_line() +
  geom_point() +
  labs(title = "Life Expectancy from 1970 to 2007", x = "Year", y = "Life Expectancy") +
  xlim(c(1970, 2007)) +
  ylim(c(60, 82))
```

# Plotting the entire data

## A simple scatter plot
```{r}
ggplot(gapminder, aes(year, lifeExp, color = continent)) +
  geom_point() +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy")
```

## Facetting
```{r}
ggplot(gapminder, aes(year, lifeExp, color = continent)) +
  geom_point() +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy") +
  facet_wrap(~continent)
```

## Boxplot
```{r}
p <- ggplot(gapminder, aes(factor(year), lifeExp, color = continent)) +
  geom_boxplot() +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy") +
  facet_wrap(~continent)
p
```

```{r, echo = FALSE}
 ggplot(gapminder, aes(factor(year), lifeExp, color = continent)) +
   geom_boxplot() +
   labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy") +
   facet_wrap(~continent) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
```

## One Fix
```{r}
ggplot(gapminder, aes(factor(year), lifeExp, color = continent)) +
  geom_boxplot() +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy") +
  facet_wrap(~continent) +
  coord_flip()
```


# Fit Linear Model to the Data

```{r}
ggplot(gapminder, aes(year, lifeExp, color = continent)) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Life Expectancy from 1952 to 2007", x = "Year", y = "Life Expectancy")
```

# When in a rush

```{r}
qplot(factor(gapminder$year), gapminder$lifeExp, color = factor(gapminder$year), geom = "boxplot")
```
