Skip to content

Implement geom_function(). #3982

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

Merged
merged 6 commits into from
May 2, 2020

Conversation

clauswilke
Copy link
Member

A first stab at implementing a geom_function(). Closes #3611.

Open questions:

  • How much should we rewrite the documentation to stat_function()? Can it remain mostly as is, or should it now recommend geom_function() over stat_function()?
  • Is the current warning for multiple groups OK, or should the code filter out the duplicated groups?
library(tidyverse)

data <- tibble(
  x = 1:50,
  y = x*x - 2*x + 5 + 200*rnorm(50),
  a = sample(letters[1:3], 50, replace = TRUE)
)

ggplot(data, aes(x, y)) +
  geom_point(aes(color = "data")) +
  geom_function(aes(color = "function"), fun = ~.x*.x - 2*.x + 5)

ggplot(data, aes(x, y, color = a)) +
  geom_point(aes()) +
  geom_function(aes(color = "function"), fun = ~.x*.x - 2*.x + 5)

ggplot(data, aes(x, y, color = a)) +
  geom_point(aes()) +
  geom_function(aes(), fun = ~.x*.x - 2*.x + 5)
#> Warning: Multiple drawing groups in `geom_function()`. Did you use the correct
#> `group`, `colour`, or `fill` aesthetics?

ggplot(data, aes(x, y)) +
  geom_point(aes(color = "data")) +
  stat_function(aes(color = "function"), fun = ~.x*.x - 2*.x + 5)

ggplot(data, aes(x, y, color = a)) +
  geom_point(aes()) +
  stat_function(aes(color = "function"), fun = ~.x*.x - 2*.x + 5)

ggplot(data, aes(x, y, color = a)) +
  geom_point(aes()) +
  stat_function(aes(), fun = ~.x*.x - 2*.x + 5)
#> Warning: Multiple drawing groups in `geom_function()`. Did you use the correct
#> `group`, `colour`, or `fill` aesthetics?

Created on 2020-05-01 by the reprex package (v0.3.0)

Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good! It might be worth trying the PR on faceted plots just to make sure, but I can't see any red flags.

@clauswilke
Copy link
Member Author

With facets. Works as expected.

library(tidyverse)

data <- tibble(
  x = 1:50,
  y = x*x - 2*x + 5 + 200*rnorm(50),
  a = sample(letters[1:3], 50, replace = TRUE)
)

ggplot(data, aes(x, y, color = a)) +
  geom_point() +
  geom_function(color = "black", fun = ~.x*.x - 2*.x + 5) +
  facet_wrap(~a)

ggplot(data, aes(x, y, color = a)) +
  geom_point() +
  geom_function(fun = ~.x*.x - 2*.x + 5) +
  facet_wrap(~a)

Created on 2020-05-01 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member Author

@hadley I'd prefer to rewrite the documentation so it refers primarily to geom_function() rather than stat_function(), since this would match geom_density(), geom_contour(), geom_smooth(), etc. Any reason not to do this? The name of the .Rd file would change, of course.

@hadley
Copy link
Member

hadley commented May 1, 2020

That make sense to me. I don't think changing the name of the .Rd file should cause any problems.

@clauswilke clauswilke changed the title WIP: Implement geom_function(). Implement geom_function(). May 2, 2020
clauswilke added 2 commits May 1, 2020 23:11
Merge branch 'master' into issue-3611-geom_function

# Conflicts:
#	NEWS.md
@clauswilke clauswilke merged commit 49aca36 into tidyverse:master May 2, 2020
@clauswilke clauswilke deleted the issue-3611-geom_function branch May 2, 2020 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stat_function() produces incorrect warning
3 participants