Last active
February 1, 2017 14:35
-
-
Save abresler/9a0c1b2785dfdaf9545d to your computer and use it in GitHub Desktop.
Expoding Boxplot Function & nbastatR test use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load_packages ----------------------------------------------------------- | |
packages <- | |
c('nbastatR', #devtools::install_github("abresler/nbastatR") | |
'explodingboxplotR', #devtools::install_github("timelyportfolio/explodingboxplotR") | |
'ggplot2', | |
'dplyr', | |
'purrr', | |
'magrittr') | |
lapply(packages, library, character.only = T) | |
# write_function ---------------------------------------------------------------- | |
get_exploding_boxplot_plot <- function(data, | |
y_variable = NA, | |
x_variable = NA, | |
label_variable = NA, | |
no_iqr = 4, | |
to_categoric_x_variable = T, | |
x_groups = NA, | |
y_label = NA, | |
x_label = NA | |
) { | |
if (!'y_variable' %>% exists() | y_variable %>% is.na()) { | |
stop("Please enter a Y variable") | |
} | |
if (!'x_variable' %>% exists() | x_variable %>% is.na()) { | |
stop("Please enter an X variable") | |
} | |
plot_data <- | |
data %>% | |
mutate_(x_var = x_variable, | |
y_var = y_variable) | |
if (label_variable %>% is.na() | !'label_variable' %>% exists()) { | |
plot_data %<>% | |
mutate(id.row = 1:n(), | |
label = "id: " %>% paste0(id.row)) | |
} else { | |
plot_data %<>% | |
mutate_(label = label_variable) | |
} | |
if (!x_variable %in% names(data)) { | |
stop("Sorry your X variable isn't in the data") | |
} | |
if (!y_variable %in% names(data)) { | |
stop("Sorry your Y variable isn't in the data") | |
} | |
plot_data %<>% | |
arrange((x_var)) | |
if (plot_data$x_var %>% class %in% c('integer', 'numeric') & | |
to_categoric_x_variable == T) { | |
if (!'x_groups' %>% exists | x_groups %>% is.na) { | |
stop("Please enter group numbers") | |
} | |
plot_data %<>% | |
arrange(desc(x_var)) %>% | |
mutate( | |
x_var = x_var %>% cut_interval(x_groups), | |
x_var = x_variable %>% paste0(": ", x_var) | |
) | |
} | |
if (x_label %>% is.na() | !'x_label' %>% exists()) { | |
x_label <- | |
x_variable | |
} | |
if (y_label %>% is.na() | !'y_label' %>% exists()) { | |
y_label <- | |
y_variable | |
} | |
plot <- | |
plot_data %>% | |
exploding_boxplot( | |
y = 'y_var', | |
group = "x_var", | |
color = "x_var", | |
label = "label", | |
iqr = no_iqr, | |
margin = list( | |
bottom = 50, | |
left = 30, | |
top = 20, | |
right = 20 | |
), | |
xlab = x_label, | |
ylab = y_label | |
) | |
return(plot) | |
} | |
# test funciton ----------------------------------------------------------- | |
stats15 <- | |
get_all_player_traditional_stat_tables(year.season_start = 2015) | |
player_ids <- | |
get_nba_players_ids() | |
stats15 %<>% | |
left_join( | |
player_ids %>% | |
dplyr::select(name.player, team, image.player) | |
) | |
stats15 %<>% | |
mutate(label = name.player %>% | |
paste0("<br><img src = '", image.player,"' width = 50 height = 50>") | |
) | |
stats15 %>% | |
get_exploding_boxplot_plot(y_variable = 'pts', | |
x_variable = 'slug.team', | |
to_categoric_x_variable = T, | |
x_groups = 8, | |
x_label = "Team", | |
y_label = "Points Per Game", | |
label_variable = 'label', | |
no_iqr = 5) |
I tried the above code. Everything is running without any error. But it doesn't give any chart.
Is there any other settings I need to set for getting a JavaScript output?
Sys.info()
sysname "Windows"
release ">= 8 x64"
Version "build 9200"
machine "x86-64"
version.string R version 3.2.3 (2015-12-10)
Error in get_all_player_traditional_stat_tables(year.season_start = 2015)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Vivekmsdba, there are two comments, you need to install prior to load.