STAA 566 Dynamic Graphs

Author

EmilyStarer

Ingest Data Source

The data source is marketing media data comparing different campaign types grouped by placement. Performance KPIs are measured through Sales, Spend and ROAS. ROAS is defined as Return on Ad Spend. The Data is reported on a monthly cadence.

#read in data
library(readr)
mediadata <- read.csv("MediaData.csv")
head(mediadata,10)
     Date                 Campaign ROAS Sales Spend
1  1/1/21                  BRANDED   28  1192  7742
2  1/1/21                    LOCAL    3    27  1472
3  1/1/21                 RLSA/DSA   13    29   339
4  1/1/21 SHOPPING/PERFORMANCE MAX   19   251  1462
5  2/1/21                  BRANDED   23   905  6560
6  2/1/21                    LOCAL    3    22  1312
7  2/1/21                 RLSA/DSA   54   114   328
8  2/1/21 SHOPPING/PERFORMANCE MAX   19   278  1421
9  3/1/21                  BRANDED   43   926  3173
10 3/1/21                    LOCAL    5    26   944
#install packages 
library(knitr)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5     ✓ dplyr   1.0.7
✓ tibble  3.1.3     ✓ stringr 1.4.0
✓ tidyr   1.1.3     ✓ forcats 0.5.1
✓ purrr   0.3.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(ggpubr)
library("tidyverse")
library("dplyr")
library("ggplot2")
library(MASS)

Attaching package: 'MASS'
The following object is masked from 'package:dplyr':

    select
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:MASS':

    select
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(htmlwidgets)
library(ggthemes)

Graph

The graph shows sales compared to ROAS with the campaign type as color. The graph is showing the efficiency of the media (y axis) compared to the volume of sales (x-axis). Trend lines and smoothed lines are added to visualize the trend. The graph is trying to show how the different campaign types compared around efficiency and effectiveness.

#make ggplot
media_plot <- ggplot(data=mediadata,
                     mapping=aes(x=Sales,
                                 y=ROAS,
                                 color=Campaign)) + 
              geom_point(aes(size=Spend),
                                  alpha=.5,
                                  shape=16) +
              geom_smooth(method="loess")
                     
#scale axis
media_plot <- media_plot + scale_y_continuous(breaks = seq(0,60,by=10))


media_plot <- media_plot + scale_x_continuous(breaks = seq(0,7500,by=1000))

#adjust legends
media_plot <- media_plot + guides(size=guide_legend(title="Amount Spent"),
                    color=guide_legend(title="Campaign Type"))

#add title
media_plot <- media_plot + labs(title="Sales Performance by Campaign Type",
                                subtitle=strwrap(paste0("Campaigns based on targeting criteria and placement. Return on Ad Spend calculated as Sales / Media Spend.")))

Dynamic Features Added

A click selector and range toggle were added. The highlight click selector utilizes the legend to remove campaigns types from the visual. This easily allows comparison of 2 or 3 of the campaign types. This also allows dynamic ability for only 1 of the campaign types to be visible at a time. The range toggle allows users to zoom in on the graph. This is helpful when selecting a campaign type that is very concentrated in one area. The user can adjust the zoom then select only one of the campaign types and easily see the trend. This function is required when campaign types are on opposite ends of the scale.

#convert to plotly
p_media_plot <- ggplotly(media_plot)
`geom_smooth()` using formula 'y ~ x'
#adjust tooltip
p_media_plot <- ggplotly(p_media_plot, tooltip = "Campaign")

#add highlight on Campaign Type and click functionality from the legend  
p_media_plot <- highlight_key(p_media_plot, ~Campaign)

#Add Stretch of Time to be able to focus in on smaller sales windows 
p_media_plot <- p_media_plot %>% layout(
      title=NA,
      xaxis = list(
    rangeselector = list(
      buttons = list(
        list(
          count = 3,
          label = "3 mo",
          step = "month",
          stepmode = "backward"),
        list(
          count = 6,
          label = "6 mo",
          step = "month",
          stepmode = "backward"),
        list(
          count = 1,
          label = "1 yr",
          step = "year",
          stepmode = "backward"),
        list(
          count = 1,
          label = "YTD",
          step = "year",
          stepmode = "todate"),
        list(step = "all"))),
  rangeslider = list(type = "date")))

Save Figure as pdf

pdf("Media Data.pdf", height=10, width=10)
print(p_media_plot)
dev.off()
quartz_off_screen 
                2 

Display Figure in HTML

p_media_plot