lethalSinger
lethalSinger

Reputation: 616

Using `coord_flip` with `facet_nested`

Here is a MWE:

Here is some data:

data <- 
structure(list(cilow = c(0.0528306698161456, 0.0708961798923575, 
-0.0860844842473584, 0.149293656874665, 0.175348742108531, 0.00150087452513042
), fullest = c(0.194641049761067, 0.282228250041396, 0.107053849480737, 
0.296390973553158, 0.38517505087032, 0.205531742216253), cihigh = c(0.336451429705988, 
0.493560320190435, 0.300192183208833, 0.443488290231651, 0.595001359632109, 
0.409562609907376), regressor = c("1. Plant 1", "2. Plant 2", 
"3. Plant 3", "4. Plant 4", "5. Plant 5", "6. Plant 6"), group = c("A. Green", 
"A. Green", "A. Green", "B. Blue", "B. Blue", "C. Red"), title = c("(i) Flowers", 
"(i) Flowers", "(i) Flowers", "(i) Flowers", "(ii) Trees", "(ii) Trees"
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

This is what I have tried so far:

ggplot(data, aes(forcats::fct_rev(regressor), fullest)) +
    geom_hline(yintercept = 0, linetype = "dashed", size = 0.3) +
    geom_errorbar(
      aes(ymin = cilow, ymax = cihigh),
      width = 0, size = 0.8) +
    geom_point(
      show.legend = F) +
    theme_bw() +
    facet_nested(. ~ group + title, scales = "free", space = "free")
    facet_wrap(. ~ group, cols = 1)

I would like to flip this around such that there is 1 column of 3 facets, stacked on top of each other. The first-level "group" facet titles should be horizontal and at the top of each facet. The second level titles ("title") should be vertical and connect the relevant facets on their left side. The "regressor" names should be on the right hand side. Is there a way to achieve this coord-flip with the nested titles?

Upvotes: 0

Views: 45

Answers (1)

Jon Spring
Jon Spring

Reputation: 66925

Here's a bit of a hack using patchwork and semi-manually defined sub-plots.

library(tidyverse)
library(patchwork)

plot_facet <- function(grp_no) {
  data |>
    group_by(group) |>
    filter(group_indices() == grp_no) |>
ggplot(aes(forcats::fct_rev(regressor), fullest)) +
  geom_hline(yintercept = 0, linetype = "dashed", size = 0.3) +
  geom_errorbar(
    aes(ymin = cilow, ymax = cihigh),
    width = 0, size = 0.8) +
  geom_point(
    show.legend = F) +
  theme_bw() +
  labs(x = NULL) +
  ggh4x::facet_nested(title ~ group, scales = "free", space = "free", switch = "y")
}

plot_facet(1) /
  plot_facet(2) /
  plot_facet(3)

enter image description here

Or with ggh4x::facet_nested(.~ group + title , scales = "free", space = "free", switch = "y")

enter image description here

The more built-in (for ggh4x::facet_nested) would be to have the group and title headers in the same region of the plot:

ggplot(data, aes(forcats::fct_rev(regressor), fullest)) +
  geom_hline(yintercept = 0, linetype = "dashed", size = 0.3) +
  geom_errorbar(
    aes(ymin = cilow, ymax = cihigh),
    width = 0, size = 0.8) +
  geom_point(
    show.legend = F) +
  theme_bw() +
  ggh4x::facet_nested(group + title~., scales = "free", space = "free", switch = "y")

enter image description here

Upvotes: 1

Related Questions