Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Codes for Meta Social Advertisements Blog
### Loading datasets
library(tidyverse)
library(DT)
library(viridis)
data = read_csv("/Users/harshvardhan/Downloads/FacebookAdLibraryReport_2022-05-22_US_lifelong/FacebookAdLibraryReport_2022-05-22_US_lifelong_advertisers.csv")
# Amounts lower than 100 USD are written as "<= 100". I will change them to 100 each.
data$`Amount spent (USD)`[data$`Amount spent (USD)` == "≤100"] = 100
data$`Amount spent (USD)` = as.numeric(data$`Amount spent (USD)`)
data = data %>%
group_by(`Page name`) %>%
summarise(`Amount spent (USD)` = sum(`Amount spent (USD)`),
`Number of ads in Library` = sum(`Number of ads in Library`))
### Plot
data %>%
arrange(desc(`Amount spent (USD)`)) %>%
slice_head(n = 30) %>%
ggplot(aes(x = reorder(`Page name`, `Amount spent (USD)`), y = `Amount spent (USD)`/10^6,
fill = factor(`Page name`), label = round(`Amount spent (USD)`/10^6))) +
geom_col() +
coord_flip() +
#scale_fill_viridis(discrete = TRUE, option = "D") +
scale_fill_manual(values = MetBrewer::met.brewer("Degas", 50)) +
scale_y_continuous(labels = scales::dollar_format(prefix="$", suffix = "M"),
limits = c(0, 125)) +
geom_text(size = 3, hjust = 1.5, col = "white") +
theme_linedraw() +
theme(legend.position = "none") +
labs(y = "Amount Spent on Meta Ads (in millions)",
x = "")
### Table
data %>%
arrange(desc(`Amount spent (USD)`)) %>%
slice_head(n = 10000) %>%
datatable(options = list(pageLength = 100)) %>%
formatCurrency(2, digits = 0) %>%
formatRound(3, digits = 0)
@harshvardhaniimi
Copy link
Author

harshvardhaniimi commented May 27, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment