---
title: "Exercise: Retail Sales Data Analysis Final"
author: "Rian DeFaccio, for BH Analytics LLC"
date: "`r Sys.Date()`"
output: html_document
---

To begin, load any libraries you would like to use.

```{r message=FALSE}
library(tidyverse)
options(scipen = 999)
```

### 0. Build Your Theme

------------------------------------------------------------------------

In the code below we have built a fairly minimal theme. Try around with adjusting some of the elements to make it your own. Specifically, we started with `ggthemes::theme_pander()` as our base theme, but you could also change that.

For the full listing of elements you can adjust, see the ggplot2 themes documentation.

🌐 <https://ggplot2.tidyverse.org/reference/theme.html>

```{r}
# build your theme here
my_theme <- function() {
  ggthemes::theme_pander() +
    theme(
      plot.title = element_text(hjust = 0.5),
      plot.subtitle = element_text(hjust = 0.5),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      axis.line.x.bottom = element_line(),
      axis.line.y.left = element_line()
    )
}

# test it out
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  my_theme() +
  ggtitle("A Title Goes Here")
```

To use your new theme as the default theme for all plots, run the following code:

```{r}
ggplot2::theme_set(my_theme())
```

### 1. Import Data

------------------------------------------------------------------------

In this exercise we are picking up where we left off with the prior exercise of working with the transactions data. Import the data that we saved as an *.rds* file from that exercise. If you didn't finish the prior exercise, that is okay! We have a version of this file saved as well:

> *data/retail_sales/retail_sales_analytic.rds*

```{r}

```

### 2. Analysis by Country

------------------------------------------------------------------------

Let's do an analysis that focuses on transactions by country. Utilize a group-by analysis to learn about the following:

-   How many items were sold in each country? Use the `Quantity` variable to understand the quantity of items sold.
-   How much money was made in each country? This question is not quite as simple as it seems. The amount of money made in a given transaction (row) is equal to the unit price of the item sold multiplied by the quantity of items sold.

Sort your output by the total sales for each country.

What country had the highest total sales? What about the most items sold?

```{r}

```

### 3. Items Per Invoice

------------------------------------------------------------------------

How many items were sold per invoice? This will also require a group-by analysis. Begin by saving a new data frame called `df_invoice_items` that has the count of items sold for each invoice number. Take a look at this data frame after you build it.

```{r}

```

Now get some quick summary statistics on the count of items sold per invoice. If you suspect there are any outliers, take a closer looks at those. If you identify invoices that are suspicious in `df_invoice_items` you can then go back to the original data to get more details on what was sold in those invoices. The steps would look something like this:

1.  Identify suspicious invoices. This might be invoices with a very large number of items.
2.  Extract the `InvoiceNo`s from these suspicious invoices.
3.  Subset the original data to include only rows for the suspicious invoices. Do these values seem plausible?

```{r}

```

```{r}

```

```{r}

```

```{r}

```

**BONUS:** Examine the following quantities that depend on country.

-   The number of items-per-invoice by country?
-   The total number of invoices per country?

Can you think of a way to examine both of these variables together?

**HINT:** There are a couple of customers that could be considered outliers in this data set. It will be easier to view the general relationship if you remove the outliers prior to trying to visualize the overall relationship between items-per-invoice and total invoices.

```{r}

```

### 4. Sales Over Time

------------------------------------------------------------------------

Finally, let's examine how the number of items sold, and total sales have changed over time. We have about one year's worth of data, so looking at this monthly makes sense.

To begin, add a variable called `YearMonth` to the original data frame. It should have values that look like this: **2010-04**

```{r}

```

Now let's look at units sold over time, which is in other words "units sold per month." Can you build a table that shows this? How about a visualization? Choose between a line plot and a bar plot for this visualization.

```{r}

```

Repeat the above analysis, but stratified by country. You should have several lines of different colors, with each color/group representing units sold in different countries.

```{r}

```

Repeat the above analysis, but for total sales instead of units sold.

```{r}

```

**BONUS:** Look at units sold and total sales in the same plot. You will have two facets in this plot, one for units sold and one for total sales.

**HINT:** Check the solutions after your first pass.

```{r}

```
