-
-
Save rasyidstat/9bf320ac2bcf2c65d8bdca2bf53b4f0a 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment