Agent K
Agent K

Reputation: 21

Discontinuous/broken axis in facet_grid (ggplot2)

Is there a way to create a discontinuous x-axis in each of my facet grid plots? I have a 2 × 2 facet grid and I want to have a break between the first and second points. The x-axis is on a log10 scale so I want to show the discontinuity between those two points (the first point being my fake 0 value, which I made equal to the x-value of the second point 0.781, divided by 4). I want to have a break between 0.781/2 and 0.781, preferably with a symbol on the axis to show the discontinuity.

This is how the graph looks normally Original plot

This is what happens when I try the ggbreak library, adding scale_x_break(c(0.781/2, 0.781)) in my code.

Plot with ggbreak

As you can see this isn't exactly what I'd like as the plots have segmented away from each other. I want a graph that looks like this:

Desired

I appreciate any help!

My Code

library(ggplot2)
library(ggbreak)

# Plot
p.8 <- d.8 %>%
  ggplot(aes(x = x, color = cond,)) +
  facet_grid(type ~ comp) +
  scale_x_log10(
    limits = c(10^-1,10^2),
    breaks = c(0.781/4,10^0,10^1,10^2),
    labels = function(labs){
      newlabs <- lapply(labs, log10)
      newlabs[[1]] <- "Ø"
      return(newlabs)
    },
  ) +
  scale_x_break(c(0.781/2, 0.781)) + #Attempted code
  ## Points
  stat_summary(
    geom = "point", 
    aes(y = y),
    fun = mean, 
    shape = 15, 
    size = 3
  ) +
  labs(
    x = "x", 
    y = "y",
  ) +
  theme(
    strip.text.y = element_textbox(
      orientation = "right-rotated",
    )
  )

