# Introduce R Markdown
# Installing and loading packages

Install R packages
```{r}
install.packages("tidyverse")
install.packages("gapminder")
install.packages("ggridges")
install.packages("ggrepel")
install.packages("viridis")
install.packages("RColorBrewer")
install.packages("ggsci")
install.packages("plotly")
install.packages("gganimate")
install.packages("gifski")
install.packages("htmlwidget")
```


Load R packages
```{r warning = FALSE, message = FALSE}
library(tidyverse) # a collection of data manipulation and visualization packages
library(gapminder) # package with a gapminder dataset
library(ggridges) # extension of ggplot2 that makes ridgeline plots
library(ggrepel) # extension of ggplot2 that makes non-overlapping labels
library(viridis) # color palette package
library(RColorBrewer) # color palette package
library(ggsci) # color palette package
library(plotly) # package for interactive data visualizations
library(gganimate) # package for producing gifs, extension of ggplot2
library(gifski) # package for converting video frames to GIF animations
library(htmlwidgets) # save interactive plot as an html file
```


# Preparing the data

Import data from the built-in "gapminder" dataset
```{r}
countries <- gapminder # create a data frame
head(countries) #see the first six rows of the dataframe
```


Check the data structure
```{r}
str(countries)
```


# Data manipulation with `dplyr`

## Filter function

Filter data of countries from 1997 and with Oceania continent
```{r}
filter(countries, year==1997 & continent=="Oceania") # Use "&" for AND condition
#assign to obeject as needed 
```


Filter observations from year 2002 or year 2007
```{r}
filter(countries, year==2002 | year==2007 ) # Use "|" for OR condition
```

Filter observations for years 1977 to 2007

```{r}
filter(countries, year %in% c(1977:2007)) #use the %in% operator to indicate you want all values from a list or range of values
```

## Summarise function

Available sub-functions:

* Center: mean(), median()
* Spread: sd(), IQR(), mad()
* Range: min(), max(), quantile()
* Position: first(), last(), nth(),
* Count: n(), n_distinct()
* Logical: any(), all()


Find average value of the lifeExp variable
```{r}
summarise(countries, mean_lifeExp=mean(lifeExp))
#can add as many arguments as needed 
```


## Piping with multiple functions

What if you want to perform multiple functions in R? Use Pipe operator (%>%) in the dplyr package. It allows you to perform multiple functions without using nested parentheses.


Piping structure

Object %>%
    function to execute first %>%
        function to execute second %>%
            function to execute third
            
```{r}
#e.g.


countries %>%
  head(n = 2)  

#VS 
head(countries, n = 2)

```

## Group_by function

Group_by function allows you to do operations by groups. E.g. find maximum life expectancy by continent
```{r}
countries %>%
  group_by(continent) %>%
  summarise(max_lifeExp=max(lifeExp))
```

Assigning an object with piping 

```{r}
lifeExpData <- countries %>%
  group_by(continent) %>%
  summarise(max_lifeExp=max(lifeExp)) 

lifeExpData
```

##PRACTICE 

summarize minimum petal length (Petal.Length) by species (Species) in the iris data set, using piping. Assign it to a new object called 'minPL'

```{r}
data(iris)
names(iris)

minPL <- iris %>% 
  group_by(Species) %>%
  summarise(minPetalLength = min(Petal.Length)) 

minPL

```
# Data visualization with `ggplot2`

Basic ggplot structure: 

data %>%
ggplot(aes(x, y)) + 
  geom +
  additional geoms + 
  thematic arguments 

## Barchart

Shows the distribution of a categorical variable
```{r}
countries %>%
  ggplot(aes(x=continent))+
    geom_bar()
```

Add unique colour to each continent using fill function
```{r}
countries %>%
  ggplot(aes(x=continent, fill = continent))+
    geom_bar()
```


