This is an R Markdown document about Plotly.
Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
Plotly’s R graphing library makes interactive, publication-quality graphs online. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, and 3D (WebGL based) charts. For more details on using R Plotly see https://plot.ly/r/.
Read in a csv file for the keyword quality score in an initaitve during the before and after periods:
#setwd("C:/Users/lshi/Desktop/QS_initiatives_monitoring")
kw_QS_before_after <- read.csv("C:/Users/lshi/Desktop/QS_initiatives_monitoring/kw_QS_before_after_ 453 .csv")
Some basic scatter plots for the keyword quality score during the before and after periods respectively:
library(plotly)
# scatter plot
plot_ly(x = kw_QS_before_after$KEYWORD, y = kw_QS_before_after$QS_before, type = "scatter", mode = "markers") %>%
layout(title = 'keyword quality score during the before period', xaxis = list(title = "keyword"), yaxis = list(title = "QS"))
plot_ly(x = kw_QS_before_after$KEYWORD, y = kw_QS_before_after$QS, type = "scatter", mode = "markers")%>%
layout(title = 'keyword quality score during the after period', xaxis = list(title = "keyword"), yaxis = list(title = "QS"))
Overlay those scatter plots so that we can see how the quality score changes for the same keyword:
library(plotly)
# scatter plots overlay
plot_ly(x = kw_QS_before_after$KEYWORD, y = kw_QS_before_after$QS_before, type = "scatter", mode = "markers", opacity = 0.5, name = "QS before") %>%
add_trace(x = kw_QS_before_after$KEYWORD, y = kw_QS_before_after$QS, type = "scatter", opacity = 0.5, name = "QS after") %>%
layout(barmode = "overlay", title = 'keyword quality score during the before and after periods', xaxis = list(title = "keyword"), yaxis = list(title = "QS"))
Overlay the histograms to see how the keywords quality scores change after initiative started:
library(plotly)
# histograms overlay
plot_ly(kw_QS_before_after, type = "histogram", x = kw_QS_before_after$QS_before, opacity = 0.5, name = "before") %>%
add_trace(x = kw_QS_before_after$QS, type = "histogram", opacity = 0.5, name = "after") %>%
layout(barmode = "overlay", title = 'keyword histogram on quality score during the before and after periods', xaxis = list(title = "QS"), yaxis = list(title = "# of keywords"))
Read the csv file for the daily performance data on keyword level:
# daily QS for keyword level in the initiative
daily_QS_KW <- read.csv("C:/Users/lshi/Desktop/QS_initiatives_monitoring/daily_QS_KW_453.csv")
daily_QS_KW$DATE = as.Date(daily_QS_KW$DATE, format = "%m/%d/%Y")
# particular KWs in the initiative
daily_QS_KW_1 <- daily_QS_KW[which(daily_QS_KW$KEYWORD == "get a domain name"),]
daily_QS_KW_2 <- daily_QS_KW[which(daily_QS_KW$KEYWORD == "hosting services"),]
daily_QS_KW_3 <- daily_QS_KW[which(daily_QS_KW$KEYWORD == "best hosting sites"),]
# combine the daily data for those KWs
a <- data.frame(daily_QS_KW_1$DATE, daily_QS_KW_1$QS, daily_QS_KW_1$max_CPC, daily_QS_KW_2$QS, daily_QS_KW_2$max_CPC, daily_QS_KW_3$QS, daily_QS_KW_3$max_CPC)
names(a) <- c("date", "QS1", "max_CPC1", "QS2", "max_CPC2", "QS3", "max_CPC3")
Overlay the line charts to show how max CPC changes over time for different keywords:
library(plotly)
# overlay line charts for 3 keywords' daily max CPCs
plot_ly(a, x = ~date, y = ~max_CPC1, name = 'get a domain name', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~max_CPC2, name = 'hosting services', mode = 'lines') %>%
add_trace(y = ~max_CPC3, name = 'best hosting sites', mode = 'lines')
Overlay the line charts to show how quality score changes over time for different keywords:
library(plotly)
# overlay line charts for 3 keywords' daily quality scores
plot_ly(a, x = ~date, y = ~QS1, name = 'get a domain name', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~QS2, name = 'hosting services', mode = 'lines') %>%
add_trace(y = ~QS3, name = 'best hosting sites', mode = 'lines')
Read in the csv file for the weekly performance data:
# weekly performance data for keyword level in the initiative
weekly_perf_KW <- read.csv("C:/Users/lshi/Desktop/QS_initiatives_monitoring/weeklydata_3KWs_453.csv")
Mapping data to symbols to show the weekly CTR vs pos relationship for different keywords on 2-dimention plot:
library(plotly)
# overlay weekly CTR vs pos scatter plots
plot_ly(data = weekly_perf_KW, x = ~POS, y = ~CTR, type = 'scatter',
mode = 'markers', symbol = ~KEYWORD, symbols = c('circle','x','o'),
color = I('black'), marker = list(size = 10))
Color shows the time change on the scatter plot:
library(plotly)
plot_ly(data = weekly_perf_KW, x = ~POS, y = ~CTR, type = 'scatter',
mode = 'markers', symbol = ~KEYWORD, symbols = c('circle','x','o'),
color = ~WWEEK, marker = list(size = 10))
Color shows the clicks on the scatter plot:
library(plotly)
plot_ly(data = weekly_perf_KW, x = ~POS, y = ~CTR, type = 'scatter',
mode = 'markers', symbol = ~KEYWORD, symbols = c('circle','x','o'),
color = ~CLICKS, marker = list(size = 10))
Color shows the clicks for a specific keyword on the scatter plot:
library(plotly)
plot_ly(data = weekly_perf_KW[which(weekly_perf_KW$KEYWORD == "get a domain name"),], x = ~POS, y = ~CTR, type = 'scatter', mode = 'markers', color = ~CLICKS, marker = list(size = 10))
Scatter plot sizing to show the weekly CTR vs pos relationship for all keywords proportional to weekly conversions:
library(plotly)
# scatter plot sizing
plot_ly(data = weekly_perf_KW, x = ~POS, y = ~CTR, type = 'scatter',
mode = 'markers', color = as.factor(weekly_perf_KW$KEYWORD), size = ~CONV)
3D scatter plot to show the CTR vs pos relationship for different keywords on 3-dimention plot:
library(plotly)
# 3D scatter plot
plot_ly(x = weekly_perf_KW$KEYWORD, y = weekly_perf_KW$POS, z = weekly_perf_KW$CTR, type = "scatter3d", mode = "markers", color = weekly_perf_KW$KEYWORD) %>%
layout(title = "weekly CTR vs POS in a 3d scatter plot",
scene = list(
xaxis = list(title = "Keyword"),
yaxis = list(title = "Pos"),
zaxis = list(title = "CTR")))
3D scatter plot to show the CPC vs pos relationship for different keywords on 3-dimention plot:
library(plotly)
# 3D scatter plot
plot_ly(x = weekly_perf_KW$KEYWORD, y = weekly_perf_KW$POS, z = weekly_perf_KW$CPC, type = "scatter3d", mode = "markers", color = weekly_perf_KW$KEYWORD) %>%
layout(title = "weekly CPC vs POS in a 3d scatter plot",
scene = list(
xaxis = list(title = "Keyword"),
yaxis = list(title = "Pos"),
zaxis = list(title = "CPC")))
3D scatter plot to show the CTR vs CPC vs pos relationship for different keywords on 3-dimention plot:
library(plotly)
plot_ly(x = weekly_perf_KW$CTR, y = weekly_perf_KW$POS, z = weekly_perf_KW$CPC, type = "scatter3d", mode = "markers", color = weekly_perf_KW$KEYWORD) %>%
layout(title = "weekly CTR vs CPC vs POS in a 3d scatter plot",
scene = list(
xaxis = list(title = "CTR"),
yaxis = list(title = "Pos"),
zaxis = list(title = "CPC")))
Pie chart to show the total impression for different keywords in 2016:
library(plotly)
# pie chart
imps_by_keyword <- aggregate(IMPS ~ KEYWORD, weekly_perf_KW, sum)
plot_ly(imps_by_keyword, labels = ~KEYWORD, values = ~IMPS, type = 'pie') %>%
layout(title = 'Total Impressions by Keywords in 2016',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
# styled pie chart
plot_ly(imps_by_keyword, labels = ~KEYWORD, values = ~IMPS, type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
text = ~paste(IMPS, ' impressions'),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1)),
#The 'pull' attribute can also be used to create space between the sectors
showlegend = FALSE) %>%
layout(title = 'Total Impressions by Keywords in 2016',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
Donut chart to show how the total weekly impressions change over time in 2016:
library(plotly)
# Donut chart
p <- aggregate(IMPS ~ WWEEK, weekly_perf_KW, sum)
plot_ly(p, labels = ~WWEEK, values = ~IMPS) %>%
add_pie(hole = 0.5) %>%
layout(title = "Donut charts for weekly impressions", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
Overlay the Box plots to show the distributions of the weekly impressions for different keywords in 2016:
library(plotly)
# Box plot
plot_ly(type = 'box') %>%
add_boxplot(y = weekly_perf_KW$IMPS[which(weekly_perf_KW$KEYWORD == "get a domain name")], name = "get a domain name") %>%
add_boxplot(y = weekly_perf_KW$IMPS[which(weekly_perf_KW$KEYWORD == "hosting services")], name = "hosting services") %>%
add_boxplot(y = weekly_perf_KW$IMPS[which(weekly_perf_KW$KEYWORD == "best hosting sites")], name = "best hosting sites")