Skip to content

Commit

Permalink
Ari Lamstein encouraged edits including movement of graphical code to…
Browse files Browse the repository at this point in the history
… separate function
  • Loading branch information
RickPack committed Oct 11, 2017
1 parent b5f8a25 commit 0e864b6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 57 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(fbadGstats)
export(graphads)
importFrom(dplyr,"%>%")
importFrom(dplyr,arrange)
importFrom(dplyr,as_tibble)
Expand Down Expand Up @@ -41,4 +42,3 @@ importFrom(stringr,str_c)
importFrom(stringr,str_subset)
importFrom(stringr,str_to_upper)
importFrom(stringr,str_trim)
importFrom(xtable,xtable)
55 changes: 3 additions & 52 deletions R/fbadGstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@
#' fbadGstats(filerd='example_DMA.csv', sumvar='REG')
#' # see more examples with:
#' vignette(package = "fbadstats")
#' }
#' fbadGstats('example_DMA.csv', sumvar = "CLICKS", filtervar = 'Book',
#' spentlim = 10, minevent = 2, prtrow = 3, tblout = "WORST",
#' graphout = "NO", ctrstats = "NO")
#' @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
#' @importFrom xtable xtable
#' @export
fbadGstats <- function(filerd = "", choosedir = "NO", sumvar = "",
filtervar = "", filtervarneg = "", spentlim = 0,
Expand Down Expand Up @@ -390,53 +386,8 @@ fbadGstats <- function(filerd = "", choosedir = "NO", sumvar = "",

# Graph section ----------------------------
if (grphoutTF == TRUE) {
# 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(graphed best performers)"),
str_c("Median cost\nper '", sumprtvar,
"'\n for (all)"),
"Median amount spent\n",
"(graphed best performers)",
"Minimum amount spent\nthreshold",
"(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))
graphads(dmastat_all, prtrow, grpvar, grpvarprt, sumnam,
todaydt, file_nam, spentlim, sumprtvar)
}
}
# restore stringsAsFactors option to original value
Expand Down
68 changes: 68 additions & 0 deletions R/graphads.R
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))
}
2 changes: 1 addition & 1 deletion man/fbadGstats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/graphads.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat.R
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")

0 comments on commit 0e864b6

Please sign in to comment.