teunbrand / ggh4x

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

facet_grid2 set the every column have same y limit range #98

Closed junjunlab closed 1 year ago

junjunlab commented 1 year ago

Hi, thanks for giving this great tool. How can I set the each column have same y limit when using facet_grid2 with scales = "free",independent = "y", the follwing is my plot: 1679982776277

ggplot(region.df) +
  geom_rect(aes(xmin = start,xmax = end,
                ymin = 0, ymax = score,
                fill = fileName,color = fileName)) +
  # facet_grid(fileName~gene,scales = "free")
  facet_grid2(fileName~gene,scales = "free",independent = "y")

I want the each facet column have same y ranges. Thanks!

teunbrand commented 1 year ago

You can use scale_y_facet() to set the range for a group of panels. The first argument is an expression that instructs to which panels to apply the scales. It can also be COL == 1 and COL == 2 to work with the panel layout instead of the facetting variables.

library(ggh4x)
#> Loading required package: ggplot2

ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  facet_grid2(cyl ~ vs, scales = "free_y", independent = "y") +
  scale_y_facet(
    vs == 0,
    limits = range(mtcars$disp[mtcars$vs == 0])
  ) +
  scale_y_facet(
    vs == 1,
    limits = range(mtcars$disp[mtcars$vs == 1])
  )

Created on 2023-03-28 with reprex v2.0.2

junjunlab commented 1 year ago

Thank you very much, I will have try!