我怎样才能对齐两个 ggpubr 直方图,使它们面对面?

How could I align two ggpubr histograms so they are facing each other?

我正在尝试对齐两个直方图,使它们彼此面对,就像这张 link 的图像: [示例图片] (https://i.stack.imgur.com/mI6Q4.png)

我有每个组的两个单独的直方图,我尝试使用 cowplot 中的函数 align_plots 对齐它们。但是,我以这样的方式结束: ![我对齐的地块] (https://i.stack.imgur.com/E04UQ.png)

这是我用来对齐它们的代码,关于对齐和轴参数的任何更改,图像仍然会产生与上面发布的第二个类似的结果:

aligned_hist <- cowplot::align_plots(Asyhist, Symhist, align = "hv", axis = "b")
align_hist <- ggdraw(aligned_hist[[1]]) + draw_plot(aligned_hist[[2]])
align_hist

抱歉没有直接发布图片,但我是 stack overflow 的新手,不允许我这样做。希望有人能帮我解决这个问题。谢谢!

我不知道 ggpubr 直方图,但使用香草 ggplot2 的方法是使用 after_stat() 根据组更改值。

library(ggplot2)

set.seed(0)
df <- data.frame(
  x = c(rnorm(100, 1), rnorm(100, 2)),
  group = rep(c("A", "B"), each = 100)
)

ggplot(df, aes(x, fill = group)) +
  geom_histogram(
    aes(y = after_stat(ifelse(group == 2, -1, 1) * count))
  ) +
  scale_y_continuous(labels = abs)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex package (v2.0.1)

创建于 2021-09-11