---
title: "Analyzing Hass USA"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Analyzing Hass USA}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

The {avocado} package provides a weekly summary - starting from January 2017 through November 2020 - of Hass Avocado sales. There are three datasets in this package and let's start with the dataset `hass_usa` which focuses on weekly avocado sales in the contiguous US.

Let's start by loading the package - along with {dplyr} (for data wrangling) and {ggplot} (for data visualization) - and exploring it's structure

```{r setup}
library(avocado)
library(dplyr)
library(ggplot2)

data('hass_usa')

dplyr::glimpse(hass_usa)
```


## Exploratory Data Analysis

Let's begin by exploring the following two topics:

- Fluctuation of average selling price
- Non Organic Avocado sales revenue

### Fluctuation of Average Selling Price

```{r fig.height=5, fig.width=8}

hass_usa |> 
  ggplot(aes(x = week_ending)) +
  geom_line(aes(y = avg_selling_price, color = as.factor(type))) +
  scale_color_manual(labels = c('Conventional','Organic'), values = c('steelblue','forestgreen')) +
  scale_x_date(date_breaks = '1 year', date_labels = '%Y') +
  labs(
    x = 'Year',
    y = 'Average Selling Price per Unit (US$)',
    title = 'Fluctuation of Average Selling Price', 
    caption = 'Not adjusted for inflation\nSource: Hass Avocado Board',
    color = ''
  ) +
  ylim(min = 0, max = 3.0) +
  theme(
    plot.background = element_rect(fill = "grey20"),
    plot.title = element_text(color = "#FFFFFF"),
    axis.title = element_text(color = "#FFFFFF"),
    axis.text.x = element_text(color = 'grey50', angle = 45, hjust = 1),
    axis.text.y = element_text(color = 'grey50'),
    plot.caption = element_text(color = 'grey75'),
    panel.background = element_blank(),
    panel.grid.major = element_line(color = "grey50", linewidth = 0.2),
    panel.grid.minor = element_line(color = "grey50", linewidth = 0.2),
    legend.background = element_rect(fill = 'grey20'),
    legend.key = element_rect(fill = 'grey20'),
    legend.title = element_text(color = 'grey75'),
    legend.text = element_text(color = 'grey75'),
    legend.position = 'inside',
    legend.position.inside = c(0.85, 0.85)
  )

```

Interestingly, we can see that the average selling price for organic avocados tends to be higher than the average selling price for non-organic (Conventional) avocados. Note how there seems to be a fairly large spike in selling price in late 2017. Moreover, it seems as if the peak average selling price of avocados is declining as time goes on.