Add title, subtitle, x and y labels, and caption
```{r}
countries %>%
  ggplot(aes(x=continent, fill=continent))+
    geom_bar()+
    labs(title="Distribution of observations by continents", 
         subtitle = "Measured as counts", 
         x="Continents",
         y="Number of observations",
         caption = "Source: Gapminder data")
```

Make a relative barchart by modifying y-values to measure the percentage of observations
```{r}
countries %>%
  ggplot(aes(x=continent, fill=continent))+
    geom_bar(aes(y=stat(count)/sum(count)))+
    labs(title="Data distribution by continents", 
         x="Continents",
         y="Percent of observations")


```


Remove legend using `guides` command
```{r}
countries %>%
  ggplot(aes(x=continent, fill=continent))+
    geom_bar(aes(y=stat(count)/sum(count)))+
    labs(title="Data distribution by continents", 
         x="Continents",
         y="Percent of observations")+
    guides(fill = "none")
```


## Histograms

Visualizes the distribution of one quantitative variable
```{r}
countries %>%
  ggplot(aes(x=lifeExp))+
    geom_histogram()+
    labs(title="Histogram of life expenctancy ", 
         x="Life expectancy",
         y="Number of observations")
```


Use light-blue fill color with a black border
```{r}
countries %>%
  ggplot(aes(x=lifeExp))+
    geom_histogram(color="black", fill="lightblue")+
    labs(title="Histogram of life expenctancy ", 
         x="Life expectancy",
         y="Number of observations")
```


Change bin-width from 2 to 4
```{r}
countries %>%
  ggplot(aes(x=lifeExp))+
    geom_histogram(binwidth=4, color="black", fill="lightblue")+
    labs(title="Histogram of life expenctancy ", 
         x="Life expectancy",
         y="Number of observations")
```


## Density plots


Visualizes the distribution of a quantitative variable
```{r}
countries %>%
  ggplot(aes(x=lifeExp))+
    geom_density()+
    labs(title="Density plot of life expenctancy", 
         x="Life expectancy")
```

Contrasting lifeExp distributions of continents using density plots
```{r}
countries %>%
  ggplot(aes(x=lifeExp, fill=continent))+
    geom_density()+
    labs(title="Density plot of life expenctancy by     continent ", 
    x="Life expectancy")
```


Change alpha to make it more transparent, alpha measures color saturation (0 to 1)
```{r}
countries %>%
  ggplot(aes(x=lifeExp, fill=continent))+
    geom_density(alpha=0.3)+
    labs(title="Density plot of life expenctancy by continent ", 
         x="Life expectancy")
```


Or make a ridgeline plot using`geom_density_ridges` function (from `ggridges` package)
```{r}
countries %>%
  ggplot(aes(x=lifeExp, y=continent, fill=continent))+
    geom_density_ridges(alpha=0.7)+
    labs(title="Distribution of life expenctancy by continent ", 
         x="Life expectancy")
```


## Boxplots

Boxplots are used to compare distributions of one quantitative variable across multiple categories (a visualization alternative to density plots)


Make a boxplot of life expectancy
```{r}
countries %>%
  ggplot(aes(y=lifeExp))+
    geom_boxplot()+
    labs(title="Boxplots of life expenctancy",
         y="Life expectancy")
```


Comparing distribution of lifeExp variable by continent
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_boxplot()+
    labs(title="Boxplots of life expenctancy by   continent",
         x="Continent",
         y="Life expectancy")
```


Reorder continents from smallest to highest life expectancy
```{r}
countries %>%
  ggplot(aes(x=reorder(continent,lifeExp), y=lifeExp, fill=continent))+
    geom_boxplot()+
    labs(title="Boxplots of life expenctancy by continent",
         x="Continent",
         y="Life expectancy")
```


Change the name of your legend title to "Continent"
```{r}
countries %>%
  ggplot(aes(x=reorder(continent,lifeExp), y=lifeExp, fill=continent))+
    geom_boxplot()+
    labs(title="Boxplots of life expenctancy by continent",
         x="Continent",
         y="Life expectancy",
         fill="Continent")
