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

gf_function without xlim argument #164

Open
jiyunson opened this issue Jul 21, 2023 · 5 comments
Open

gf_function without xlim argument #164

jiyunson opened this issue Jul 21, 2023 · 5 comments

Comments

@jiyunson
Copy link

jiyunson commented Jul 21, 2023

As always, just such a big fan of ggformula and all the development work you do! I've had several experiences where I'm thinking "it would be nice if there was..." and voila -- ggformula has already thought of it. Thanks!

I've been playing more with gf_function() and there seems to be something odd about it when I'm just using vectors instead of data frames.

n <- 100
x <- rnorm(n, mean = .2, sd = .01)
y <- rnorm(n, mean = 4.2, sd = .8)

my_function <- function(x){-5.5 + 49*x}

gf_point(y ~ x) %>%
  gf_point(mean(y) ~ mean(x), color = "red") %>%
  gf_function(my_function, color = "blue", xlim = c(.15, .25))

If I leave the xlim argument out of gf_function, it won't depict the function.

gf_point(y ~ x) %>%
  gf_point(mean(y) ~ mean(x), color = "red") %>%
  gf_function(my_function, color = "blue")
image

But if I put the xlim in, it will work as expected. Is there a way to get gf_function() to work without having to set xlim?

If I use gf_function() on an x and y that is in a data frame (as in the mtcars example below), it works without xlim.

my_function <- function(x){40 + -3*x}

gf_point(mpg ~ cyl, data = mtcars) %>%
  gf_point(mean(mpg) ~ mean(cyl), color = "red") %>%
  gf_function(my_function, color = "blue")
image

Thanks for considering!

@jiyunson
Copy link
Author

Hi, as I continue to test out gf_function() there seems to be some discrepancy between the way gf_function() and geom_function() work.

This ggplot2 syntax produces the graph below:

test_function <- function(X){ 60.3605 + 0.6665*X}

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "steelblue") +
  geom_function(fun = test_function, xlim = c(10,40)) 

image

I had expected this ggformula code to produce the same thing; but it doesn't:

test_function <- function(X){ 60.3605 + 0.6665*X}
           
gf_point(mpg ~ wt, data = mtcars, color = "steelblue") %>%
  gf_function(test_function, xlim = c(10,40)) 

image

In my various tests of xlim values (within the range of the data; outside the range of the data), gf_function seems to be ignoring the xlim() argument.

@rpruim
Copy link

rpruim commented Aug 30, 2023

Sorry for the slow response. Pending some testing, I think I may have this fixed.

suppressPackageStartupMessages(library(ggformula))
test_function <- function(X){ 60.3605 + 0.6665*X}

gf_point(mpg ~ wt, data = mtcars, color = "steelblue") %>%
  gf_function(test_function, xlim = c(10,40)) 

Created on 2023-08-30 with reprex v2.0.2

@nicholasjhorton
Copy link

Randy, thanks!

@jiyunson
Copy link
Author

jiyunson commented Sep 9, 2023

Hi @rpruim! Thanks for working on this. Could you let me know when the fix will be available (e.g., via github)? I tried reinstalling ggformula fro github (i.e., devtools::install_github("ProjectMOSAIC/ggformula")) and the fix wasn't working yet...

@jiyunson
Copy link
Author

jiyunson commented Sep 9, 2023

Also is there a way to make gf_function() only appear for one facet in a faceted plot? I might do this in ggplot2:

test_function <- function(X){ 60.3605 + 0.6665*X}

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(color = "steelblue") +
  facet_wrap(~ vs) +
  geom_function(data = filter(mtcars, vs == 1), fun = test_function, xlim = c(10,40)) 
image

I would assume this would be the ggformula version:

gf_point(mpg ~ wt, data = mtcars) %>%
  gf_facet_wrap(~ vs) %>%
  gf_function(test_function, data = filter(mtcars, vs == 1)) 

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

No branches or pull requests

3 participants