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

DATA VISUALIZATION WITH GGPLOT2

# Workshop material:

(1) Intro/getting started
(2) Basic ggplot plots
(3) Customizing ggplot aesthetics
(4) Faceting
(5) Animated and interactive plots
(6) Conclusion

## 1.INTRO/GETTING STARTED

Install R packages

Remove the #s if you need to run this code to install a package/multiple packages

```{r}
#install.packages("ggplot2")
#install.packages("gapminder")
#install.packages("RColorBrewer")
```

Load packages

```{r warning = FALSE, message = FALSE}
library(ggplot2)
library(gapminder) # package with a gapminder dataset
library(RColorBrewer) # color palette package
```

### Basic ggplot structure (the grammar of graphics!):

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

### Plots you can make with ggplot:

-Barcharts -Histograms -Scatterplots -Boxplots -Density plots -Violin plots ...And many more

### You can customize:

-Labels -Colours -Legends -Trendlines -Scales ...And much more

Today we will be working with the countries dataset from gapminder:

```{r}
countries <- gapminder #renaming dataset
head(countries) #inspecting data
```

## 2.BASIC GGPLOT PLOTS

lets start with creating a bar chart - This will show the distribution of a categorical variable, like our continent variable. Recall the basic structure of ggplot2 code:

ggplot(aes(x, y), data) + geom

We will start with building the most basic plot possible, with no modifications to the theme.

```{r}
ggplot(aes(x = continent), data = countries) +
  geom_bar()
```

Note that in a barplot there is no 'y' variable, as it is a count of the data.

This is similar to a histogram, but a histogram is for showing the distribution of a numeric variable, like life expentancy:

```{r}
ggplot(aes(x = lifeExp), data = countries) +
  geom_histogram(bins = 50)
```

To create a plot with an x and a y variable, all we need to do is specify an additional argument.

for example, this boxplot:

```{r}
#one numeric variable, one categorical: 
ggplot(aes(x = continent, y = lifeExp), data = countries) +
  geom_boxplot()
```

### PRACTICE:

For all practices today, could you paste your answers in the chat to let us know how many people have completed the practice?

Using the iris dataset, create a scatter plot ( geom_point() ) of Petal.Length (x variable) vs Sepal.length (y variable)

```{r}
head(iris) #inspecting the data 

#Answer: 


```

## 3.CUSTOMIZING GGPLOT AESTHETICS

There are a number of aesthetics you can customize in ggplot to improve the clarity or complexity of your plots, and make them publication/thesis-worthy. The grammar/structure of how you do so is consistent across plot types.

Note that modifying aesthetics can complicate/lengthen your plot's code very quickly.

Returning to our iris scatterplot, we may want to add more informative labels:

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point() +
  labs(x = 'Petal length (cm)',
       y = 'Sepal length (cm)',
       title = 'Petal length vs sepal length of irises')
```

To modify this text to be more aesthetically pleasing:

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point() +
  labs(x = 'Petal length (cm)',
       y = 'Sepal length (cm)',
       title = 'Petal length vs sepal length of irises') +
  theme(plot.title = element_text(hjust = 0.5, # horizontal justification
                                  face = 'bold',
                                  size = 18))

```

With this same structure (ie if we replaced 'plot.title'), we could modify:

axis text: axis.text axis labels: axis.title

Our data is clearly positively trended. We could visualize this trend using 'geom_smooth':

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point() + 
  geom_smooth(method = 'lm', se = FALSE) # specifying a linear trendline with no standard error interval
```

Our data is also clearly in groups (in this case, it has to do with species). We can visualize this with a color argument:

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) + #setting color by species 
  geom_smooth(method = 'lm', se = FALSE, col = 'grey25') #making the trendline grey 
```

This plot illustrates a key distinction in specifying attributes of a plot - when you specify something, like colour (col) WITHIN aes(), it becomes a part of the legend, and is usually distinguished by values of a specified variable (in this case, species).

When you specify OUTSIDE aes(), it is a simple aesthetic specification and you provide the colour directly (see the case below).

To modify the colours when col is specified within aes, additional code is needed:

```{r}
unique(iris$Species)

ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) + 
  geom_smooth(method = 'lm', se = FALSE, col = 'grey25')+
  scale_color_manual(values = c("turquoise3", "goldenrod1","salmon2")) #providing custom colours
```

There are also packages with built in color scales that are aesthetically pleasing and color-blind friendly, e.g. 'viridis':

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) +
  geom_smooth(method = 'lm',
              se = FALSE,
              col = 'grey25') +
  scale_color_brewer(palette = "Dark2") #specifying colours for a discrete variable 
