y axis break with facets

Hello. I am trying to produce a plot with a discontinuous y-axis but can't get the facet titles to only show once:

Example Data:

data(mpg)
library(ggplot2)
> ggplot(mpg, aes(displ, cty)) +
+     geom_point() +
+     facet_grid(. ~ drv)

After much digging it appears that this is impossible in ggplot2, but I have discovered the gg.gap package. However, this package replicates the facet titles for each segment of the plot. Let's say I want a break in the y axis from 22-32 as follows:

library(gg.gap)
gg.gap(plot = p,
       segments = c(22, 32),
       ylim = c(0, 35))

Capture

Nirgrahamuk's solution seems to work for bar plots, but does not work when plotting with facets. Facet titles still appear for each plot segment. I would be grateful for any insight of help anyone could provide! I'm stumped.

I know this is possible if I plot in base R, but given other constraints I am unable to do so.

You can have any facet titles you want with a labeller function:
Construct labelling specification — labeller • ggplot2 (tidyverse.org)

1 Like

Thanks @martin.R ! I am trying to remove the facet titles for the bottom row though - I don't see that I can do this with the labeller function but maybe I'm missing something?

they might be a facet grid solution or facet_zoom
data(mpg)
library(ggplot2)
library(tidyverse)
#library(ggforce)

mpg$city_below_22 <- ifelse(mpg$cty< 24,"below 24","above 32")
mpg<- mpg %>%
filter(cty < 24 | cty >32)

ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(city_below_22 ~ drv,scales="free",space="free")

Thanks @smouksassi! That did the trick :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.