rggplot2facetggforce

ggforce : force facet_zoom to set on the lateral side of the graph


Is there a way to force the zoom from the function facet_zoom() from the package ggforce on the lateral side of the graph instead of the bottom ?

I have this code

require(dplyr)
require(tidyverse)
require(ggforce)

g + facet_zoom(xlim = c(x1,x2)))

How should I proceed?

EDIT

Through the really good answer provided by Stefan I can add a picture representing what I want (I am definitely open for other ways to reach it).

Expected graph


Solution

  • It is possible but it requires a bit of a hack. As far as I get it from the docs this could only be achieved when zooming both in the x and y direction. To this end you could set the zoom in the y direction to y=TRUE and afterwards set split=FALSE and horizontal = TRUE. Doing so will place the zoom panel on the left of the main plot. Additionally we have to get rid of the background fill for the zoom in the y direction via theme option zoom.y.

    Using the example data and code from your former post:

    Note: One side effect of using theme_bw is that we get some segments at the panel borders which connect the main and the zoom panel. Personally I didn't like that so I added color=NA to theme(zoom.y = element_rect(fill = NA, color = NA)) to remove these lines.

    DebitH %>%
      ggplot(aes(x = Date, y = Débit_horaire)) +
      geom_point(alpha = 6 / 10, size = 0.3, color = "blue") +
      geom_line(alpha = 4 / 10, size = 0.5, color = "blue") +
      labs(x = "Date", y = "Débit Horaire (m3/s)") +
      theme_bw() +
      theme(
        panel.grid.major.y = element_line(
          color = "grey",
          size = 0.25,
          linetype = 2
        ),
        legend.position = "none"
      ) +
      facet_zoom(
        xlim = as.POSIXct(c("2021-01-15 00:00:00", "2021-02-15 00:00:00")),
        y = TRUE, zoom.size = .5,
        split = FALSE, horizontal = TRUE
      ) +
      theme(zoom.y = element_rect(fill = NA, color = NA))
    

    enter image description here