```

These colours/scales are further customizable. If interested, see: <https://r-graph-gallery.com/38-rcolorbrewers-palettes.html>

Palettes are a nice way to increase accessibility of your work while making the graphs even prettier! Take for example, colorblindness.

```{r}
# display only color blind freindly RColorBrewer Pallettes 
display.brewer.all(colorblindFriendly = TRUE)
```

Similarly, there are shortcuts for modifying our entire 'theme':

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) +
  geom_smooth(method = 'lm',
              se = FALSE,
              col = 'black') +
  theme_bw() #black and white theme 
```

or:

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) + 
  geom_smooth(method = 'lm', se = FALSE, col = 'black')+
  theme_classic() #'minimal' theme 
```

See all the ggplot2 themes here <https://ggplot2.tidyverse.org/reference/ggtheme.html>

if we combine ALL the modifications so far in the workshop:

```{r}
ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) +
  labs(x = 'Petal length (cm)',
       y = 'Sepal length (cm)',
       title = 'Petal length vs sepal length of irises') +
  geom_smooth(method = 'lm',
              se = FALSE,
              col = 'grey25') +
  scale_color_brewer(palette = "Dark2")
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5,
                                  face = 'bold',
                                  size = 16)) #additional theme modifications have to come after theme_minimal to not be overridden 
```

You can save your graphs from the plot window, or to your working directory using code (with the ggsave function) if you assign your plot to an object:

```{r}
myplot <-
  ggplot(aes(x = Petal.Length, y = Sepal.Length), data = iris) +
  geom_point(aes(col = Species)) +
  labs(x = 'Petal length (cm)',
       y = 'Sepal length (cm)',
       title = 'Petal length vs sepal length of irises') +
  geom_smooth(method = 'lm',
              se = FALSE,
              col = 'grey25') +
  scale_color_brewer(palette = "Dark2")
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5,
                                  face = 'bold',
                                  size = 16))

ggsave(
  "myplot.png",
  plot = myplot,
  device = "png",
  width = 8,
  height = 5
)

getwd()

## note, you can specify the filepath when saving
## you can also arrange multiple plots next to each other and add figure labels with the papckage ggarrange 
```

### PRACTICE:

Using the countries dataset, plot gdpPercap (gdp per capita, x variable) vs lifeExp (life expectancy). Add a linear trendline to the plot.

```{r}
#Answer: 


```

You will notice our data is non-linear - we can modify our plot by log-ing the x variable to correct for this:

```{r}
ggplot(aes(x = log(gdpPercap), y = lifeExp), data = countries) + #log of x
  geom_point(aes(col = year)) +
  geom_smooth(method = 'lm',
              se = FALSE,
              col = 'grey25') +
  labs(x = 'log 10 of GDP per capita ($)', y = 'life expectancy (yrs)') +
  theme_bw()
```

It is hard to see individual trends in this plot. We could add a separate trendline for each year:

```{r}
countries$year <-
  as.character(countries$year) #need our data to be categorical instead of continuous for this

ggplot(aes(x = log(gdpPercap), y = lifeExp), data = countries) +
  geom_point(aes(col = year)) +
  geom_smooth(aes(col = year), method = 'lm', se = FALSE) + #add colour argument
  labs(x = 'log 10 of GDP per capita ($)', y = 'life expectancy (yrs)') +
  theme_bw()
```

## 4.FACETING

Even better, we could use faceting to separate our plot out by year:

```{r}
ggplot(aes(x = log(gdpPercap), y = lifeExp), data = countries) +
  geom_point(aes(col = year)) +
  geom_smooth(col = 'grey25',
              method = 'lm',
              se = FALSE) +
  labs(x = 'log 10 of GDP per capita ($)', y = 'life expectancy (yrs)') +
  theme_bw() +
  facet_grid(year~.) #facet by year
```

## 5.ANIMATED AND INTERACTIVE PLOTS

And perhaps more powerfully, we could animate our plot to show the evolution of our trend through time:

This is meant as a demo of what you can do in ggplot2. You can just follow along again, or if you successfully downloaded the "nice to have" packages, you can code along.

```{r}
#Nice to have packages
library(gganimate) # package for producing gifs, extension of ggplot2
library(plotly)
library(gganimate)
library(gifski)
library(htmlwidgets)

```

```{r}
plot1 <- ggplot(aes(
  x = log(gdpPercap),
  y = lifeExp,
  frame = year
), #adding frame to aes
data = countries) +
  geom_point(aes(color = continent)) +
  labs(title = "Life expectancy vs GDP per capita through time",
       x = "Log 10 of GDP per capita",
       y = "Life expectancy") +
  geom_smooth(aes(color = continent), method = "lm", se = FALSE) +
  theme_bw()

ggplotly(plot1)

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