简体   繁体   中英

Facet wrap plots with nested `ggforce::facet_zoom`

I'd like to make a plot with ggplot2::facet_wrap or facet_grid with nested plots that use ggforce::facet_zoom .

I'd also be happy enough lining them up in a similar fashion to facet_grid , preferably with the strip text labels, though also can't quite see a way to get that to work either.

Eg a faceted plot

library(ggplot2)
library(ggforce)
library(dplyr)

p1 <- ggplot(mtcars) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_wrap(
    facets = ~cyl,
    nrow = 2,
    scales = "free_y"
  )

p1

But each facet should be like this (this is the first facet above):


zoomlims <- data.frame(
  cyl = c(4, 6, 8),
  lower = c(27, 18, 13),
  upper =c(32, 21, 17)
)

p2a <- ggplot(mtcars %>% filter(cyl == 4)) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_zoom(
    ylim = c(zoomlims$lower[1], zoomlims$upper[1]),
    zoom.size = 1
  )

p2a

Ideally one facet call would take all the arguments to both zoom and wrap, but they won't, and calling one facet function after the other just overrides the previous one, eg zoom second

p3a <- ggplot(mtcars) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_wrap(
    facets = ~cyl,
    nrow = 2,
    scales = "free_y"
  ) +
  facet_zoom(
    ylim = c(zoomlims$lower, zoomlims$upper),
    zoom.size = 1
  )

p3a

Or wrap second



p3b <- ggplot(mtcars) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_zoom(
    ylim = c(zoomlims$lower, zoomlims$upper),
    zoom.size = 1
  ) +
  facet_wrap(
    facets = ~cyl,
    nrow = 2,
    scales = "free_y"
  )

p3b

It might be possible to mimic this with patchwork, but that doesn't work either , and wouldn't include the strip text from wrapping (though could annotate).


library(patchwork)
p2b <- ggplot(mtcars %>% filter(cyl == 6)) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_zoom(
    ylim = c(zoomlims$lower[1], zoomlims$upper[1]),
    zoom.size = 1
  )

p2c <- ggplot(mtcars %>% filter(cyl == 8)) +
  geom_boxplot(
    aes(
      x = factor(gear),
      y = mpg,
      fill = factor(gear)
    )
  ) +
  facet_zoom(
    ylim = c(zoomlims$lower[1], zoomlims$upper[1]),
    zoom.size = 1
  )


p4 <- p2a + p2b + p2c + plot_layout(nrow = 2, guides = "collect")

p4

Thoughts?

Created on 2020-02-14 by the reprex package (v0.3.0)

I think the new version of ggforce fixes this problem Just install the new version with devtools::install_github("thomasp85/ggforce")

Release text: https://github.com/thomasp85/ggforce/releases/tag/v0.4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM