I am trying to align two histograms so they face each other, like the image of this link: [Example image] (https://i.stack.imgur.com/mI6Q4.png)
I had the two individual histograms for each group, and I tried to align them using the function align_plots from cowplot. However, I end with something like this: ![My aligned plots] (https://i.stack.imgur.com/E04UQ.png)
Here is the code I used to align them, and regarding any changes in the align and axis parameters, the image still result in something similar to the second one posted above:
aligned_hist <- cowplot::align_plots(Asyhist, Symhist, align = "hv", axis = "b")
align_hist <- ggdraw(aligned_hist[[1]]) draw_plot(aligned_hist[[2]])
align_hist
Sorry for not posting the images directly, but I am new in stack overflow and didn't allow me to do it. Hope somebody could help me to figure this out. Thanks!
CodePudding user response:
I don't know about ggpubr histograms, but an approach with vanilla ggplot2 would be to use after_stat()
to change the value depending on the group.
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`.
Created on 2021-09-11 by the reprex package (v2.0.1)