```


Add average life expectancy values to each boxplot
```{r}
countries %>%
  ggplot(aes(x=reorder(continent,lifeExp), y=lifeExp, fill=continent))+
    geom_boxplot()+
    labs(title="Boxplots of life expenctancy by continent",
         x="Continent",
         y="Life expectancy",
         fill="Continent")+
    stat_summary(fun=mean, geom="point", shape=18, size=4)
```


## Violin charts

Make a violin plot of life expectancy grouped by continent
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent", 
         y="Life expectancy")
```

##PRACTICE

Using the iris dataset, create a violin plot of petal length, grouped by species, with appropriate x and y axis labels. 

```{r}
data(iris)
names(iris)

iris %>% 
  ggplot(aes(x= Species, y = Petal.Length, fill = Species)) + 
  geom_violin() + 
  labs(x = "Species", y = "Petal length") 

```

## Small multiples

### `facet_wrap` function

Using `facet_wrap` function to break down each violin chart by year
```{r}

countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent", 
         y="Life expectancy")+
    facet_wrap(~year) +
  #rotate axis text 
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```


## Scatterplots


Scatterplots show the relationship between two quantitative variables
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point()+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="GDP per capita", 
         y="Life expectancy")
```
##PRACTICE

(1) Filter countries to just countries on the following continents: Europe, Asia, Africa. (2) Create a scatter plot of population on the x axis and gdpPercap on the y axis (3) Add separate colors for each continent.

HINT: This is similar to what we did for the violin plot but you will use a colour argument instead of a fill argument. 


```{r}

countries %>%
filter(continent %in% c('Africa', 'Asia', 'Europe')) %>%
  ggplot(aes(x=gdpPercap, y=lifeExp, colour = continent)) +
    geom_point()+
    labs(title="Scatterplot of GDP per capita and life expectancy",
         x="GDP per capita", 
         y="Life expectancy")

```


Change the size of data points to measure the size of population
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent, size=pop))+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="GDP per capita", 
         y="Life expectancy")
```


### Transforming x and y values

Transform the gdpPercap variable using a `scale_x_log10()`
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop))+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_x_log10() +
    scale_x_continuous()
```


Specify ranges for x and y values using `xlim` and `ylim` functions
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop))+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_x_log10()+
    xlim(1000,10000)+
    ylim(50,70)
```


### Best fit lines

Add a best fit line to the scatterplot
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop))+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_x_log10()+
    geom_smooth(method="lm", se=FALSE)
```


Add a best fit line for each continent and increase transparency for points
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop), alpha=0.3)+
    labs(title="Scatterplot of GDP per capita and life expectancy", 
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_x_log10()+
    geom_smooth(aes(color=continent),method="lm", se=FALSE)
```


###PRACTICE 

