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

Usage of different scales #40

Closed
bnorthoff opened this issue Nov 5, 2022 · 5 comments
Closed

Usage of different scales #40

bnorthoff opened this issue Nov 5, 2022 · 5 comments
Labels
bug Something isn't working ggplot2 issue Issue relating to ggplot2's framework and not ggside.
Milestone

Comments

@bnorthoff
Copy link

I would like to use a log10 scale for the main plot and a continuous scale for the additonal ggside facet. Although I am able to define these scales by scale_y_log10and scale_xsidey_continuous the values gets transformed:

df <- data.frame(
  x = seq(from = 20, to = 60, by = 1),
  y1 = seq(from = 0.001, to = 1, length.out = 41),
  y2 = seq(from = 5000, to = 1, length.out = 41)
)

ggplot(data = df) +
  geom_line(mapping = aes(x = x, y = y1, group = "obs1")) +
  geom_xsideline(mapping = aes(x = x, y = y2)) +
  scale_y_log10(expand = c(0, 0), limits = c(0.001,1)) +
  scale_xsidey_continuous(expand = c(0, 0))

Is there a solution?

@jtlandis jtlandis added the bug Something isn't working label Nov 10, 2022
@jtlandis
Copy link
Owner

This definitely looks like a bug - I will have a look at what is going on this weekend. I personally do not know where in the ggplot2 pipeline the transformation is being applied.

@jtlandis jtlandis added the ggplot2 issue Issue relating to ggplot2's framework and not ggside. label Nov 12, 2022
@jtlandis
Copy link
Owner

I have taken an extensive look into the issue and this seems that this may not be easily added feature due to the internals of the ggplot2:::ggplot_build.ggplot() function. This assumes that all layers will have the same positional scales, thus the data of any geom_*side* layer will also be transformed according the any scales transform for the default plot.

Unfortunately, I do not think I could implement this feature without doing something either really hacky, or by writing my own ggplot_build.ggside() method. Both are undesirable for different reasons. The first is likely to break because ggplot2's internals may change at any time. The second is also not ideal for very similar reasons to the first. Writing a method that will stand the test of time is extremely difficult.

I may consider doing the hacky solution since a lot of the package has clever ways to bypass the ggproto's factory methods. But I may need to put some serious thought into the long term longevity of ggside and invest in writing a proper ggplot_build.ggside method.

@jtlandis
Copy link
Owner

Against my better judgement I have implemented the hacky solution. Hopefully it doesn't break too soon. For now it will reside in the branch dev-scales. Please check out this branch and test it.

here is a reprex of a very minimal case. I have yet to test with other facets_*() functions

library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#>   method from   
#>   +.gg   ggplot2

df <- data.frame(
  x = seq(from = 20, to = 60, by = 1),
  y1 = seq(from = 0.001, to = 1, length.out = 41),
  y2 = seq(from = 5000, to = 1, length.out = 41)
)

ggplot(data = df) +
  geom_line(mapping = aes(x = x, y = y1, group = "obs1")) +
  geom_xsideline(mapping = aes(x = x, y = y2)) +
  scale_xsidey_continuous(expand = c(0, 0), trans = scales::log10_trans()) 

ggplot(data = df) +
  geom_line(mapping = aes(x = x, y = y1, group = "obs1")) +
  geom_xsideline(mapping = aes(x = x, y = y2)) +
  scale_y_log10() +
  scale_xsidey_continuous(expand = c(0, 0))

Created on 2022-11-12 with reprex v2.0.2

I will consider adding this as a feature if it seems safe with other facets. Please let me know if you find anything.

jtlandis added a commit that referenced this issue Nov 13, 2022
@jtlandis
Copy link
Owner

jtlandis commented Dec 4, 2022

I have recently decided that I will move forward with a submission to ggside that excludes these developments. While this has been fully implemented on the dev branch, it is not in a place where I believe it is stable enough for CRAN. Hopefully I will be able to put in enough work where I can make another submission in a little over a month.

@jtlandis jtlandis added this to the ggside 0.3.0 milestone Dec 11, 2023
@jtlandis
Copy link
Owner

with the release of 0.3.0 support for this feature has been introduced.

library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#>   method from   
#>   +.gg   ggplot2

df <- data.frame(
  x = seq(from = 20, to = 60, by = 1),
  y1 = seq(from = 0.001, to = 1, length.out = 41),
  y2 = seq(from = 5000, to = 1, length.out = 41)
)

adding a global scale transform does not affect ggside scales unless they share a scale with the main plot.

ggplot(data = df) +
  geom_line(mapping = aes(x = x, y = y1, group = "obs1")) +
  geom_xsideline(mapping = aes(x = x, y = y2)) +
  scale_y_log10() 

You can still transform the side scale with a new set of functions as below.

ggplot(data = df) +
  geom_line(mapping = aes(x = x, y = y1, group = "obs1")) +
  geom_xsideline(mapping = aes(x = x, y = y2)) +
  scale_xsidey_log10(expand = c(0, 0))

It wasn't my intention originally to make native ggplot2 scale transforms not affect adjacent side scales, but I think it is a reasonable assumption as the package moved towards supporting a mixing of continuous and discrete axis combinations. I.e. data scales are inferred from the data mappings to either be continuous or discrete, and anything beyond that you must opt into explicitly.

Created on 2024-02-25 with reprex v2.1.0

Thank you for your patients with this issue, I believe it is time for it to be closed. Thanks @bnorthoff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ggplot2 issue Issue relating to ggplot2's framework and not ggside.
Projects
None yet
Development

No branches or pull requests

2 participants