Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geom_baseball doesn't connect well with other data sets #6

Open
billyfryer opened this issue Jun 22, 2022 · 3 comments
Open

geom_baseball doesn't connect well with other data sets #6

billyfryer opened this issue Jun 22, 2022 · 3 comments
Labels
feature request New feature or request

Comments

@billyfryer
Copy link

Hey Ross, here is some sample code of the issue I am running into. It isn't a big deal with the way this data is set up, but when I have many geom_point() functions, it starts to become a little tedious. Ideally, I'd like to do the plot on top rather than the top on bottom. The difference being that I can have a ggplot statement that takes care of the data rather than putting data = in the geom_point(). The error I get says "Error in ggplot_add():! Can't add sportyR::geom_baseball(league = "MLB") to a ggplot object. Run rlang::last_error() to see where the error occurred."

# Sample data
x <- c(96.49, 108.33, 98.68, 65.81, 111.83,
       109.20, 105.26, 70.63, 129.80, 63.18)
y <- c(161.63, 148.44, 158.33, 177.18, 160.22,
       160.69, 163.51, 156.92, 154.09, 170.11)

data <- data.frame(x,y)

# What I'd like to do, but doesn't work
ggplot(data = data) +
  geom_point(aes(x = x,
                            y = y),
             stroke = 1,
             size = 5,
color = "blue") +
  labs(title = "Sample Data",
       color = "Result") +
theme(plot.title = element_text(hjust = 0.5)) +
  sportyR::geom_baseball(league = "MLB")


# What I have to do instead
sportyR::geom_baseball(league = "MLB") +
  geom_point(data = data,
         aes(x = x,
             y = y),
         color = "blue",
         stroke = 1,
         size = 5) +
  labs(title = "Sample Data",
       color = "Result") +
theme(plot.title = element_text(hjust = 0.5))
@rossdrucker
Copy link
Collaborator

Admittedly, I'm not sure I fully understand what the behavior you're expecting from the first example would be... wouldn't plotting the field over the points then effectively hide the points?

The error you're getting is actually coming from ggplot2 directly, and is because it's antithetical to the way that ggplot2 is designed. What ggplot() is doing is establishing the default set of aesthetics and data to use when plotting, essentially creating the basis for the plot. In your first example, it's what allows the points to be added (before calling sportyR::geom_baseball("MLB"). Under the hood, however, geom_baseball("MLB") is also calling ggplot() to establish a common set of aesthetics for visualizing spatiotemporal baseball data.

But, back to the issue at hand: what's the expected functionality that you're looking for in the first option? Is it to merely pass the data as an argument? If so, I can investigate how to best implement this, but I do think there's some value in specifying the data and manual aesthetics the way you showed in your second example (and how it's demonstrated in the README). Thoughts?

@billyfryer
Copy link
Author

I totally meant passing the data as an argument to geom_baseball(). Serves me right for trying to code too early in the morning (for me). What I meant to say was:

# Sample data
x <- c(96.49, 108.33, 98.68, 65.81, 111.83,
       109.20, 105.26, 70.63, 129.80, 63.18)
y <- c(161.63, 148.44, 158.33, 177.18, 160.22,
       160.69, 163.51, 156.92, 154.09, 170.11)

data <- data.frame(x,y)

# What I'd like to do, but doesn't work
sportyR::geom_baseball(league = "MLB") +
  ggplot(data = data) +
  geom_point(aes(x = x,
                 y = y),
             color = "blue",
             stroke = 1,
             size = 5) +
  labs(title = "Sample Data",
       color = "Result") +
  theme(plot.title = element_text(hjust = 0.5))

Or passing the data argument into geom_baseball() since like you said geom_baseball("MLB") calls the ggplot() function under the hood

@rossdrucker
Copy link
Collaborator

Ah okay, I see what you mean now. This should then be something for all geom_{sport}() functions in the package then. I'll see if I can implement a way to directly pass the data in from the geom_{sport}() call in a future version of the package. Temporarily tabling this but leaving it open for future development.

@armstjc armstjc added the feature request New feature or request label Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants