最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - keep plots together with facet_wrap() and scale_y_break() - Stack Overflow

programmeradmin6浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

I 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)
发布评论

评论列表(0)

  1. 暂无评论