【问题标题】:How could I align two ggpubr histograms so they are facing each other?我如何对齐两个 ggpubr 直方图,使它们彼此面对?
【发布时间】:2021-11-07 18:19:57
【问题描述】:

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

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

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

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

很抱歉没有直接发布图片,但我是堆栈溢出的新手,不允许我这样做。希望有人可以帮助我解决这个问题。谢谢!

【问题讨论】:

标签: r ggplot2 ggpubr


【解决方案1】:

我不知道 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 创建

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2015-11-12
    • 2022-10-15
    • 2014-03-09
    相关资源
    最近更新 更多