Problem 1 [20 points]

Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.

This plot can be a scatter plot, overlayed density plots (graphing variable is continuous, separate densities grouped by categorical variable), etc. choropleth maps could also be on the list…you have to admit they look kinda cool.

You can use any data of your choosing, just reference this.

The graph must include:

  1. customized hover text that is informative to the graphing elements in the plot

  2. separate color to represent groups

  3. labeled axes and appropriate title

Include at least a 1-paragraph discussion about the graph. Discuss what is being plotted and what information is being displayed in the graph. Discuss any information that the reader may gain from hovering the cursor over graphing elements. Discuss any issues/chalenges you had (if any) while making the plot, and you you dealt with or overcame them.

Below is the solution

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
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
custom_palette <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf")

Adidas_Sales_2021 <- read.csv("AdidasSalesdata_21.csv")

str(Adidas_Sales_2021)
## 'data.frame':    8346 obs. of  14 variables:
##  $ Retailer        : chr  "Foot Locker" "Foot Locker" "Foot Locker" "Foot Locker" ...
##  $ Retailer.ID     : int  1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 ...
##  $ Invoice.Date    : chr  "Tuesday, October 26, 2021" "Wednesday, October 27, 2021" "Thursday, October 28, 2021" "Friday, October 29, 2021" ...
##  $ Region          : chr  "Northeast" "Northeast" "Northeast" "Northeast" ...
##  $ State           : chr  "Pennsylvania" "Pennsylvania" "Pennsylvania" "Pennsylvania" ...
##  $ City            : chr  "Philadelphia" "Philadelphia" "Philadelphia" "Philadelphia" ...
##  $ Gender.Type     : chr  "Men" "Women" "Men" "Men" ...
##  $ Product.Category: chr  "Apparel" "Apparel" "Street Footwear" "Athletic Footwear" ...
##  $ Price.per.Unit  : int  55 45 45 45 35 40 55 45 50 50 ...
##  $ Units.Sold      : int  125 225 475 125 175 50 125 225 445 150 ...
##  $ Total.Sales     : int  68750 101250 213750 56250 61250 20000 68750 101250 222500 75000 ...
##  $ Operating.Profit: num  24063 30375 117563 19688 24500 ...
##  $ Operating.Margin: num  0.35 0.3 0.55 0.35 0.4 0.4 0.35 0.3 0.55 0.35 ...
##  $ Sales.Method    : chr  "Outlet" "Outlet" "Outlet" "Outlet" ...
Adidas_Sales_2021 %>%
  plot_ly(x = ~Product.Category, y = ~Total.Sales, color = ~Gender.Type, 
          type = "box",
          colors = custom_palette,
          hoverinfo = "text",
          text = ~paste("Product Category:", Product.Category, "<br>",
                        "Total Sales:", Total.Sales, "<br>",
                        "Gender:", Gender.Type))%>%
  layout(title = "Total Sales by product Category and Gender",
         xaxis = list(title = "Product Category"),
         yaxis = list(title = "Total Sales($)"))

The above graph visualizes the total sales of Adidas products in 2021 across different product categories, segmented by gender. Each box represents the distribution of total sales for a specific product category, with separate boxes for men and women’s products. The x-axis denotes the product categories, while the y-axis represents the total sales in dollars. By hovering the cursor over each box, the reader can gain insights into the specific product category, total sales, and the corresponding gender type. This interactive feature allows users to explore the data points more effectively.

One challenge encountered while creating this plot was a warning error ” Warning: minimal value for n is 3, returning requested palette with 3 different levels “. I dealt with this by defining a custom color palette with more than three different levels and used the custom color palette in the plot_ly function. Additionally, formatting the hover text to display informative details without overwhelming the viewer was a challenge. However, by carefully selecting the relevant information to display and formatting it appropriately using the paste() function, I overcame this challenge.

Problem 2 [20 points]

Create an animated plotly graph with a data set of your choosing. This can be, but does not have to be a scatter plot. Also, the animation does not have to take place over time. As mentioned in the notes, the frame can be set to a categorical variable. However, the categories the frames cycle through should be organized (if needs be) such that the progression through them shows some pattern.

This graph should include:

  1. Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.

  2. Appropriate axis labels and a title

  3. Augment the frame label to make it more visible. This can include changing the font size and color to make it stand out more, and/or moving the frame label to a new location in the plotting region. Note, if you do this, make sure it is till clearly visible and does not obstruct the view of your plot.

Include at least a 1-paragraph discussion about the plot. Discuss what you are plotting and what trends can be seen throughout the animation. Discuss any issues, if any, you ran into in making the plot and how you overcame them.

What to turn in:

Below is the solution

library(ggplot2)
library(plotly)
library(dplyr)


Adidas_Sales_2021 <- read.csv("AdidasSalesdata_21.csv")

str(Adidas_Sales_2021)
## 'data.frame':    8346 obs. of  14 variables:
##  $ Retailer        : chr  "Foot Locker" "Foot Locker" "Foot Locker" "Foot Locker" ...
##  $ Retailer.ID     : int  1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 1185732 ...
##  $ Invoice.Date    : chr  "Tuesday, October 26, 2021" "Wednesday, October 27, 2021" "Thursday, October 28, 2021" "Friday, October 29, 2021" ...
##  $ Region          : chr  "Northeast" "Northeast" "Northeast" "Northeast" ...
##  $ State           : chr  "Pennsylvania" "Pennsylvania" "Pennsylvania" "Pennsylvania" ...
##  $ City            : chr  "Philadelphia" "Philadelphia" "Philadelphia" "Philadelphia" ...
##  $ Gender.Type     : chr  "Men" "Women" "Men" "Men" ...
##  $ Product.Category: chr  "Apparel" "Apparel" "Street Footwear" "Athletic Footwear" ...
##  $ Price.per.Unit  : int  55 45 45 45 35 40 55 45 50 50 ...
##  $ Units.Sold      : int  125 225 475 125 175 50 125 225 445 150 ...
##  $ Total.Sales     : int  68750 101250 213750 56250 61250 20000 68750 101250 222500 75000 ...
##  $ Operating.Profit: num  24063 30375 117563 19688 24500 ...
##  $ Operating.Margin: num  0.35 0.3 0.55 0.35 0.4 0.4 0.35 0.3 0.55 0.35 ...
##  $ Sales.Method    : chr  "Outlet" "Outlet" "Outlet" "Outlet" ...
Adidas_Sales_2021 %>%
  plot_ly(x = ~Product.Category, y = ~Total.Sales, color = ~Region, 
          type = "bar",
          showlegend = FALSE,
          hoverinfo = "text",
          text = ~paste("Product Category:", Product.Category, "<br>",
                        "Total Sales:", Total.Sales, "<br>",
                        "Region:", Region),
          frame = ~Region) %>%
  animation_slider(hide = FALSE) %>%
  layout(title = "Total Sales by Product Category and Region",
         xaxis = list(title = "Product Category"),
         yaxis = list(title = "Total Sales ($)"))
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length

This animated plotly graph visualizes the total sales of Adidas products by product category and region. Each bar represents the total sales for a specific product category, with different colors indicating different regions. The animation cycles through the regions, allowing viewers to see how total sales vary across product categories within each region. One Challenge is that without the animation slider visible, it may be less intuitive for viewers to understand that the animation is cycling through different regions. Adding a visible animation slider hleps improve the user experience by providing clearer feedback on the progression through the categorical variable.