# dput(d.8)
structure(list(comp = c("comp1", "comp1", "comp1", "comp1", "comp1",
"comp1", "comp1", "comp1", "comp1", "comp1", "comp1", "comp1", 
"comp1", "comp1", "comp1", "comp1", "comp1", "comp1", "comp1",
"comp1", "comp1", "comp1", "comp1", "comp1", "comp1", "comp1",
"comp1", "comp1", "comp1", "comp1", "comp1", "comp1", "comp1", 
"comp1", "comp1", "comp1", "comp2", "comp2", "comp2", "comp2",
"comp2", "comp2", "comp2", "comp2", "comp2", "comp2", "comp2",
"comp2", "comp2", "comp2", "comp2", "comp2", "comp2", "comp2", 
"comp2", "comp2", "comp2", "comp2", "comp2", "comp2", "comp2",
"comp2", "comp2", "comp2", "comp2", "comp2", "comp2", "comp2", 
"comp2", "comp2", "comp2", "comp2"), type = c("type1", "type1",
"type1", "type1", "type1", "type1", "type1", "type1", "type1", 
"type2", "type2", "type2", "type2", "type2", "type2", "type2",
"type2", "type2", "type1", "type1", "type1", "type1", "type1", 
"type1", "type1", "type1", "type1", "type2", "type2", "type2", 
"type2", "type2", "type2", "type2", "type2", "type2", "type1",
"type1", "type1", "type1", "type1", "type1", "type1", "type1", 
"type1", "type2", "type2", "type2", "type2", "type2", "type2",
"type2", "type2", "type2", "type1", "type1", "type1", "type1", 
"type1", "type1", "type1", "type1", "type1", "type2", "type2",
"type2", "type2", "type2", "type2", "type2", "type2", "type2"
), cond = c("cond1", "cond1", "cond1", "cond1", "cond1", "cond1", 
"cond1", "cond1", "cond1", "cond1", "cond1", "cond1", "cond1",
"cond1", "cond1", "cond1", "cond1", "cond1", "cond2", "cond2", 
"cond2", "cond2", "cond2", "cond2", "cond2", "cond2", "cond2",
"cond2", "cond2", "cond2", "cond2", "cond2", "cond2", "cond2",
"cond2", "cond2", "cond1", "cond1", "cond1", "cond1", "cond1",
"cond1", "cond1", "cond1", "cond1", "cond1", "cond1", "cond1", 
"cond1", "cond1", "cond1", "cond1", "cond1", "cond1", "cond2",
"cond2", "cond2", "cond2", "cond2", "cond2", "cond2", "cond2",
"cond2", "cond2", "cond2", "cond2", "cond2", "cond2", "cond2", 
"cond2", "cond2", "cond2"), x = c(100, 50, 25, 12.5, 6.25, 3.13,
1.56, 0.781, 0.19525, 100, 50, 25, 12.5, 6.25, 3.13, 1.56, 0.781,
0.19525, 100, 50, 25, 12.5, 6.25, 3.13, 1.56, 0.781, 0.19525,
100, 50, 25, 12.5, 6.25, 3.13, 1.56, 0.781, 0.19525, 100, 50, 
25, 12.5, 6.25, 3.13, 1.56, 0.781, 0.19525, 100, 50, 25, 12.5,
6.25, 3.13, 1.56, 0.781, 0.19525, 100, 50, 25, 12.5, 6.25, 3.13,
1.56, 0.781, 0.19525, 100, 50, 25, 12.5, 6.25, 3.13, 1.56, 0.781, 
0.19525), y = c(0.889965971976534, 0.774872226404536, 1.23783188856279,
1.24731593817019, 1.24756038633595, 1.23888067903866, 1.20771803143735,
1.22223483739853, 1.13493628620327, 0.50132298162012, 0.979266481384757, 
1.05236212940931, 1.11378501709881, 1.1356525813725, 1.09539310072071,
1.12061379468959, 1.11335813789531, 1.10633848984496, 0.827803710754651,
1.10051376516159, 1.18069662962694, 1.166756677486, 1.18410152787503,
1.13527165048817, 1.15312301466617, 1.0727626539158, 1.08381504454183, 
0.3778689887735, 0.95579528218527, 0.993183553168085, 1.04979692919271,
1.01041201233452, 1.1081414663175, 0.90147910083813, 1.03083530526742, 
1.04336017568079, 0.926850285950349, 0.902483830572804, 0.719953266805231,
0.723425336621009, 0.877409599669511, 0.784403441731968, 0.915420340558475,
1.10578581479712, 0.786129794348124, 0.00015882980851304, 0.590939175312857, 
0.827319519232864, 0.905880861800243, 0.956949596291066, 0.930873385708882,
0.960873207470942, 0.922380942007316, 0.944496705568747, 0.762488399867932,
0.863999576077165, 0.845914579548712, 1.00302384701821, 1.03392333790319, 
1.0087580147911, 1.07274081058937, 0.922220689880065, 0.844258630973813,
-0.0013639066869388, 0.22696625995875, 0.412497194601114, 0.745620747364923,
0.869868783918688, 0.974447394691819, 0.799568318205823, 0.932100689345611, 
0.844615199168336)), row.names = c(NA, -72L), class = c("tbl_df",
"tbl", "data.frame"))

# Some sessionInfo() info
R version 4.4.1 (2024-06-14 ucrt)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
 [1] ggbreak_0.1.2       extrafont_0.19      ggh4x_0.2.8
 [4] ggrepel_0.9.5       ggtext_0.1.2        mdthemes_0.1.0.9000
 [7] glue_1.7.0          latex2exp_0.9.6     httpgd_2.0.2       
[10] scales_1.3.0        lubridate_1.9.3     forcats_1.0.0
[13] stringr_1.5.1       dplyr_1.1.4         purrr_1.0.2
[16] readr_2.1.5         tidyr_1.3.1         tibble_3.2.1       
[19] ggplot2_3.5.1       tidyverse_2.0.0     librarian_1.8.1

Upvotes: 1

Views: 51

Answers (0)

Related Questions