library(rvest)
library(stringr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(plotly)
library(crosstalk)
#1225
#scrape the rotoguru site
dfs1225 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=25&year=2020&game=dk")
#extract data table
dfs1225 <- dfs1225 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1225 <- data.frame(dfs1225)

#remove top row
dfs1225 <- dfs1225[-1,]
#1226
#scrape the rotoguru site
dfs1226 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=26&year=2020&game=dk")
#extract data table
dfs1226 <- dfs1226 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1226 <- data.frame(dfs1226)

#remove top row
dfs1226 <- dfs1226[-1,]
#1227
#scrape the rotoguru site
dfs1227 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=27&year=2020&game=dk")
#extract data table
dfs1227 <- dfs1227 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1227 <- data.frame(dfs1227)

#remove top row
dfs1227 <- dfs1227[-1,]
#1228
#scrape the rotoguru site
dfs1228 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=28&year=2020&game=dk")
#extract data table
dfs1228 <- dfs1228 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1228 <- data.frame(dfs1228)

#remove top row
dfs1228 <- dfs1228[-1,]
#1229
#scrape the rotoguru site
dfs1229 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=29&year=2020&game=dk")
#extract data table
dfs1229 <- dfs1229 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1229 <- data.frame(dfs1229)

#remove top row
dfs1229 <- dfs1229[-1,]
#1230
#scrape the rotoguru site
dfs1230 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=30&year=2020&game=dk")
#extract data table
dfs1230 <- dfs1230 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1230 <- data.frame(dfs1230)

#remove top row
dfs1230 <- dfs1230[-1,]
#1231
#scrape the rotoguru site
dfs1231 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=12&day=31&year=2020&game=dk")
#extract data table
dfs1231 <- dfs1231 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs1231 <- data.frame(dfs1231)

#remove top row
dfs1231 <- dfs1231[-1,]
#0101
#scrape the rotoguru site
dfs0101 <- read_html("http://rotoguru1.com/cgi-bin/hyday.pl?mon=01&day=01&year=2021&game=dk")
#extract data table
dfs0101 <- dfs0101 %>%
        html_nodes("table") %>%
        .[9] %>%
        html_table(fill = TRUE)

#convert to df
dfs0101 <- data.frame(dfs0101)

#remove top row
dfs0101 <- dfs0101[-1,]
#bind rows
todate <- rbind(dfs1225, dfs1226, dfs1227, dfs1228, dfs1229, dfs1230, dfs1231, dfs0101)
#change col names
colnames(todate)[1] <- "Pos"
colnames(todate)[2] <- "Name"
colnames(todate)[3] <- "Pts"
colnames(todate)[4] <- "Salary"
colnames(todate)[5] <- "Team"
colnames(todate)[6] <- "Opp"
colnames(todate)[7] <- "Score"
colnames(todate)[8] <- "Min"
colnames(todate)[9] <- "Statline"
#remove non player data 
todate <- todate[ grep("RotoGuru", todate$Pos, invert = TRUE) , ]
todate <- todate[ grep("Unlisted", todate$Pos, invert = TRUE) , ]
todate <- todate[ grep("Guards", todate$Pos, invert = TRUE) , ]
todate <- todate[ grep("Centers", todate$Pos, invert = TRUE) , ]
todate <- todate[ grep("Forwards", todate$Pos, invert = TRUE) , ]
todate <- todate[ grep("DNP", todate$Min, invert = TRUE) , ]
#remove na minutes
todate <- todate %>%
        drop_na(Min)
#use gsub to remove $ and comma from monetary values prior to data conversion to numeric (or you will get error)
todate$Salary <- gsub("\\$", "", todate$Salary)
todate$Salary <- gsub("\\,", "", todate$Salary)
todate[, 3:4] <- lapply(todate[, 3:4], as.numeric)
#create a new column to capture Points over Salary
todate <- todate %>%
        mutate(Ppd = round(Pts/Salary,5))
todate <- todate %>%
  group_by(Name) %>%
  mutate(Avg = round(mean(Pts),2), Games=n())
#created a new todate df because of some NAs in salary. Need to figure out where NA's are coming from 
todate1 <- todate %>%
        drop_na(Salary)
#drop the ^ character - need to use \\ to drop special characters
todate1$Name <- gsub("\\^", "", todate1$Name)
today <- c("nyk", "ind", "okc", "orl", "cha", "phi", "cle", "atl", "tor", "no")

#filter for todays games
todate1 <- todate1 %>%
  filter(Team %in% today)

Using ggplot

#create a static plot
static <- todate1 %>%
        ggplot(aes(x= Salary, y=Pts, color = Ppd)) +
        geom_point()
static

using plotly

#add function using plotly 
ggplotly(static)
#Add opactity to the markers
todate1 %>%
        plot_ly(x=~Salary, y=~Pts) %>%
        add_markers(marker = list(opacity = 0.2))
#change the symbol
todate1 %>%
        plot_ly(x=~Salary, y=~Pts) %>%
        add_markers(marker = list(symbol = "circle-open")) # Change symbol
#Adding color as a factor for position
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Pos,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts)
                ) %>%
        add_markers()
#add a second polynomial smoother
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Pos,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary)) %>%
        add_markers() %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "DK Rulez")
