Skip to content

Instantly share code, notes, and snippets.

@harshvardhaniimi
Created November 11, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshvardhaniimi/2d47c68f1d2e06778c3a1094b1a0acc7 to your computer and use it in GitHub Desktop.
Save harshvardhaniimi/2d47c68f1d2e06778c3a1094b1a0acc7 to your computer and use it in GitHub Desktop.
Ratio of baby diapers sale to adult diapers sale
library(tidyverse)
library(readxl)
library(MetBrewer)
theme_set(ggthemes::theme_clean())
setwd("/Users/harshvardhan/Desktop/Dump/diapers/")
# its funny how something so simple requires such ninja skills
# 1. Data from Statista is sometimes in millions, sometimes in billions
# 2. You cannot download more than three countries of data at a time
# 3. Even if you have the premium (Thanks UTK for the Statista Premium access! Libraries are best)
# babies come in crying loud!
baby = read_excel("data.xlsx", sheet = "Baby Diapers") %>%
janitor::clean_names() %>%
pivot_longer(!year, names_to = "country", values_to = "baby") %>%
mutate(baby = as.numeric(baby),
year = as.numeric(year))
baby
# adults go out silently on a life support, sed...
adult = read_excel("data.xlsx", sheet = "Incontinence") %>%
janitor::clean_names() %>%
pivot_longer(!year, names_to = "country", values_to = "adult") %>%
mutate(adult = as.numeric(adult),
year = as.numeric(year))
adult
# baby + adult = huggies
diapers = inner_join(baby, adult, by = c("year", "country")) %>%
pivot_longer(!c(year, country), names_to = "type", values_to = "volume") %>%
mutate(volume = as.numeric(volume))
diapers %>%
garlic::show_in_excel()
countries = unique(diapers$country)
# ggplot2
diapers %>%
filter(country == "india") %>%
ggplot(aes(x = year, y = volume, fill = type)) +
geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75) +
#scale_fill_discrete(labels = c("Adult", "Baby")) +
scale_fill_manual(labels = c("Adult", "Baby"),
values = met.brewer("Java", 2)) + #Java is okay
#MetBrewer::scale_color_met_d("VanGogh3") +
labs(x = "Year", y = "Sales in million USD",
fill = "Type")
plot_country = function(oc)
{
p = diapers %>%
filter(country == oc) %>%
ggplot(aes(x = year, y = volume, fill = type)) +
geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75) +
# See MetBrewer::colorblind_palettes
scale_fill_manual(labels = c("Adult Diapers", "Baby Diapers"),
#Java is okay
values = met.brewer("Java", 2)) +
scale_x_continuous(breaks=seq(2014, 2027, 1)) +
labs(x = "Year", y = "Sales in million USD",
fill = "Type", title = toupper(oc))
ggsave(paste0("plots/", oc, ".png"), plot = p)
return(p)
}
plot_country("china")
lapply(countries, plot_country)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment