#upload libraries
library(ggplot2)
library(datasets)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(skimr)
library(dplyr)
library(knitr)
library(directlabels)
library(cowplot)
##
## Attaching package: 'cowplot'
##
## The following object is masked from 'package:lubridate':
##
## stamp
library(ggrepel)
library(dplyr)
library(forcats)
#upload the fastfood_sales document
d <- read.csv("data_fastfood_sales.csv")
#check the data file
head(d)
## restaurant average_sales us_sales num_company_stores num_franchised_stores
## 1 Subway 416.86 10800.00 0 25908
## 2 Mcdonalds 2670.32 37480.67 842 13194
## 3 Starbucks 945.27 13167.61 8222 5708
## 4 Dunkin Donuts 733.13 9192.00 0 12538
## 5 Pizza Hut 900.00 5510.84 96 7426
## 6 Burger King 1387.81 10028.32 50 7196
## unit_count
## 1 25908
## 2 14036
## 3 13930
## 4 12538
## 5 7522
## 6 7266
dim(d)
## [1] 19 6
colnames(d)
## [1] "restaurant" "average_sales" "us_sales"
## [4] "num_company_stores" "num_franchised_stores" "unit_count"
#take out the required variables in the dataset
restaurant = d[, "restaurant"]
avg_sales = d[, "average_sales"]
us_sales = d[, "us_sales"]
num_comp_stores = d[, "num_company_stores"]
num_franch_stores = d[, "num_franchised_stores"]
unit_count = d[, "unit_count"]
avg_sales <- d$average_sales * 1000
aes(reorder(Name, Value), Value)
## Aesthetic mapping:
## * `x` -> `reorder(Name, Value)`
## * `y` -> `Value`
d$average_sales
## [1] 416.86 2670.32 945.27 733.13 900.00 1387.81 1500.00 1610.00 1000.00
## [10] 815.00 1200.00 1250.00 1130.00 968.45 796.97 360.72 1940.00 1543.00
## [19] 1440.19
plotdata <- d %>% select(average_sales, restaurant) %>%
group_by(restaurant) %>% mutate(avg_sale = average_sales * 1000, digits = 0)%>% mutate(round(avg_sale, digits = 0)) %>%
mutate(as = reorder(avg_sale, restaurant))
## Warning: There were 19 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `as = reorder(avg_sale, restaurant)`.
## ℹ In group 1: `restaurant = "Arbys"`.
## Caused by warning in `mean.default()`:
## ! argument is not numeric or logical: returning NA
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 18 remaining warnings.
# Create factor column with decreasing order TRUE
plotdata$restaurant <- factor(plotdata$restaurant, levels = plotdata$restaurant[order(plotdata$avg_sale, decreasing = FALSE)])
p <- ggplot(plotdata, aes( y = round(avg_sale, digits = 0), x= restaurant))
p + geom_bar(stat = "identity", position = 'dodge')+
coord_flip()+
ylab("Average sales per unit store (in thousands)") +
xlab("Restaurant")+
scale_y_continuous(labels = scales::label_dollar()) +
theme_classic() +
geom_text(aes(label = scales::dollar(round(average_sales,digits = 0)), hjust = 0))