#convert the position from a character string to a factor
todate1$Pos <- as.factor(todate1$Pos) 
todate1 %>%
        group_by(Pos) %>%
        do(p=plot_ly(., x= ~Salary, y= ~Pts, color = ~Pos, type="scatter", hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary))) %>%
        subplot(nrows=4, shareX= FALSE, shareY= TRUE)
#Adding color as a factor for Ppd and adding axis titles
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Ppd,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Team:", Team)) %>%
        add_markers() %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "DK Rulez")
m <- loess(Pts ~ Salary, data = todate1, span = 1.5)
#add a smoother
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Ppd,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Team:", Team)) %>%
        add_markers() %>%
        add_lines(y = ~fitted(m)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "DK Rulez")
m2 <- lm(Pts ~ poly(Salary, 2), data = todate1)
#add a second polynomial smoother
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Ppd,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers() %>%
        add_lines(y = ~fitted(m), name = "LOESS") %>%
        add_lines(y=~fitted(m2), name = "Polynomial") %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "DK Rulez")
todate1 %>%
        group_by(Pos) %>%
                do(p=plot_ly(., x= ~Salary, y= ~Ppd, color = ~Pos, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team))) %>%
        subplot(nrows=4, shareY=TRUE)
#change the size of the dot based on points per game metric (dk points/salary)
todate1 %>%
        plot_ly(x=~Salary, y=~Pts, color = ~Ppd,
                hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(size = ~Ppd) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "DK Rulez")
#breakdown by position - Center eligible
center <- todate1 %>%
        filter(Pos == "C"| Pos =="PF/C") 

cplot <- center %>%
        plot_ly(
                x = ~Salary, y = ~Pts, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers")
cplot
cplotavg <- center %>%
        plot_ly(
                x = ~Salary, y = ~Avg, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Average:", Avg, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "AVG DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers AVG")
cplotavg
#breakdown by position - Forward Eligible
forward <- todate1 %>%
        filter(Pos == "PF"| Pos =="PF/C" | Pos == "SF" | Pos == "SF/PF" | Pos == "SG/SF" | Pos == "PG/SF")

fplot <- forward %>%
        plot_ly(
                x = ~Salary, y = ~Pts, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Forwards")
fplot
fplotavg <- forward %>%
        plot_ly(
                x = ~Salary, y = ~Avg, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Average:", Avg, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "AVG DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Forwards AVG")
fplotavg
#breakdown by position - Guard Eligible
guard <- todate1 %>%
        filter(Pos == "PG"| Pos =="PG/SF" | Pos == "PG/SG" | Pos == "SG" | Pos == "SG/SF")

gplot <- guard %>%
        plot_ly(
                x = ~Salary, y = ~Pts, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Guards")
gplot
gplotavg <- guard %>%
        plot_ly(
                x = ~Salary, y = ~Avg, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Average:", Avg, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "AVG DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Guards AVG")
gplotavg
#Create an animated object
ani <- center %>%
        plot_ly(x = ~Salary, y = ~Pts, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(frame = ~Pts, ids=~Name, showlegend = FALSE) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "DK Points", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers")

Run Transition Animation from Lowest to Highest DK Points based on Salary

#toggle with animation options
#transition speed - lower is faster, must be lower than frame
#hide slider
ani %>%
        animation_opts(frame = 100,
                       transition = 50) %>%
        animation_slider(hide=TRUE)

Linking tables

Crosstalk allows you to highlight a section of each table in the faceted chart and link the selections using Box Select or Lasso Select

#use subplot to facet out by Center, Forward and Guard
#Enable linked brushing via highlight()
subplot(cplot, fplot, gplot, titleX = TRUE, titleY = TRUE) %>%
        hide_legend() %>%
        highlight(on = "plotly_selected", persistent =TRUE)

Making Shinier Charts

#create a shared data set for the Center position for points and Ppd values
#cplot is setup for Points, need to create a Ppd plot

cplot1 <- center %>%
        plot_ly(
                x = ~Salary, y = ~Ppd, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "Points per Dollar", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers")
cplot1
bscols(cplot, cplot1)
sharedcenter <- center %>%
        SharedData$new(~Pos)
c1 <- sharedcenter %>%
        plot_ly(
                x = ~Salary, y = ~Ppd, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "Points per Dollar", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers")
c2 <- sharedcenter %>%
        plot_ly(
                x = ~Salary, y = ~Pts, hoverinfo = "text",
                text = ~paste("Name:", Name, "<br>",
                              "Points:", Pts, "<br>",
                              "Salary:", Salary, "<br>",
                              "Team:", Team)) %>%
        add_markers(
                size = ~Ppd,
                color = ~Pos,
                marker = list(opacity = 0.3,
                              sizemode = "diameter",
                              sizeref = 2)) %>%
        layout(xaxis = list(title = "DK Salary", zeroline = FALSE, showgrid=FALSE),
                yaxis = list(title = "Points per Dollar", zeroline = FALSE,  showgrid=FALSE),
               title = "Centers")
bscols(c1, c2)
bscols(filter_checkbox(id= "Pos", label = "Pos",
                       sharedData=sharedcenter, group = ~Pos), c1)
bscols(filter_select(id= "Pos", label = "Pos",
                       sharedData=sharedcenter, group = ~Pos), c1)
bscols(filter_slider(id= "Pts", label = "Pts",
                       sharedData=sharedcenter, column = ~Pts), c1)