knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.5 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.0
## ✔ readr 2.1.3 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
theme_set(theme_bw())
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(rnaturalearth)
library(rnaturalearthdata)
library(scico)
df.retail <- read.csv("online_retail.csv")
#Find out the best selling product
df.retail %>%
group_by(StockCode, Description) %>%
summarise(sales = sum(Quantity, na.rm = TRUE),
revenue = sum(Quantity * UnitPrice, na.rm = TRUE)) %>%
ungroup() %>%
arrange(desc(sales)) %>%
slice_head(n = 1) %>%
kable()
## `summarise()` has grouped output by 'StockCode'. You can override using the
## `.groups` argument.
| 84077 |
WORLD WAR 2 GLIDERS ASSTD DESIGNS |
53847 |
13587.93 |
#The top 10 countries contributing to the sales of the best selling product
df.bs <- subset(df.retail, StockCode == 84077) %>%
group_by(Country) %>%
summarise(sales = sum(Quantity, na.rm = TRUE),
revenue = sum(Quantity * UnitPrice, na.rm = TRUE)) %>%
slice_head(n = 10) %>%
arrange(desc(sales)) %>%
ungroup()
#Find out the product with the highest revenue
df.retail %>%
group_by(StockCode, Description) %>%
summarise(sales = sum(Quantity, na.rm = TRUE),
revenue = sum(Quantity * UnitPrice, na.rm = TRUE)) %>%
ungroup() %>%
arrange(desc(revenue)) %>%
slice_head(n = 1) %>%
kable()
## `summarise()` has grouped output by 'StockCode'. You can override using the
## `.groups` argument.
| DOT |
DOTCOM POSTAGE |
707 |
206245.5 |
#Make a barplot for the top 10 countries
ggplot(data = df.bs, mapping = aes(x = Country, y = sales, fill = sales)) +
geom_col() +
scale_fill_gradient(low = "#b1f4cf", high = "#9890e3") +
labs(title = "Top 10 Countries Contributing to Best Selling Product")

#Plot a map - worldwide
df.bs1 <- df.bs %>%
mutate(Country = str_replace_all(Country, fixed("EIRE"), "Ireland"))
world_map <- map_data('world') %>%
left_join(df.bs1,
by = c('region' = 'Country'))
ggplot() +
geom_map(
data = world_map,
map = world_map,
aes(x = long, y = lat, map_id = region, fill = sales),
color = 'black'
) +
theme_bw() +
labs(fill = 'Topselling Contries Worldwide') +
theme(legend.position = 'bottom',
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
scico::scale_fill_scico(palette = "devon")
## Warning: Ignoring unknown aesthetics: x, y

#Plot a map - zoom in to Central Europe
world <- map_data("world")
europe <- subset(world, region %in% c("Albania", "Andorra", "Armenia", "Austria",
"Azerbaijan",
"Belarus", "Belgium", "Bosnia and Herzegovina", "Bulgaria",
"Croatia", "Cyprus", "Czechia", "Denmark", "Estonia",
"Finland", "France", "Georgia", "Germany", "Greece",
"Hungary", "Iceland", "Ireland", "Italy", "Kazakhstan",
"Kosovo", "Latvia", "Liechtenstein",
"Lithuania", "Luxembourg", "Malta", "Moldova", "Monaco",
"Montenegro", "Macedonia", "Netherlands", "Norway",
"Poland", "Portugal", "Romania", "San Marino",
"Serbia", "Slovakia", "Slovenia", "Spain",
"Sweden", "Switzerland", "Turkey", "Ukraine", "UK",
"Vatican"))
europe_map <- europe %>%
left_join(df.bs1,
by = c('region' = 'Country'))
ggplot() +
geom_map(
data = europe_map,
map = europe_map,
aes(x = long, y = lat, map_id = region, fill = sales),
color = 'black'
) +
theme_bw() +
labs(fill = 'Topselling Countries in Europe (Excluding Russia)') + #excluding Russia to better display the plot of Central Europe
theme(legend.position = 'bottom',
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
scico::scale_fill_scico(palette = "devon")
## Warning: Ignoring unknown aesthetics: x, y
