This report includes data taken from Tableau Superstore sales dataset. The data has sales values for the years 2015 to 2018. My aim is to plot a faceted bar chart for the total sales across the 3 categories Furniture, Office Supplies, Technology over the time period 2015-2018. For this purpose, some changes were made to the sales dataset and a new dataframe Categorical_Sales is created as shown in the code below.
library(tidyverse)
library(ggplot2)
sales <-
read_csv("D:/UC MSBANA/Data Wrangling with R/Week 5/Week 5/train.csv")
sales$`Ship Date` <- as.Date(sales$`Ship Date`, format = "%d/%m/%Y")
sales$`Order Date` <-
as.Date(sales$`Order Date`, format = "%d/%m/%Y")
sales$year <- format(sales$`Order Date`, "%Y")
Categorical_Sales <-
sales %>% group_by(year, Category) %>% summarize(gr_sum = sum(Sales)) %>% ungroup()
ggplot() function was used with arguments as Categorical_Sales for data, Category on X-Axis, gr_sum on Y-axis(Aesthetic mappings). The entire plot is faceted with the newly created variable year. Y-Axis was renamed and labeled in dollars. The bars for each category are labeled with the categorical sum of sales for that year using geom_text(). The title and subtitle for the visualization were given by ggtitle() function. The X-axis labels are rotated 90 degrees in counter-clockwise direction.
ggplot(data = Categorical_Sales, aes(x = Category, y = gr_sum, fill = Category)) +
geom_bar(stat = "identity") + geom_text(
aes(label = round(gr_sum, 2)),
position = position_dodge(width = 1),
hjust = 0.5,
vjust = 2.5,
size = 3
) +
scale_y_continuous(labels = scales::dollar_format()) +
ggtitle("Superstore Sales from 2015 to 2018", subtitle = "Total Sales across various categories ") + facet_wrap( ~ year) + theme(axis.text.x = element_text(
angle = 90,
vjust = 0.5,
hjust = 1
)) + labs(y = "Categorical Sum of Sales")