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

Cant make use of tidy context in scale_x/y_ #5314

Closed
Yann-C-INN opened this issue May 23, 2023 · 4 comments
Closed

Cant make use of tidy context in scale_x/y_ #5314

Yann-C-INN opened this issue May 23, 2023 · 4 comments

Comments

@Yann-C-INN
Copy link

Yann-C-INN commented May 23, 2023

Hello. I want to point out three ggplot-related issues that dont correspond with the tidy-philosophy:

1 + 2: Cant make use of modified data

library(tidyverse)
# Get a vertical interecept at the maximum point of filtered data
mtcars %>% filter(cyl > 4) %>% 
  ggplot(aes(x = mpg, y = qsec)) + geom_point()+
  geom_vline(xintercept = max(.data$mpg))

# Scale y-axis breaks according to filtered data
mtcars %>% filter(cyl > 4) %>%
  ggplot(aes(x = mpg, y = qsec)) + geom_point() + 
  scale_y_continuous(breaks = seq(min(.data$qsec),max(.data$qsec), length = 5)))

This code doesn't work (error below). In both cases I think there could be a recognition, even without the .data$ that I am refering in a tidy way to my already assigned data (that is, a filtered mtcars)

Error:
! Can't subset `.data` outside of a data mask context.
Run `rlang::last_trace()` to see where the error occurred.

Why can't we simply pass the data frame (after we modified it for ggplot) as an argument and have R recognizing the variables in a tidy fashion?

3: facet_wrap could be wayyy simpler

mtcars %>% ggplot(aes(x = mpg, y = qsec)) + geom_point() +
  facet_wrap(~)

This is more of an improvement suggestion, when implementing facets, I would expect R to recognize the columns in my dataset, and offering them to me on top of the different args the function can take.
The '~' or using vars() is really redundent, the use-case of most users (all?) is very simple. to use a grouping feature to divide the plots.
image

I hope my issues are clear :)

@teunbrand
Copy link
Collaborator

You can make use of the data piped into ggplot2 if you use curly braces and insert . at the appropriate spots. This will cost you four symbols of extra typing, but you don't have to write .data, so it's a net neutral on code length in your example.

library(tidyverse)
mtcars %>% filter(cyl > 4) %>% {
  ggplot(., aes(x = mpg, y = qsec)) + geom_point() +
    geom_vline(xintercept = max(.$mpg))
}

The facet syntax is as clunky as it is due to historical choices. It seems it can't be improved without breaking a lot of people's plotting code.

@Yann-C-INN
Copy link
Author

Oh that's something I wasnt aware of. putting curly bracets outside of the entire plot. thanks

@teunbrand
Copy link
Collaborator

I realised this issue had been discussed in the past in for example #3268 and was decided against, so I'm going to close this issue for the same reason (requiring a breaking rewrite).

@teunbrand teunbrand closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2023
@Yann-C-INN
Copy link
Author

Yann-C-INN commented Jun 19, 2023

I realised this issue had been discussed in the past in for example #3268 and was decided against, so I'm going to close this issue for the same reason (requiring a breaking rewrite).

One last thing. the {} method works only with magritrr pipes (%>%) but not the base-R pipes (|>). Is it worth to ask to include base r pipes as well to work with this method? I switched to use them automatically with Ctrl + Shift + M

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

2 participants