ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
38 stars 11 forks source link

gf_function without xlim argument #164

Open jiyunson opened 1 year ago

jiyunson commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Randy, thanks!

jiyunson commented 1 year ago

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 commented 1 year ago

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))