teunbrand / ggnomics

A small project to add ggplot2 extensions
https://teunbrand.github.io/ggnomics/
Other
80 stars 4 forks source link

Set scales per facet #14

Closed teunbrand closed 4 years ago

teunbrand commented 4 years ago

Currently, it is a bit of a pain to precisely control what the limits of a facet are. Maybe, there might something that can be done to give more control, something that would work like the following:

position_scales_by_facet(
  list(
    scale_x_continuous(limits = c(0, 100)),
    scale_x_continuous(limits = c(50, 150))
  )
)
teunbrand commented 4 years ago

Note to self:

Probably best to make the function overrule the init_scales() function in the Facet* ggprotos to accept custom scales. Alternative would be to make a new facet function, but that would lose all flexibility the current facetting options provide.

The following worked on data in the wild:

nfacet$init_scales <- function(layout, x_scale = NULL, y_scale = NULL, params) {
  scales <- list()
  if (!is.null(x_scale)) {
    scales$x <- lapply(seq_len(max(layout$SCALE_X)), function(i) {x_scale$clone()})
  }
  if (!is.null(y_scale)) {
    scales$y <- list(
        upper = scale_y_continuous(trans = "reverse", expand = c(0,0), name = "",
                     labels = function(x) {paste0(x / 1e6, "Mb")}),
        lower = scale_y_continuous(trans = "reverse", expand = c(0,0), name = "",
                                   limits = c(1, -1), oob = squish, 
                                   breaks = c(-1, 0, 1))
    )
  }
  scales
}
teunbrand commented 4 years ago

This is now implemented in the dev branch.