We will be looking at ice cream machines at McDonalds in the US. Whether they are active or broken. The data is sourced from Kaggle.
# I paste some code in here, maybe to identify all of the libraries I need to use and then to read in the data and to report some details about the data.
library(plyr)
library(ggplot2)
library(scales)
library(DescTools)
library(data.table)
library(dplyr)
library(RColorBrewer)
library(ggthemes)
library(plotly)
library(leaflet)
library(ggrepel)
# I paste some code in here if needed. This might be manipulation of the data after reading it in, to remove bad data, for example.
filename <- "mcdonalds_dataset.csv"
df <- fread(filename, na.strings = c(NA,""))
NARows <- which(is.na(df$state))
NACity <- which(is.na(df$city))
NA_and_NoCity <- c(NARows, NACity)
rows_to_drop <- setdiff(NA_and_NoCity, df)
df <- df[-rows_to_drop,]
active_by_state <- df %>%
filter(is_active == TRUE) %>%
select(state) %>%
group_by(state) %>%
summarize(n = n()) %>%
data.frame()
length(unique(df$state))
## [1] 51
top_states <- count(df, state)
top_states <- top_states[order(-n),]
top_states[top_states$state %in% c("CA", "TX"), "n"] / sum(top_states$n)
## n
## 1: 0.09102342
## 2: 0.08300582
state_df <- df %>%
select(state) %>%
mutate(MyState = ifelse(state== "CA", "CA", ifelse(state== "TX", "TX", "Other"))) %>%
group_by(MyState) %>%
summarise(n= length(MyState), .groups='keep') %>%
group_by(n) %>%
mutate(percent_of_total = round(100*n/sum(n),1)) %>%
ungroup() %>%
data.frame()
not_broken <- df %>%
filter(is_broken == FALSE) %>%
filter(state == "CA") %>%
select(lat, lon) %>%
group_by(lat, lon) %>%
summarize(n = n()) %>%
data.frame()
x_axis_labels = min(active_by_state$n):max(active_by_state$n)
hi_lo <- active_by_state %>%
filter(n == min(n) | n== max(n)) %>%
data.frame()
is_broken <- df %>%
filter(is_broken == TRUE) %>%
select(state) %>%
group_by(state) %>%
summarize(n = n()) %>%
data.frame()
summary_by_state <- df %>%
filter(state == "CA" | state == "IL" | state == "FL" | state == "TX") %>%
select(state, is_broken, is_active) %>%
group_by(state, is_broken, is_active) %>%
summarize(n = n(), .groups = 'keep') %>%
data.frame()
mylevels <- c('CA','FL','IL','TX')
summary_by_state$state <- factor(summary_by_state$state, levels = mylevels)
breaks <- c(seq(0, max(summary_by_state$n), by=50))
icon.glyphicon <- makeAwesomeIcon(icon='flag', markerColor = 'blue', iconColor = 'white')
p2 <- ggplot(active_by_state, aes(x = state, y = n)) +
geom_bar(stat = "identity") +
labs(title = "Count of Active McDonald's Ice Cream Machines by State",
x = "State", y = "Count of active ice cream machine")
p2
plot_ly(state_df, labels = ~MyState, values = ~n, type = "pie",
textposition = "outside", textinfo = "label + percent") %>%
layout(title="# of Active McDonald's Ice Cream Machines by State")
m <- leaflet() %>%
addTiles() %>%
addAwesomeMarkers(lng = not_broken$lat, lat = not_broken$lon,
icon=icon.glyphicon)
m
p3 <- ggplot(active_by_state, aes(x = state, y = n)) +
geom_line(color='black', size=1) +
geom_point(shape=21, size=4, color='red', fill='white') +
labs(x="State", y= "# of Active Machines", title="Active Ice Cream Machines at McDonalds", caption="Source:Kaggle") +
theme_light() +
theme(plot.title = element_text(hjust = .05)) +
geom_point(data = hi_lo, aes(x = state, y =n), shape=21, size=4, fill='red', color='red') +
geom_label_repel(aes(label= ifelse(n == max(n)| n == min(n), scales::comma(n),"")),
box.padding = 1,
point.padding = 1,
size=4,
color='Gray50',
segment.color = 'darkblue')
p3
g <- ggplot(summary_by_state, aes(x = is_active, y = state, fill=n)) +
geom_tile(color="black") +
geom_text(aes(label=comma(n))) +
coord_equal(ratio=1) +
labs(title="Heatmap: Top Active By State",
x = "Active",
y = "States",
fill = "Active Count") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) +
scale_y_discrete(limits = rev(levels(summary_by_state$state))) +
scale_fill_continuous(low="white", high="red", breaks = breaks) +
guides(fill = guide_legend(reverse=TRUE, override.aes=list(colour="black")))
g
We can see the active machines by state, the top states with active machines are California and Texas. Not surprisingly we can see that in California most of the active machines are by metro areas. Interestly enough both California and Texas also have the greatest number of broken machines.