title: "week5labmcnolan.nb.html" output: htmldocument: dfprint: paged --- ```{r} library(tidyverse) library(completejourney)

```

```{r} transactions <- gettransactions() candyData <- transactions %>% innerjoin(products, by = "productid") %>% filter(strdetect(productcategory, regex("candy - packaged", ignorecase = TRUE))) %>% innerjoin(demographics, by = "householdid")

couponData <- couponredemptions %>% innerjoin(demographics, by = "household_id")

productCategories <- transactions %>% innerjoin(products, by = "productid") %>% innerjoin(demographics, by = "householdid") %>% groupby(productcategory, kids_count) %>% summarize(timesBought = n())

ggplot(candyData, aes(x = producttype, y = householdsize, fill = producttype)) + geombar(stat = "identity") + labs(title = "Sales of Different Candy Types", x = "Candy Type", y = "Household Size")

```

{r} monthly_spending_and_quantity %>% ggplot(aes(x = month)) + geom_col(aes(y = spend, fill = 'Total Net Spend ($)')) + geom_point(aes(y = qty/75)) + geom_path(aes(y = qty/75, group = 1, color = 'Quantity Sold')) + scale_y_continuous( name = 'Total Net Spend ($)', sec.axis = sec_axis(~.*50, name = 'Quantity Sold') ) + labs( title = 'Spending/ Quantity Per Month', x = 'Month', subtitle = 'The data below shows the total net amount in dollars spent and the net quantity sold by month in the year 2010.' ) + scale_fill_manual( name = '', values = c('Total Net Spend ($)' = 'blue') ) + scale_colour_manual( name = '',values = c('Quantity Sold' = 'red') ) + theme( plot.title = element_text(face = "bold", size = 20), legend.key.width = unit(0.5, 'cm'), legend.text = element_text(size = 6), axis.text = element_text(size = 8), axis.title = element_text(size = 9) )

{r} avg_candy_spending_household <- transactions_sample %>% inner_join(products, by = "product_id") %>% inner_join(demographics, by = "household_id") %>% filter(grepl("candy", product_category, ignore.case = TRUE)) %>% group_by(household_size) %>% summarise(avg_spending = mean(sales_value, na.rm = TRUE)) %>% arrange(desc(avg_spending)) avg_candy_spending_household$highlight <- ifelse(avg_candy_spending_household$household_size == "5+", "highlight", "normal") ggplot(avg_candy_spending_household, aes(x = reorder(household_size, -avg_spending), y = avg_spending, fill = highlight)) + geom_bar(stat = "identity") + scale_fill_manual(values = c("highlight" = "pink", "normal" = "grey")) + geom_text(aes(label = scales::dollar(avg_spending)), vjust = -0.5, size = 4, color = "black", data = avg_candy_spending_household %>% filter(highlight == "highlight")) + labs(title = "Candy Spending by Household Size (2010)", x = "Household Size", y = "Average Spent per Transaction") + scale_y_continuous(labels = scales::dollar_format()) + theme_minimal() + theme(legend.position = "none")