## Introduction!

Today we will focus on time series data. Time series data consists of any metric that is measured over regular time intervals. Examples: Weather data, Local Crime Statistics, Stock prices, Industry forecasts, Number of Protests Events.

Note: you could take an entire course on time series data and modeling! This is only an introduction, but it will plant the seeds for future learning on the topic and help you understand why 'accounting for time' even if visually, is important. To begin, let's explore the ever-intuitive line-plot using familiar data.

## Line plots

Line plots are particularly useful for exploring time series data. We will continue working with the `gapminder` data.  
```{r, warning = F, fig.height = 2, fig.width = 4}
library(gapminder)
library(ggplot2)
str(gapminder)
df <- gapminder
```

To begin, let's say we wanted to know a bit more about GDP development in China. Below, we will graph the GDP per capita development of China from 1952 to 2007. We select the data for China by using the `subset()` function on the original data frame.
```{r}
ggplot(subset(df, country == "China"),aes(x = year,y = gdpPercap)) +
  geom_line()
```

We can add points to the line to highlight which observations are available in the underlying data.
```{r, warning = F, fig.height = 2, fig.width = 4}
ggplot(subset(df, country == "China"),aes(x = year, y = gdpPercap)) +
  geom_line() +
  geom_point()
```

China and India both have very large populations that have grown rapidly over the last few decades. What if we wanted to also check out whether a similar trend exists for India? 

```{r}
ggplot(subset(df, country == "India"),aes(x = year, y = gdpPercap)) +
  geom_line() +
  geom_point()
```

Now that we are exploring comparisons, let's compare the data across multiple countries overtime. 

Go ahead and make a plot to compare the GDP per capita development of the BRICS countries (Brazil, Russia, India, China, South Africa; these have been coined as five major emerging national economies). Unfortunately, Russia is not part of the `gapminder` data, so we cannot display it in the plot.

*Practice 1*

Please create a publication-ready graph that can be printed using grayscale. Keep in mind you should distinguish the different lines easily from one another.

```{r}
# students:





```

In cases like this, we might want to facet the plot by a single variable, using `facet_wrap()`. The first argument of `facet_wrap()` should be a formula, which you create with `~` followed by a variable name (here "formula" is the name of a data structure in R, not a synonym for "equation"). The variable that you pass to `facet_wrap()` should be discrete. Below, we can use facet wrap ~ country to inspect trends across groupings.


```{r, warning = F, fig.height = 2, fig.width = 7, echo = TRUE, message = F}
ggplot(subset(df, country %in% c("Brazil", "Russia", "China", "India", "South Africa")),
       aes(x = year,
           y = gdpPercap)) +
  geom_line(alpha = 0.5) +
  geom_point(alpha = 0.8,
             size = 0.4) +
  theme_light() +
  labs(title = "GDP per capita in BRICS countries",
       x = "Year",
       y = "GDP per capita") +
  facet_wrap(~country, nrow = 1) +
  scale_x_continuous(breaks = seq(1950,2000, 25))
```

Which plot do you like better? Facet wrap, or everything on the same graph? What are the pros and cons?


*Practice 2*
We may also want to look at how population in BRICS countries changed over time. Try to produce a similar graph for population but use `facet_grid()` instead of `facet_wrap()`. You might want to check the help file for `facet_grid()` because there is a tiny difference between the two. 

Hint: consider adding `scale_y_continuous(breaks=seq(0, 1500000000, 300000000)` and changing `labels = function(x){paste0(x/1000000, ' million')})` to control the labels of your Y axis.

```{r}
# students:





```
### Spaghetti plots

Spaghetti plots are line plots with many lines displayed in a plot. Typically, the lines are very thin and/or have a high level of transparency to show trends in the data. Below, we graph the evolution of life expectancy for all countries in the data set. We allow each country to have its own line by using the `group()` parameter inside `aes()`. We will highlight one country in red.

```{r, warning = F, fig.height = 2.5, fig.width = 4, message = F}
brics <- c("Brazil", "Russia", "China", "India", "South Africa")
ggplot(df, aes(x = year, y = lifeExp, group = country)) +
  geom_line(alpha = 0.2, 
            size = 0.1) +
  labs(title = "Life expectancy over time",
       x = "Year",
       y = "Life expectancy") +
  theme_light() +
  geom_line(data = subset(df, country %in% "South Africa"),
            color = "red")
```

*Practice 3*
We can also display the temporal trend for select countries using color. Below, plot a spagetti plot simialr to the one above, but this time highlight all four of the BRIC countries. Here is a good example of when and why you might use multiple layers of the same geom! 

