
-------------
  Color := Campaign
  Size  := CPC
  Y     := CTR
  X     := Age
-------------

ep <- function(x) eval(parse(text=x), envir=parent.frame())

campns <- fb.agg[, Campaign]
campns <- fb.agg[, CampShort]

camps.selected <- campns[c(1:10, 59L, 17L, 16L, 24L, 73:100L, 32L, 37L)]

yMin <- .15
yMax <- 20
szVar  <- "Cost.Per.Click"
szVar  <- "Total.Campaign.Spend"
colVar <- "CampShort" # "Age"
yVar <- "ATR"
xVar <- "Age"
logY <- TRUE
dropNACamps <- TRUE
dropFirstNSizeLevs = 2
# xVar <- quote(as.factor(as.numeric(CampShort)))

# -------------------------------------------------------------------- #
# Identify which levels to drop
# -------------------------------------------------------------------- #
  levs <- fb.agg[, levels(get(szVar))]
  levsToDrop <- levs2[if (dropFirstNSizeLevs >= 1) seq(dropFirstNSizeLevs) else c()]
# -------------------------------------------------------------------- #

# -------------------------------------------------------------------- #
# Compute fb.using
# -------------------------------------------------------------------- #
  fb.using <- 
    if (length(levsToDrop)>0) {
      fb.agg[(!get(szVar) %in% levsToDrop) & get(yVar) > yMin & get(yVar) < yMax]
    } else
      fb.agg[get(yVar) > yMin & get(yVar) < yMax]
# -------------------------------------------------------------------- #

# -------------------------------------------------------------------- #
#  Log Labels for Y                                                                                          #
# -------------------------------------------------------------------- #
  ySampleMax <- fb.using[, max(get(yVar))]                                                                     
  yBreaks <- 2^seq(-8, log(ceiling(ySampleMax), 2))                                                            
  yLabels <- as.character(ifelse(yBreaks == round(yBreaks), round(yBreaks), fw3(round(yBreaks, 2), 2)))        
  if (grepl("^[AC]TR$", yVar, ignore.case=TRUE))                                                               
  yLabels <- paste(yLabels, "%")                                                                               
# -------------------------------------------------------------------- #


#.  if(dropNACamps)
#.    camps.selected <- intersect(camps.selected, fb.using[["CampShort"]] )
howManyLevs <- fb.using[, lunique(get(szVar))]

# -------------------------------------------------------------------- #
# G <- 
ggplot(data = fb.using, #[.(camps.selected)], 
#        aes(y=ep(yVar), x=ep(xVar), color=ep(colVar), size=exp(as.numeric(ep(szVar))))) +
        aes(y=ep(yVar), x=ep(xVar), color=ep(colVar), size=ep(szVar) )) +  # , shape=factor(floor(log(Spend, 10)))
#        aes_string(y=yVar, size=szVar, color=colVar)) + 
        geom_point(aes_string(x=xVar)) + 
        # geom_point(aes(x=as.factor(as.numeric(get(xVar))))) + 
        ylab(paste0(yVar, ifelse(logY, "\n (on a log scale)", ""))) + xlab(xVar)  +
        # turn off part of the legend
       {if (logY) scale_y_log10(breaks=yBreaks, labels=yLabels)} + 
       ## If size is continuous
       # scale_size_continuous(range=c(1,20)) + 
       scale_size_discrete(range=c(2,1.5*howManyLevs)) + 
       guides(color=FALSE, size=guide_legend(gsub("\\.", " ", szVar)))
# -------------------------------------------------------------------- #



       guides(size=gsub("\\.", " ", szVar), color="Campaign")

        # turn off part of the legend
        guides(colour=FALSE)

        + 
        theme(legend.position="bottom")


legendPlot <- g_legend(G)
ggplot(data=fb.using[.(camps.selected)])

legend <- 

ggplot(data=fb.agg, aes(size=`Cost.Per.Click`)) + 
    geom_point(aes(x=Age, y=eval(parse(text=yVar)))) + ylab(yVar)




ggplot(data=fb.using[.(camps.selected)][!is.na(Age)][order(`Cost.Per.Click`,decreasing=TRUE)], aes(size=`Cost.Per.Click`, color=`Cost.Per.Click`)) + # , color=Age)) + 
  geom_point(aes(x=eval(parse(text=xVar)), y=eval(parse(text=yVar)) )) + 
  ylab(yVar) + xlab(xVar)


ggplot(data=fb.using[.(camps.selected)][Age == "TOTAL"], aes(size=`Cost.Per.Click`, color=Age)) + 
  geom_point(aes(x=as.factor(as.numeric(Campaign)), y=eval(parse(text=yVar)))) + 
  ylab(yVar) + xlab("Campaign")


OLD: 
# ggplot(data=fb.using[.(camps.selected)], aes(size=`Cost.Per.Click`, color=Campaign)) + 
#   geom_point(aes(x=Age, y=eval(parse(text=yVar)))) + ylab(yVar) + .nolegend


# Remove anything with too few impressions or too few spend on the total campaign


Freq
Clicks
Spend



# 
# G <- 
# ggplot(data=datUsing[.(agg, TRUE)], aes(x=Date, y=value, color=Campaign)) + 
#   geom_point(size=1) + geom_line(size=0.2) +  theme(legend.position="bottom")
# 
# ggplot() +
# geom_point(data=totUsing[.(agg)], aes(x=Date, y=value), size=2, color="red") + 
#       geom_line(data=totUsing[.(agg)], aes(x=Date, y=value), size=1.2, alpha=0.5) + 
#       facet_grid(variable~.)
# 
