Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Oct 27, 2022
0 parents commit 4bc6a3c
Show file tree
Hide file tree
Showing 48 changed files with 517 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^nbaplotR\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^README\.Rmd$
^cran-comments\.md$
^data-raw$
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.DS_Store
21 changes: 21 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Package: nbaplotR
Title: NBA Logo Plots in 'ggplot2'
Version: 0.0.1
Authors@R:
person("Sebastian", "Carl", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: A set of functions to visualize National Basketball League
analysis in 'ggplot2'.
License: MIT + file LICENSE
Depends:
R (>= 3.6)
Imports:
ggpath (>= 1.0.0),
ggplot2 (>= 3.3.0),
grid
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2022
COPYRIGHT HOLDER: Sebastian Carl
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2022 Sebastian Carl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(GeomNBAlogo)
export(geom_nba_logos)
export(valid_team_names)
129 changes: 129 additions & 0 deletions R/geom_nba_logos.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#' ggplot2 Layer for Visualizing NBA Team Logos
#'
#' @description This geom is used to plot NBA team logos instead
#' of points in a ggplot. It requires x, y aesthetics as well as a valid NBA
#' team abbreviation. The latter can be checked with [`valid_team_names()`].
#'
#' @inheritParams ggplot2::geom_point
#' @section Aesthetics:
#' `geom_nba_logos()` understands the following aesthetics (required aesthetics are in bold):
#' \itemize{
#' \item{**x**}{ - The x-coordinate.}
#' \item{**y**}{ - The y-coordinate.}
#' \item{**team_abbr**}{ - The team abbreviation. Should be one of [`valid_team_names()`]. The function tries to clean team names internally by calling [`nflreadr::clean_team_abbrs()`]}
#' \item{`alpha = NULL`}{ - The alpha channel, i.e. transparency level, as a numerical value between 0 and 1.}
#' \item{`colour = NULL`}{ - The image will be colorized with this colour. Use the special character `"b/w"` to set it to black and white. For more information on valid colour names in ggplot2 see <https://ggplot2.tidyverse.org/articles/ggplot2-specs.html?q=colour#colour-and-fill>}
#' \item{`angle = 0`}{ - The angle of the image as a numerical value between 0° and 360°.}
#' \item{`hjust = 0.5`}{ - The horizontal adjustment relative to the given x coordinate. Must be a numerical value between 0 and 1.}
#' \item{`vjust = 0.5`}{ - The vertical adjustment relative to the given y coordinate. Must be a numerical value between 0 and 1.}
#' \item{`width = 1.0`}{ - The desired width of the image in `npc` (Normalised Parent Coordinates).
#' The default value is set to 1.0 which is *big* but it is necessary
#' because all used values are computed relative to the default.
#' A typical size is `width = 0.075` (see below examples).}
#' \item{`height = 1.0`}{ - The desired height of the image in `npc` (Normalised Parent Coordinates).
#' The default value is set to 1.0 which is *big* but it is necessary
#' because all used values are computed relative to the default.
#' A typical size is `height = 0.1` (see below examples).}
#' }
#' @param ... Other arguments passed on to [ggplot2::layer()]. These are
#' often aesthetics, used to set an aesthetic to a fixed value. See the below
#' section "Aesthetics" for a full list of possible arguments.
#' @return A ggplot2 layer ([ggplot2::layer()]) that can be added to a plot
#' created with [ggplot2::ggplot()].
#' @export
#' @examples
#' \donttest{
#' library(nbaplotR)
#' library(ggplot2)
#'
#' team_abbr <- valid_team_names()
#'
#' df <- data.frame(
#' a = rep(1:8, 4),
#' b = sort(rep(1:4, 8), decreasing = TRUE),
#' teams = team_abbr
#' )
#'
#' # keep alpha == 1 for all teams including an "A"
#' matches <- grepl("A", team_abbr)
#' df$alpha <- ifelse(matches, 1, 0.2)
#' # also set a custom fill colour for the non "A" teams
#' df$colour <- ifelse(matches, NA, "gray")
#'
#' # scatterplot of all logos
#' ggplot(df, aes(x = a, y = b)) +
#' geom_nfl_logos(aes(team_abbr = teams), width = 0.075) +
#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
#' theme_void()
#'
#' # apply alpha via an aesthetic from inside the dataset `df`
#' # please note that you have to add scale_alpha_identity() to use the alpha
#' # values in your dataset!
#' ggplot(df, aes(x = a, y = b)) +
#' geom_nba_logos(aes(team_abbr = teams, alpha = alpha), width = 0.075) +
#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
#' scale_alpha_identity() +
#' theme_void()
#'
#' # apply alpha and colour via an aesthetic from inside the dataset `df`
#' # please note that you have to add scale_alpha_identity() as well as
#' # scale_color_identity() to use the alpha and colour values in your dataset!
#' ggplot(df, aes(x = a, y = b)) +
#' geom_nba_logos(aes(team_abbr = teams, alpha = alpha, colour = colour), width = 0.075) +
#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
#' scale_alpha_identity() +
#' scale_color_identity() +
#' theme_void()
#'
#' # apply alpha as constant for all logos
#' ggplot(df, aes(x = a, y = b)) +
#' geom_nba_logos(aes(team_abbr = teams), width = 0.075, alpha = 0.6) +
#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
#' theme_void()
#'
#' }
geom_nba_logos <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {

ggplot2::layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomNBAlogo,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}

#' @rdname nbaplotR-package
#' @export
GeomNBAlogo <- ggplot2::ggproto(
"GeomNBAlogo", ggplot2::Geom,
required_aes = c("x", "y", "team_abbr"),
# non_missing_aes = c(""),
default_aes = ggplot2::aes(
alpha = NULL, colour = NULL, angle = 0, hjust = 0.5,
vjust = 0.5, width = 1.0, height = 1.0
),
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {

data$path <- system.file(paste0(data$team_abbr, ".png"), package = "nbaplotR")

ggpath::GeomFromPath$draw_panel(
data = data,
panel_params = panel_params,
coord = coord,
na.rm = na.rm
)
},
draw_key = function(...) grid::nullGrob()
)
6 changes: 6 additions & 0 deletions R/nbaplotR-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL
Binary file added R/sysdata.rda
Binary file not shown.
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' Output Valid NAB Team Abbreviations
#'
#' @description The abbreviations used in this function are extracted from ESPN
#'
#' @export
#' @return A vector of type `"character"`.
#' @examples
#' # List valid team abbreviations
#' valid_team_names()
valid_team_names <- function(){
n <- sort(names(logo_list))
n
}
56 changes: 56 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# nbaplotR

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/nbaplotR)](https://CRAN.R-project.org/package=nbaplotR)
<!-- badges: end -->

The goal of nbaplotR is to ...

## Installation

You can install the development version of nbaplotR like so:

``` r
# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE?
```

## Example

This is a basic example which shows you how to solve a common problem:

```{r example}
library(nbaplotR)
## basic example code
```

What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so:

```{r cars}
summary(cars)
```

You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date. `devtools::build_readme()` is handy for this. You could also use GitHub Actions to re-render `README.Rmd` every time you push. An example workflow can be found here: <https://github.com/r-lib/actions/tree/v1/examples>.

You can also embed plots, for example:

```{r pressure, echo = FALSE}
plot(pressure)
```

In that case, don't forget to commit and push the resulting figure files, so they display on GitHub and CRAN.
5 changes: 5 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## R CMD check results

0 errors | 0 warnings | 1 note

* This is a new release.
18 changes: 18 additions & 0 deletions data-raw/build_internal_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## code to prepare `build_internal_data` dataset goes here
teams_colors_logos <- hoopR::espn_nba_teams()

purrr::walk(teams_colors_logos$abbreviation, function(abbr){
url <- teams_colors_logos$logo[teams_colors_logos$abbreviation == abbr]
download.file(url, file.path("inst", paste0(abbr, ".png")))
})

primary_colors <- teams_colors_logos$color |>
rlang::set_names(teams_colors_logos$abbreviation)

secondary_colors <- teams_colors_logos$alternate_color |>
rlang::set_names(teams_colors_logos$abbreviation)

usethis::use_data(
primary_colors, secondary_colors,
internal = TRUE, overwrite = TRUE
)
Binary file added inst/ATL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/BKN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/BOS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/CHA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/CHI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/CLE.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/DAL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/DEN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/DET.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/GS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/HOU.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/IND.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/LAC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/LAL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/MEM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/MIA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/MIL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/MIN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/NO.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/NY.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/OKC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/ORL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/PHI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/PHX.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/POR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/SA.png
Binary file added inst/SAC.png
Binary file added inst/TOR.png
Binary file added inst/UTAH.png
Binary file added inst/WSH.png
Loading

0 comments on commit 4bc6a3c

Please sign in to comment.