library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
transactions <- transactions_sample
combined_data <- transactions %>%
left_join(demographics, by = "household_id") %>%
left_join(products, by = "product_id")
top_products <- combined_data %>%
group_by(income, product_id) %>%
summarize(total_sales = sum(sales_value)) %>%
arrange(desc(total_sales)) %>%
group_by(income) %>%
top_n(5)
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.
## Selecting by total_sales
ggplot(data = top_products, aes(x = income, y = total_sales, fill = product_id)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(name = "Total Sales", limits = c(0, 1000), labels = scales::dollar) +
labs(title = "Top 5 Products Purchased by Each Income Level",
x = "Income Level",
fill = "product_id") +
theme_classic()
## Warning: Removed 4 rows containing missing values (`geom_bar()`).

library(dplyr)
library(ggplot2)
library(completejourney)
transactions <- transactions_sample
joined_data <- transactions %>%
left_join(demographics, by = "household_id")
top_products <- joined_data %>%
group_by(income, product_id) %>%
summarize(total_sales = sum(sales_value),
total_quantity = sum(quantity)) %>%
arrange(desc(total_sales)) %>%
group_by(income) %>%
top_n(5)
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.
## Selecting by total_quantity
ggplot(top_products, aes(x = product_id, y = total_sales, fill = income)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(name = "Total Sales Value", limits = c(0, 2500), labels = scales::dollar) +
labs(title = "Top 5 Products by Income Level",
x = "Product ID",
y = "Total Sales Value") +
theme_classic()
## Warning: Removed 1 rows containing missing values (`geom_bar()`).
