teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

help_secondary facet free scales issue #88

Closed joekeane7 closed 1 year ago

joekeane7 commented 1 year ago

Hi Teun,

I absolutely love the ggh4x package, it's helped me a few times with data visualisation in ggplot, particularly when it comes to facets and dual Y scales.

I've recent;y used your help_secondary function and it is great in that it is relatively simple to apply and it has added flexibility around ylimits and methodology of scale.

However I have come up against an old problem I used to have when I was using my old method of applying a transformation within the ggplot, in that the Primary Y scale does not respect the free_y in the facet_manual I am using. I want it too so you can see clearly variables on each facet not just based on the single scale.

I don't know if this is an issue or something I am doing wrong, but would really appreciate if you can help me out!

Couldn't post code here unfortunately but I can provide if there is a way to share.

I have tried to post some images below as well of the output from both methods

I would much rather use the help_secondary method if I can get the primary axis to behave as in the second image below.

ggh4x sec_axis transmeth

teunbrand commented 1 year ago

Hi there,

Glad you find it useful! I didn't really make help_secondary() with facets in mind, but I'm relatively sure that it just provides a 1:1 mapping of the primary range to the secondary and doesn't know about any panel structure. Thus, if you give it the total range, the helper will just transform to the total range. I don't know how the regular sec_axis() would deal with that, so if you can scramble together an example using built-in datasets, that would be helpful.

joekeane7 commented 1 year ago

Hi,

So I've attached the raw data here and also pasted the two different scripts I have used for the above, you can see the difference in that the primary Y scale doesn't act as free using the help_secondary(). I've since noticed it behaves similarly for the Seconday Y axis in that it doesn't apply the ylim to all facets (see output for Set6), and just gives the one transformation, presumably from the initial transform of the total range.

It would be great if the output could respect facet grouping in SecAxis_Transformationwithin_help_secondary.txt SecAxis_TransformationwithinGGPLOT.txt some way during the initial help_secondary() formula so it plots with full free behaviour for both Y scales as is the case when not using a facet data set.

secaxisdata1.xlsx

I've attached the two scripts as notepad files, and the data as an excel. Hopefully you should be able to clearly see the difference. I've commented out the two methods alternatively in each script so you can see where I have applied the two methods...hopefulyl!

Appreciate your help and apologies for the slightly messy coding

Joe.

teunbrand commented 1 year ago

Thanks! Yeah that sort of confirms what I was thinking. The sec_axis() method has some freedom to deal with panel-specific limits whereas help_secondary() does not. It's not that it couldn't in theory deal with panel specific limits, it's more that it can't simultaneously provide a forward transformation for the secondary data. I don't currently see a way that it could know about the panel structure, as that is only computed after the data has been taken in and processed.

joekeane7 commented 1 year ago

Thanks for your quick response.

Would you by any chance know of a way I could tweak my sec_axis method so it had variable limits per panel?

No problem if not. appreciate your help.

Joe.

teunbrand commented 1 year ago

This is all still a bit manual, but you could use facetted scales to set the sec_axis individually per panel. I'd not envy the task though.

joekeane7 commented 1 year ago

I gave it a go but I don't think I can get it to work. Thanks again.