Note: How does scale_color_brewer work? How does it 'know' where to apply color?


```{r}
# students:





```
In the plot below, we will use a log transformation on GDP per capita within the `scale_color_gradient()` function to display GDP per capita values on their original scale but map the color of the lines to the natural log of GDP per capita. Why is it useful to apply a log transformation here?  Discuss and interpret this plot. 


```{r, warning = F, message = F, fig.height = 3, fig.width = 8}
ggplot(df,aes(x = year,y = lifeExp, group = country, color = gdpPercap)) +
  # Setting up spaghetti plot
  geom_line(alpha = 1, size = 0.1) +
  
  # Each continent in a separate panel
  facet_wrap(~continent, nrow = 1) +
  
  # Choosing default theme
  theme_light() +
  
  # Adjusting default theme
  theme(panel.background = element_rect(fill = "black"),
        panel.grid = element_line(size = 0.1),
        strip.background = element_rect(fill = "black"),
        strip.text = element_text(color = "white")) +
  
  # Making the colors pop
  scale_color_gradient(low = "#f7ff00",
                      high = "#00f7ff",
                      name = "GDP per capita",
                      trans = "log") +
  
  # Additional appearance adjustments
  labs(x = "Year",
       y = "Life expectancy",
       title = "Global life expectancy") +
  theme(legend.position = "bottom",
        legend.key.width = unit(1.5, "cm")) +
  scale_x_continuous(breaks = seq(1950, 2010, 25))
```


## Why time matters

Understanding time often tells us a lot about changes in trajectories across different cases in our data. Later in your education as a data scientist, you will also learn about the ways in which dimensions of the data correlate overtime.

Let's consider the `eu_stocks` dataset (available in R by default as EuStockMarkets). This dataset contains daily closing prices of major European stock indices from 1991-1998, specifically, from Germany (DAX), Switzerland (SMI), France (CAC), and the UK (FTSE). The data were observed when the markets were open, so there are no observations on weekends and holidays. 

```{r}
#install.packages("datasets.load") #some might not have to do this
data("EuStockMarkets")
eu_stocks = EuStockMarkets
```

Below, explore the `ts()` and `is.ts()` function from the R stats package, as well as the help files that describe what a time series object is.

Explore: What is the 'frequency' argument of a time series object? Comment these below. 

```{r}
is.ts(eu_stocks)
start(eu_stocks)
end(eu_stocks)
frequency(eu_stocks)
```

We can tell from using the time-series function that this data is already a time-series object. The `ts()` function will convert a numeric vector into an R time series object. The format is `ts(vector, start=, end=, frequency=)` where `start` and `end` are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

```{r}
# R recognizes when the x argument is a ts object and actually calls the plot.ts() function under the hood
plot(eu_stocks)
```

```{r}
ts.plot(eu_stocks, col = 1:4, xlab = "Year", ylab = "Index Value", main = "Major European Stock Indices, 1991-1998")
legend("topleft", colnames(eu_stocks), lty = 1, col = 1:4, bty = "n")
```

To create a similar plot in ggplot we can use the handy `ggfortify`! `ggfortify` let `ggplot2` know how to interpret ts objects. After loading `ggfortify`, you can use ggplot2::autoplot function for `ts` objects.

```{r}
library(ggfortify)
autoplot(eu_stocks) + theme_bw()
#note, what kind of data object does autoplot use?
```

For more illustration:

```{r}
set.seed(123)
# a sequence from 1 to 100 scaled up by 10 to avoid negatives +  some random normal noise 
t <- seq(from = 1, to = 100, by = 1) + 10 + rnorm(100, sd = 7)
plot(t)
```

This could potentially represent some time series, with time represented along the x-axis. However, it’s hard to tell. The x-axis is simply an index from 1 to 100 in this case.

A vector object such as t above can easily be converted to a time series object using the `ts()` function. Let’s assume our generated data is quarterly data that starts in the first quarter of 2000.

```{r}
tseries <- ts(t, start = c(2000, 1), frequency = 4)
print(tseries)
plot(tseries)
```



## References
- https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_ts.html
-  https://github.com/thereseanders/workshop-dataviz-fsu
- http://worldpopulationreview.com/countries/
- http://r-statistics.co/Time-Series-Analysis-With-R.html
- https://ntguardian.wordpress.com/2017/03/27/introduction-stock-market-data-r-1/
- https://www.statmethods.net/advstats/timeseries.html
- https://buildmedia.readthedocs.org/media/pdf/a-little-book-of-r-for-time-series/latest/a-little-book-of-r-for-time-series.pdf
- https://datascienceplus.com/time-series-analysis-in-r-part-1-the-time-series-object/
