sportsdataverse / sportyR

R package for drawing regulation playing surfaces for several sports
https://sportyR.sportsdataverse.org
GNU General Public License v3.0
103 stars 7 forks source link

Graph title does not appear with geom_basketball #32

Closed AndrewUr closed 4 months ago

AndrewUr commented 9 months ago

Writing in Quarto/Bookdown and the title in the labs() does not appear in the chart. Apologize if I'm just doing something stupid that I can't see and neither can my co-authors.

sportyrissue.pdf

rossdrucker commented 9 months ago

Hi @AndrewUr, can you please include example code so I can try to recreate the issue?

rtelmore commented 9 months ago

Here's a quarto doc:

---
title: "Reprex SportyR"
---

```{r}
library(sportyR)
library(ggplot2)
set.seed(1982)
df <- data.frame(locationX = runif(100, -25, 25),
                 locationY = runif(100, 40, 400))
p <- geom_basketball(league = "NBA", 
                     display_range = "defense",
                     color_updates = list(
                     defensive_half_court = "white",
                     offensive_half_court = "white",
                     court_apron = "white",
                     center_circle_fill = "white",
                     two_point_range = "white",
                     painted_area = "white",
                     free_throw_circle_fill = "white",
                     basket_ring = "black")) 
p + geom_point(data = df,
               aes(y = locationX, 
                   x = (locationY - 420)/10), 
               alpha = .25)  +
               labs(title = "Rajon Rando's Shots",
                    x = "shot one",
                    y = "shot two")
sessionInfo()
rtelmore commented 9 months ago

Here's the sessionInfo():

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Denver
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] ggplot2_3.4.3 sportyR_2.2.1

loaded via a namespace (and not attached):
 [1] gt_0.9.0          tidyr_1.3.0       utf8_1.2.4       
 [4] future_1.33.0     generics_0.1.3    renv_0.16.0      
 [7] xml2_1.3.6        lpSolve_5.6.18    stringi_1.8.3    
[10] hms_1.1.3         listenv_0.9.0     digest_0.6.33    
[13] magrittr_2.0.3    grid_4.3.1        timechange_0.2.0 
[16] fastmap_1.1.1     jsonlite_1.8.8    httr_1.4.7       
[19] rvest_1.0.3       purrr_1.0.2       fansi_1.0.6      
[22] scales_1.2.1      codetools_0.2-19  cli_3.6.2        
[25] rlang_1.1.2       parallelly_1.36.0 munsell_0.5.0    
[28] withr_2.5.2       remotes_2.4.2.1   cachem_1.0.8     
[31] tools_4.3.1       parallel_4.3.1    tzdb_0.4.0       
[34] memoise_2.0.1     dplyr_1.1.4       colorspace_2.1-0 
[37] nbastatR_0.1.152  globals_0.16.2    curl_5.2.0       
[40] vctrs_0.6.5       R6_2.5.1          lifecycle_1.0.4  
[43] lubridate_1.9.3   snakecase_0.11.1  stringr_1.5.1    
[46] furrr_0.3.1       janitor_2.2.0     pkgconfig_2.0.3  
[49] gtable_0.3.3      pillar_1.9.0      glue_1.6.2       
[52] xfun_0.39         tibble_3.2.1      tidyselect_1.2.0 
[55] rstudioapi_0.15.0 knitr_1.40        htmltools_0.5.6  
[58] readr_2.1.4       compiler_4.3.1 
rtelmore commented 9 months ago

The package is awesome @rossdrucker !!

rossdrucker commented 9 months ago

Thanks for calling this out @AndrewUr and @rtelmore, and hope you guys had a happy holiday!

I've looked into it, and I think I've figured out what's going on. It has to do with the underlying mechanics of creating the plot and the way that I've got certain theme elements set. Specifically, I have the axis labels set to be blanks (ggplot2::element_blank()) since the intention of the package was originally to use the spatial information from the surface (in this case, the basketball court) to display where certain events occurred. I figured that this would likely be a common enough user action where it made sense to always hide the axes labels, which is why the defaults are set this way. I suppose it doesn't matter, but I am curious as to why the x and y axis labels are needed in your case?

The missing plot title, on the other hand, is a result of the margins I've got set. These were initially set to reduce some of the additional white-space around the plot. I'm looking at updating these and will likely release a patch here, but before doing so I want to make sure that the change is appropriate and doesn't cause any issues with any other sports.

To resolve your problem with the example code above, the following should do the trick:

library(sportyR)
library(ggplot2)
set.seed(1982)

df <- data.frame(
  locationX = runif(100, -25, 25),
  locationY = runif(100, 40, 400)
)

p <- geom_basketball(
  league = "NBA", 
  display_range = "defense",
  color_updates = list(
  defensive_half_court = "white",
  offensive_half_court = "white",
  court_apron = "white",
  center_circle_fill = "white",
  two_point_range = "white",
  painted_area = "white",
  free_throw_circle_fill = "white",
  basket_ring = "black")
)  + 
  ggplot2::theme(
    axis.title = ggplot2::element_text(),
    plot.title = ggplot2::element_text(),
    plot.margin = ggplot2::margin(
      t = 0,
      b = 0
    )
)

p + geom_point(
  data = df,
  aes(
    y = locationX,
    x = (locationY - 420)/10), 
    alpha = .25
)  +
  labs(
    title = "Rajon Rando's Shots",
    x = "shot one",
    y = "shot two"
)

image

AndrewUr commented 9 months ago

Thanks @rossdrucker!

You're right that we don't need the x and y axes, but we do need the title. We just put the x and y axis labels in the reprex for completeness sake and perhaps to aid you in debugging.