Modify the code below to produce a seperate plot for each continent. HINT: Use facetting. 
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop), alpha=0.3)+
    labs(title="Best-fit lines between gdpPercap(log10) and lifeExp for each continent",
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_x_log10()+
    geom_smooth(aes(color= continent), method="lm",se=FALSE)+
    facet_wrap(~continent)
```


## Labels

Use `geom_text` function to add labels your observations
```{r}
countries %>%
  filter(year==2007, continent=="Europe") %>%
    ggplot(aes(x=gdpPercap, y=lifeExp))+
      geom_point(aes(size=pop), color="blue", alpha=0.3)+
      geom_text(aes(label=country), size = 3)+
      labs(title="GDP per capita and life expectancy in Europe in 2007",
           x="GDP per capita", 
           y="Life expectancy")
```

Use `geom_text_repel` function (from `ggrepel` package) to make sure labels do not overlap
```{r}
countries %>%
  filter(year==2007, continent=="Europe") %>%
    ggplot(aes(x=gdpPercap, y=lifeExp))+
      geom_point(aes(size=pop), color="blue", alpha=0.3)+
      geom_text_repel(aes(label=country), size = 3)+
      labs(title="GDP per capita and life expectancy in Europe in 2007",
           x="GDP per capita", 
           y="Life expectancy")
```

## Themes

  - `theme_gray()` - the signature ggplot2 theme with a grey background and white gridlines, designed to put the data forward yet make comparisons easy.
  - `theme_bw()` - the classic dark-on-light ggplot2 theme. May work better for presentations displayed with a projector.
  - `theme_linedraw()` - a theme with only black lines of various widths on white backgrounds, reminiscent of a line drawing. Serves a purpose similar to theme_bw. Note that this theme has some very thin lines (<< 1 pt) which some journals may refuse.
  - `theme_light()` - a  theme similar to theme_linedraw but with light grey lines and axes, to direct more attention towards the data.
  - `theme_dark()` - the dark cousin of theme_light, with similar line sizes but a dark background. Useful to make thin coloured lines pop out.
  - `theme_minimal()` - a minimalistic theme with no background annotations.
  - `theme_classic()` - a classic-looking theme, with x and y axis lines and no gridlines.
  - `theme()` - create your own theme by modifying theme components, see https://ggplot2.tidyverse.org/reference/theme.html


###PRACTICE

Play around with a few themes - contrast and compare, let us know your favorite ones in the comments
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop), alpha=0.3)+
    geom_smooth(aes(color=continent), method="lm",se=FALSE)+
    scale_x_log10()+
    labs(title="Best-fit lines between gdpPercap(log10) and lifeExp for each continent",
         x="Log 10 of GDP per capita", 
         y="Life expectancy") +
    theme_light()
```


### Modify graphs with`theme()`
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop), alpha=0.3)+
    geom_smooth(aes(color=continent), method="lm",se=FALSE)+
    scale_x_log10()+
    labs(title="Best-fit lines between gdpPercap(log10) and lifeExp for each continent",
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    theme_light()+
    theme( # change font size, font, and color for x-axis title
      axis.title.x=element_text(size=20, family="serif", face="italic", color="red"))

```



## Colors


### Set ggplot color manually

Use `scale_fill_manual()` for box plot, bar plot, violin plot, dot plot, etc
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent",
         y="Life expectancy")+
    scale_fill_manual(values = c("#FFDB6D","#D16103","#52854C","#4E84C4","#293352"))
```


Use `scale_color_manual()` for lines and points
```{r}
countries %>%
  ggplot(aes(x=gdpPercap, y=lifeExp))+
    geom_point(aes(color=continent,size=pop), alpha=0.3)+
    geom_smooth(aes(color=continent), method="lm",se=FALSE)+
    scale_x_log10()+
    labs(title="Best-fit lines between gdpPercap(log10) and lifeExp for each continent",
         x="Log 10 of GDP per capita", 
         y="Life expectancy")+
    scale_color_manual(values = c("#FFDB6D","#D16103","#52854C","#4E84C4","#293352"))
```


### Viridis color palettes

The viridis R package provides color palettes to make beautiful plots that are: printer-friendly, perceptually uniform and easy to read by those with colorblindness.

  - Use `scale_fill_viridis()` for box plot, bar plot, violin plot, dot plot
  - Use `scale_color_viridis()` for lines and points
  
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent",
         y="Life expectancy")+
    scale_fill_viridis(discrete = TRUE)
