Loading in the data:
The OJ data set contains many categorical and numeric observations regarding Citrus Hill and Minute Maid orange juice and their prices.
The treadmill data set contains information on customers who purchased treadmills
# OJ data set - OJ
library(ISLR)
# Treadmill data set
df <- read.csv("CardioGoodFitness.csv")
ggplot graph
library(ggplot2)
# Box plot of price differences by store
ggplot(OJ, aes(x = as.factor(StoreID), y = PriceDiff))+
geom_boxplot(fill = c("#f7b267","#f79d65","#f4845f","#f27059","#f25c54"))+
labs(title = "Price Differences in Orange Juice Brands in Different Stores",
x = "Store ID", y = "Price Difference (MM-CH)")
In this plot I investigated how the prices of Minute Maid (MM) and Citrus Hill (CH) differed across the different stores in this data set. Overall, it appears that MM is usually priced 20 cents higher than CH Orange Juice. However, there is many differences in variability between stores. Store 7 has the highest range of differences, which could be caused by extreme sales on one of the two products. Its also interesting to note that store 1 had the lowest difference, indicating that at one point the price of MM was over 60 cents lower than the price of CH.
Base R Graph
# creating a table to use in my graph
tbl <- table(OJ$Purchase, OJ$StoreID)
# Bar plot of the total purchases of CH and MM by store
barplot(tbl, beside = TRUE, main = "Purchases of Citrus Hill and Minute Maid",
ylab = "Number of Purchases", xlab = "Store ID",
col = c("#f7b267","#f25c54"), ylim = c(0,300), cex.axis = .8)
legend("top",legend = c("CH","MM"), fill = c("#f7b267","#f25c54"))
This plot looks to analyze if customers are buying Minute Maid or Citrus Hill orange juice more often and if there are differences for the buying patterns for each store. For stores 1-3, it looks like the sales for each brand are relatively close. Although store 1 shoppers tend to buy Citrus Hill more often, shoppers at stores 2 and 3 tend to buy more Minute Maid. When looking at graphs 4 and 7, there is an overwhelming majority of shoppers that buy Cirtus Hill orange juice over minute maid orange juice. This is interesting to see, especially when the prior graph showed that the difference in prices is usually the same across all stores. After looking at this graph we can see that stores 7 and 4 sell much more Citrus Hill Orange Juice than Minute Maid and this is something noteworty to look into.
Plotly Graph - 1
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## 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
# plotly
# making plotly graph 1
plot_ly(data = df, x = ~Income, y = ~Miles, color = ~Product ,
type = "scatter", colors = c("#edae49","#d1495b","#00798c")) %>%
# adding titles and axis labels
layout(title = "Expected Running Miles vs Income", xaxis = list(title = "Income"), yaxis = list(title = "Miles Expected to Run"), legend = list(title = list(text = "Product"))) %>%
# creating custom hover elements
add_trace(type = "scatter", mode = "markers", showlegend = FALSE, x = df$Income, marker = list(size = 10), y = df$Miles, text = df$Product, hovertemplate = paste(
"<b>%{text}</b><br>",
"Miles: %{y:,.0f}<br>",
"Income: %{x:,}",
"<extra></extra>"
))
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
The goal of this visualization is to see if there are any patterns between a person’s income, the number of miles that they expect to run on their purchased treadmill, and what type of treadmill is purchased. The fist thing to note is that there is a pattern between those two variables. As a person’s income increases, the number of miles they plan to run also increases. We can also see what product a person bought based on the color of the point. As a persons income goes up they are more likely to buy the TM798 treadmill. The other two treadmill types appear to appeal more to lower income buyers. Based on the information in the graph, it makes sense that buyers with a higher income expect to run more miles. According to the graph, it appears that the higher income buyers are buying (what appears to be) the more expensive treadmill and therefore would expect to run more miles to justify the expensive purchase. This is just a theory that would need to be investigated further, but overall we can see that there is a pattern between a persons income, their expected running miles, and the type of treadmill purchased.
Plotly Graph - 2
# creating a tble for the graph
tbl <- table(df$Usage, df$MaritalStatus)
proptbl <- prop.table(tbl,2)
dftb<- as.data.frame.matrix(proptbl)
dftb$Usage <- c(2:7)
# making the graph
plot_ly(dftb, x =~Usage, y =~Partnered, type = "bar", name = "Partnered") %>%
add_trace(y=~Single, name = "Single") %>%
layout(title = "Comparing Goal Number of Uses and Marital Status", xaxis = list(title = "Number of Desired Uses"), yaxis = list(title = "Proportion"), legend = list(title = list(text = "Marital Status")), barmode = 'group')
For this visualization, I was interested in examining the number of projected treadmill uses for single buyers and partnered buyers. To do this I decided to use a proportion bar plot, where the y axis shows the proportion of partnered/single buyers who’s goal is to run on the treadmill twice per week, three times per week, etc. From the graph, we can see that both graphs follow the same trend that is skewed to the right and has a peak at 3 times per week. However it can be noted that more singe buyers said 3 or 4 than partnered buyers. However, the partnered buyers were more likely to say 2 times a week, 6 times a week, and 7 times a week than the single buyers. Overall, we can see that regardless of marital status, most buyers have a goal to run on their purchased treadmill 3-4 times per week. Based on the last visualization, it would be interesting to see if the “Uses” feature is impacted by Income at all since the “Miles” feature was.