-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ari Lamstein encouraged edits including movement of graphical code to…
… separate function
- Loading branch information
Showing
6 changed files
with
101 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' Generates ggplot histogram and add'l statistics for fbadGstats | ||
#' @description More text coming. | ||
#' @param dmastat_all post-processed data frame from fbadGstats | ||
#' @param prtrow text | ||
#' @param grpvar text | ||
#' @param grpvarprt text | ||
#' @param sumnam text | ||
#' @param todaydt text | ||
#' @param file_nam text | ||
#' @param spentlim text | ||
#' @param sumprtvar text | ||
#' | ||
#' @return A graph with a complementary table for the best performers | ||
#' @importFrom ggplot2 ggplot aes scale_x_discrete scale_y_continuous geom_col | ||
#' @importFrom ggplot2 labs xlab ylab theme element_text ggplotGrob geom_text | ||
#' @importFrom gridExtra grid.arrange tableGrob ttheme_minimal arrangeGrob | ||
#' @importFrom stringr str_c str_to_upper str_subset str_trim | ||
#' @importFrom dplyr select %>% as_tibble pull filter contains quo group_by | ||
#' @importFrom dplyr summarize mutate_if funs distinct arrange min_rank | ||
#' @importFrom dplyr left_join inner_join mutate slice trunc_mat tbl_df | ||
#' @export | ||
graphads <- function(dmastat_all, prtrow, grpvar, grpvarprt, sumnam, todaydt, file_nam, | ||
spentlim, sumprtvar){ | ||
# Capture the best breakdown groups in sumevtavg | ||
sumevtavg <- arrange(dmastat_all, rnkevt) | ||
statset <- sumevtavg %>% slice(1:min(prtrow, 8)) %>% filter(sumevt > 0) | ||
sumevtavg_gt0 <- sumevtavg %>% filter(sumevt > 0) | ||
medtop <- round(median(statset$costevt), 1) | ||
medall <- round(median(sumevtavg_gt0$costevt), 1) | ||
medspent <- round(median(statset$sumspent)) | ||
plotforms <- ggplot(statset) + | ||
aes(x = (statset %>% pull(!!grpvar)), | ||
y = costevt, | ||
fill = (statset %>% pull(!!grpvar))) + | ||
scale_x_discrete(limits = statset %>% | ||
pull(!!grpvar)) + | ||
scale_y_continuous( | ||
labels = scales::dollar) + | ||
geom_col(show.legend = FALSE) + | ||
labs(title = paste("Facebook Ads Analysis for ", | ||
sumnam, ": Created on ", todaydt, | ||
sep = ""), | ||
caption = paste("Data from ", | ||
file_nam, | ||
sep = "")) + | ||
theme(plot.title = | ||
element_text(hjust = 0.5)) + | ||
xlab(str_c("Best performing (lowest cost) ", grpvarprt)) + | ||
ylab(str_c("Cost per ", sumnam)) + | ||
geom_text(aes(label = paste0("$",costevt,"; Spent = $", | ||
sumspent)), vjust = 0) | ||
plotformsG <- ggplotGrob(plotforms) | ||
extrainfo <- paste("Median cost (all) only considers where | ||
there was\nat least one ", sumnam, sep = "") | ||
stat_tbl <- data.frame(medtop, medall, medspent, | ||
spentlim, extrainfo) | ||
colnames(stat_tbl) <- c(str_c("Median cost\nper '", sumprtvar, | ||
"'\n for (all)"), | ||
"Median amount spent\n", | ||
"(graphed best performers)", | ||
"(spentlim parameter)", "INFO:") | ||
tt <- ttheme_minimal(base_size = 10) | ||
# print without print function to avoid undesired extraneous output | ||
grid.arrange(grobs = list(plotforms, | ||
tableGrob(stat_tbl, theme = tt, | ||
rows = NULL)), as.table = TRUE, | ||
heights = c(3, 1)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
library(testthat) | ||
library(fbadstats) | ||
test_check("fbadstats") | ||
#library(testthat) | ||
#library(fbadstats) | ||
#test_check("fbadstats") |