```

### Scientific journal color palettes

The R package ggsci contains a collection of high-quality color palettes inspired by colors used in scientific journals, data visualization libraries, and more.

- scale_color_npg() and scale_fill_npg(): Nature Publishing Group color palettes
- scale_color_aaas() and scale_fill_aaas(): American Association for the Advancement of Science color palettes
- scale_color_lancet() and scale_fill_lancet(): Lancet journal color palettes
- scale_color_jco() and scale_fill_jco(): Journal of Clinical Oncology color palettes
- scale_color_tron() and scale_fill_tron(): This palette is inspired by the colors used in Tron Legacy. It is suitable for displaying data when using a dark theme.

Remember to use `fill` functions for box plot, bar plot, violin plot, dot plot

## Annotations

Use `geom_hline` to add life expectancy in Canada as a reference line to your graph (replace for `geom_vline` for vertical lines):
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent",
         y="Life expectancy")+
    geom_hline(color="red", yintercept=82.51) + 
    scale_fill_viridis(discrete = TRUE)
```

Use `annotate` to add text and point to the line:
```{r}
countries %>%
  ggplot(aes(x=continent, y=lifeExp, fill=continent))+
    geom_violin()+
    labs(title="Distribution of life expenctancy by continent",
         x="Continent",
         y="Life expectancy")+
    geom_hline(color="red", yintercept=82.51)+
    annotate("text", x = "Americas", y = 85.51, label = "Canada 2020", color="red")+
    annotate("point",x = "Americas", y = 82.51, color="black")+     scale_fill_viridis(discrete = TRUE)
```


## Piping in `ggplot2`

Visualize changes in annual average life expectancy by continent with points representing the size of populations
```{r}
lifeExp_continent <- countries %>%
    group_by(continent, year) %>%
      summarise(lifeExp=mean(lifeExp), pop = sum(pop)) %>%
      ggplot(aes(x=year, y=lifeExp, color=continent))+
          geom_point(aes(size=pop))+
          labs(title="Annual average life expectancy by continent", 
               y="Life expectancy")+
          geom_line() 

lifeExp_continent
```


## Save produced graphs

Use `ggsave` function to save the plot in your working directory
```{r}
ggsave("lifeExp_continent.png", plot = lifeExp_continent, device="png", width = 8, height = 5)

getwd()
```

# Interactive visualizations

## `ggplotly`

Make an interactive plotly scatterplot of life expectancy and GDP per capita in 2007
```{r}
plot1 <- countries %>%
  filter(year==2007) %>%
    ggplot(aes(x=gdpPercap, y=lifeExp))+
      geom_point(aes(size=pop, color=continent))+
      labs(title="Life expectancy and GDP per capita, 2007",
           x="Log 10 of GDP per capita", 
           y="Life expectancy")+
      scale_x_log10()+
      geom_smooth(aes(color=continent), method="lm", se=FALSE)
ggplotly(plot1)
```


Add year as a frame so that we can see changes over time
```{r}
plot2 <- countries %>%
    ggplot(aes(x=gdpPercap, y=lifeExp, frame=year))+
      geom_point(aes(color=continent, size=pop,label=country))+
      geom_smooth(aes(color=continent), method="lm",se=FALSE)+
      scale_x_log10()+
      labs(title="Life exp. and GDP per capita, 1952-2007",
           x="Log 10 of GDP per capita", 
           y="Life expectancy") 

ggplotly(plot2)


#save the object as an html file, using htmlwidget package
plotly2<-ggplotly(plot2)
saveWidget(as_widget(plotly2), "plotly2.html")
```

Note: You can make any static `ggplot2` graph interactive with `plotly`

## `gganimate`

Create an animation using `transition_time` function + show historical traces by adding `shadow_wake` function
```{r}
gif_countries <- countries %>%
    ggplot(aes(x=gdpPercap, y=lifeExp))+
      geom_point(aes(color=continent, size=pop,label=country))+
      scale_x_log10()+
      transition_time(year)+
      shadow_wake(wake_length = 0.1)+
      labs(title="Life expectancy and GDP per capita in {frame_time}",
           x="Log 10 of GDP per capita", 
           y="Life expectancy")

gif_countries
```

Save animation using `anim_save` function
```{r}
anim_save("countries.gif", animation = gif_countries, path = NULL)
```
