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

geom_image does not work when using facet_grid as well #34

Closed philly1993 closed 4 months ago

philly1993 commented 8 months ago

I am trying to add a logo to a rink using ggimage::geom_image and a facet_grid but I am getting an error. I am able to add the logo without adding the facet_grid and use the facet_grid without the logo, but not both. Any advice will be appreciated. Thanks!

Example:

This errors out

geom_hockey(league = "NHL", rotation = 90, display_range = "ozone") +
+     geom_point(data = tmp, aes(x = x, y = y), color = "navy", alpha = 0.5, size = 1) +
+     geom_image(aes(x = 0, y = 95, image = here::here("wsh.png")), size = 0.1) +
+     facet_grid(~manpowerSituation~shotResult)

Error in FUN(X[[i]], ...) : subscript out of bounds

These two work

geom_hockey(league = "NHL", rotation = 90, display_range = "ozone") +
+     geom_point(data = tmp, aes(x = x, y = y), color = "navy", alpha = 0.5, size = 3) +
+     geom_image(aes(x = 0, y = 95, image = here::here("wsh.png")), size = 0.1)

image

geom_hockey(league = "NHL", rotation = 90, display_range = "ozone") +
+     geom_point(data = tmp, aes(x = x, y = y), color = "navy", alpha = 0.5, size = 1) +
+     facet_grid(~manpowerSituation~shotResult)

image

rossdrucker commented 4 months ago

Hi, sorry for the delay in responding here. Working on this a bit... can you provide me with what tmp contains so I can try to recreate what it is you're doing?

rossdrucker commented 4 months ago

Actually, was able to simulate your data a bit. The code below works, although you may need to adjust the positional parameters for annotation_custom()

library(sportyR)
library(ggplot2)
library(grid)
library(png)

set.seed(2379)
tmp <- data.frame(
  manpowerSituation = sample(
    c("EV", "PP"),
    size = 100,
    replace = TRUE,
    prob = c(.5, .5)
  ),
  x = runif(100, -22, 22),
  y = runif(100, 65, 88),
  shotResult = sample(
    c("goal", "onnet", "missed"),
    size = 100,
    replace = TRUE,
    prob = c(1/3, 1/3, 1/3)
  )
)

# Note that this will need to be changed based on your file name/location
wsh <- grid::rasterGrob(png::readPNG("wsh.png"))

geom_hockey(league = "NHL", rotation = 90, display_range = "ozone") +
  geom_point(data = tmp, aes(x = x, y = y), color = "navy", alpha = 0.5, size = 1) +
  annotation_custom(wsh, xmin = -11, xmax = 11, ymin = 92, ymax = 100) + 
  facet_grid(~manpowerSituation~shotResult) + 
  NULL

image

rossdrucker commented 4 months ago

Also noting here you can use geom_image() if you add the image to the data:

library(sportyR)
library(ggplot2)
library(grid)
library(png)

set.seed(2379)
tmp <- data.frame(
  manpowerSituation = sample(
    c("EV", "PP"),
    size = 100,
    replace = TRUE,
    prob = c(.5, .5)
  ),
  x = runif(100, -22, 22),
  y = runif(100, 65, 88),
  shotResult = sample(
    c("goal", "onnet", "missed"),
    size = 100,
    replace = TRUE,
    prob = c(1/3, 1/3, 1/3)
  )
)

tmp$image <- here::here("wsh.png")

geom_hockey(league = "NHL", rotation = 90, display_range = "ozone") +
  geom_point(data = tmp, aes(x = x, y = y), color = "navy", alpha = 0.5, size = 1) +
  geom_image(data = tmp, aes(x = 0, y = 96, image = image), size = 0.1, by = "width") + 
  facet_grid(~manpowerSituation~shotResult)

image

rossdrucker commented 4 months ago

@philly1993 just confirming that this resolves the problem? If so (or after ~a week of inactivity here), I'm going to close this issue

rossdrucker commented 4 months ago

Closing due to inactivity; feel free to open a new issue if more support needed