```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo       = FALSE, # whether to show code chunks
  message    = FALSE, # whether to show messages from your code
  warning    = FALSE, # whether to show warnings from your code
  fig.width  = 8,     # figure width in inches (at 96 dpi)
  fig.height = 5,     # figure height in inches (at 96 dpi)
  out.width = "50%"   # figures/images span 50% of the page width
)

library(tidyverse)
library(kableExtra) # for table formatting
```


```{r, message=FALSE}
# Data from https://www.kaggle.com/kyanyoga/sample-sales-data
sales_online <- read_csv("https://psyteachr.github.io/ads-v2/data/sales_data_sample.csv")
```

```{r sales_counts}
# keep only the data from planes, motorcycles, and cars and then
sales_pmc <- filter(sales_online,
         PRODUCTLINE %in% c("Planes", "Motorcycles", "Classic Cars"))

# count how many are in each PRODUCTLINE
sales_counts <-count(sales_pmc, PRODUCTLINE)
```


## Sample sales report

This report summarises the sales data for different types of vehicles sold between 2003 and 2005. This data is from [Kaggle](https://www.kaggle.com/kyanyoga/sample-sales-data).

### Sales by type

The *total* number of **planes** sold was `r sales_counts$n[3]`

The *total* number of **classic cars** sold was `r sales_counts$n[1]`.

The *total* number of **motorcycles** sold was `r sales_counts$n[2]`.

```{r}
# important numbers

n <- nrow(sales_online) # the total number of sales (number of rows)
first <- min(sales_online$YEAR_ID) # the first (minimum) year
last <- max(sales_online$YEAR_ID) # the last (maximum) year
```

```{r fig-vehicle-sales}
ggplot(data = sales_counts, 
       mapping = aes(x = PRODUCTLINE, 
                     y = n, 
                     fill = PRODUCTLINE)) +
  geom_col(show.legend = FALSE) +
  labs(x = "Type of vehicle",
       y = "Number of sales",
       title = "Sales by vehicle type",
       subtitle = "2003 - 2005")
```


```{r fig-example}
knitr::include_graphics("https://upload.wikimedia.org/wikipedia/commons/3/3f/P-51_Mustang_edit1.jpg")
```


## Data in Table Form

```{r}
sales_counts
```

```{r}
k <- kable(sales_counts, 
      col.names = c("Product", "Sales"),
      caption = "Number of sales per product line.", 
      align = "c")
k_style <- kable_classic(k, full_width = FALSE) 
k_highlighted <- row_spec(k_style, row = 0, bold = TRUE, color = "red") 

k_highlighted
```








