I want to make a plot composed out of two plots with both having the same broken y axis and forced into one column. However, ggplot2
always mixes the two plots into this scheme:
[plot1 higher y-axis]
[plot2 higher y-axis]
[plot1 lower y-axis]
[plot2 lower y-axis]
But I need:
[plot1 higher y-axis]
[plot1 lower y-axis]
[plot2 higher y-axis]
[plot2 lower y-axis]
I guess the solution is pretty easy, but I just can't seem to find it. Here's an MRE:
library(ggplot2)
library(ggbreak)
library(tibble)
xs <- c(1, -1, 1, -1)
ys <- c(5, 100, 10, 200)
group <- c("one", "one", "two", "two")
data <- tibble(xs = xs,
xy = ys,
group = group)
(
plot <-
ggplot(data, aes(xs, ys)) +
geom_point() +
facet_wrap(~group,
ncol = 1) +
scale_y_break(c(20, 90))
)
I want to make a plot composed out of two plots with both having the same broken y axis and forced into one column. However, ggplot2
always mixes the two plots into this scheme:
[plot1 higher y-axis]
[plot2 higher y-axis]
[plot1 lower y-axis]
[plot2 lower y-axis]
But I need:
[plot1 higher y-axis]
[plot1 lower y-axis]
[plot2 higher y-axis]
[plot2 lower y-axis]
I guess the solution is pretty easy, but I just can't seem to find it. Here's an MRE:
library(ggplot2)
library(ggbreak)
library(tibble)
xs <- c(1, -1, 1, -1)
ys <- c(5, 100, 10, 200)
group <- c("one", "one", "two", "two")
data <- tibble(xs = xs,
xy = ys,
group = group)
(
plot <-
ggplot(data, aes(xs, ys)) +
geom_point() +
facet_wrap(~group,
ncol = 1) +
scale_y_break(c(20, 90))
)
Share
Improve this question
edited Mar 28 at 9:03
xilliam
2,2792 gold badges17 silver badges29 bronze badges
asked Mar 27 at 11:23
gernophilgernophil
4955 silver badges18 bronze badges
1 Answer
Reset to default 0I would say to make sub plots and then arrange them together with patchwork library.
From what I understood, this should be what you're looking for:
library(patchwork)
plot1 <-
ggplot(data=data[data$group=="one",], aes(x=xs, y=xy)) +
geom_point() +
scale_y_break(c(20, 90))
plot2 <-
ggplot(data=data[data$group=="two",], aes(x=xs, y=xy)) +
geom_point() +
scale_y_break(c(20, 90))
design <-"12"
plot1 + plot2 + plot_layout(design = design)