daniel1noble / orchaRd

Extending the Orchard Plot for Meta-analysis
https://daniel1noble.github.io/orchaRd/
11 stars 6 forks source link

Option to flip orientation of orchard plot to vertical #28

Closed befriendabacterium closed 1 year ago

befriendabacterium commented 1 year ago

Is your feature request related to a problem? Please describe. I want to make a vertically-oriented orchard plot

Describe the solution you'd like Option in the orchard_plot() function to flip the orientation of the plot vertically (i.e. effects on Y, studies or other groupings on X axis), keeping font in standard readable orientation.

Describe alternatives you've considered Could manually flip the whole plot outside of R, but not codable.

Additional context It's often useful to orient effect size plots this way to align better with meta-regression bubble plots, where effects are typically on Y and an explanatory moderator is on the X. Useful to keep things in consistent orientation for the reader.

daniel1noble commented 1 year ago

Hi. Good one. Can try and implement this, but have you tried just coord_flip()? It's a ggplot function. You should be able to just add that on top of orchard_plot. Let me know if that does what you want

daniel1noble commented 1 year ago

Hi @befriendabacterium. Sorry. I realised that coord_flip won't work at reverting back to a normal plot. This is a very easy fix though. I've quickly added a new argument called flip. It defaults to TRUE (making normal orchard plots) but if you set it to FALSE then it will re-orientate.

This is currently on the dev branch. And I've added an example in the vignette there to show you what I mean. Regardless, the relevant code below:

We may also at times want to revert back to a normal plot with catagorical variables on the x-axis. We can do this by setting flip argument to FALSE.

#| label: fig-Figure1212
#| fig-cap: "Caterpillars plot of the the strength of correlation between maternal and offspring size within-species"

  #Plot the meta-regression model
  lim_MR_results <- orchaRd::mod_results(lim_MR, mod = "Phylum", group = "Article", 
                                           at = list(Phylum = c("Chordata", "Arthropoda", "Mollusca")),
                                           subset = TRUE)
  orchaRd::orchard_plot(lim_MR_results, xlab = "Correlation coefficient", transfm = "tanh", g = TRUE, flip = FALSE)

This all uses the lim example in the vignette at the end of section 4.3. You will need to install the development version of orchaRd for now until I can do some more testing, but this shouldn't cause major problems. To install the development version:

rm(list = ls()) devtools::install_github("daniel1noble/orchaRd", ref = "dev", force = TRUE) pacman::p_load(devtools, tidyverse, metafor, patchwork, R.rsp, orchaRd, emmeans, ape, phytools, flextable)

Hope that helps! Dan

befriendabacterium commented 1 year ago

Hey Dan,

Thanks for this - I'm just getting stuck into orchaRd proper today and it's great so thanks (sorry I didn't say in my original Feature Request - was in a bit of a rush!).

Thanks so much for implementing this new flip option so fast - i've just installed the dev version and am giving it a go now! R seems to be thinking about it very hard so i'm gonna grab a mid afternoon coffee and will let you know if it's figured it out by the time I come back.

Cheers Matt

befriendabacterium commented 1 year ago

Hm, weird, even uninstalling the main version and then installing the dev version with your code, it doesn't find recognise the 'flip' argument, giving:

Error in orchaRd::orchard_plot(res, mod = "study_studyID", group = "study_studyID", : unused argument (flip = TRUE)

befriendabacterium commented 1 year ago

Reproducible example here:


remove.packages("orchaRd")
devtools::install_github("daniel1noble/orchaRd", ref = "dev", force = TRUE)
pacman::p_load(devtools, tidyverse, metafor, patchwork, R.rsp, orchaRd, emmeans,
               ape, phytools, flextable)

#load english data
data(english)

# We need to calculate the effect sizes, in this case d
english <- escalc(measure = "SMD", n1i = NStartControl, sd1i = SD_C, m1i = MeanC,
                  n2i = NStartExpt, sd2i = SD_E, m2i = MeanE, var.names = c("SMD", "vSMD"), data = english)

english_MA <- rma.mv(yi = SMD, V = vSMD, random = list(~1 | StudyNo, ~1 | EffectID),
                     data = english)

orchaRd::orchard_plot(english_MA, xlab = "Correlation coefficient", transfm = "tanh", g = TRUE, flip = FALSE)
daniel1noble commented 1 year ago

That is odd Matt. It has to be something with a package loading issue, but I’m surprised removing it doesn’t work. Have you tried quitting R and doing a fresh load? DanOn 8 Jun 2023, at 11:33 pm, Matt Lloyd Jones @.***> wrote: Hm, weird, even uninstalling the main version and then installing the dev version with your code, it doesn't find recognise the 'flip' argument, giving: Error in orchaRd::orchard_plot(res, mod = "study_studyID", group = "study_studyID", : unused argument (flip = TRUE)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you modified the open/close state.Message ID: @.***>

daniel1noble commented 1 year ago

Matt. I've checked some more. I think this is good so I merged dev with main. Should work for you now in case it's still installing from main branch

befriendabacterium commented 1 year ago

Hey thanks this works now - I think it was refusing to install the new version because I had orchaRd loaded.

Just a heads up that the rotation confuses the 'angle' argument because it now changes the angle of the effect size axis tick marks (which are now on the y axis), rather than the grouping labels. As a workaround you can override it by using +theme() lines to manually specify the axes, but longer term it might be worth fixing this in the orchaRd function itself. For example, could have two arguments 'angle.group' and 'angle.effect' to specify them specifically in a way that is robust to flipping the orientation of the plot? Sorry don't mean to add to your plate just flagging it up for future!

befriendabacterium commented 1 year ago

Sorry another one - the 'k = ' labels also don't align properly when flip=FALSE. They should align in the center of the line where the effect size falls, I think. See image. Sorry can't figure out how to fix myself, have tried! image

daniel1noble commented 1 year ago

Thanks for catching this Matt. I'm off to India for a week for work, but I'll fix this all up when I return, should be fairly easy to fix

befriendabacterium commented 1 year ago

No worries no rush at all just flagging it for future whilst i noticed it! Enjoy your trip and thanks for all your hard work!