使密度和直方图 y 轴的底部对齐?

问题描述

我可以在同一个图上绘制密度和直方图,如下所示:

set.seed(1)
ex=rnorm(4000,120,30)   

hist(ex,col="#00AFBB",prob=TRUE,breaks=100)
lines(density(ex),col="#E7B800")

enter image description here

但我不想将概率设置为 TRUE,因此我制作了一个密度图和一个频率直方图并将它们组合在一起,following this tutorial

library(ggpubr)
library(cowplot)
phist <- gghistogram(
  ex,# rug = TRUE,color = "#00AFBB",bins=100,# add_density = TRUE
) + 
  scale_y_continuous(position = "right")

# 2. Create the density plot with y-axis on the right
# Remove x axis elements
pdensity <- ggdensity(
  ex,color = "#E7B800",alpha = 0,# rug = TRUE
) +
  scale_y_continuous(expand = expansion(mult = c(0,0.05)),position = "left")  +
  theme_half_open(11,rel_small = 1) +
  rremove("x.axis") +
  rremove("xlab") +
  rremove("x.text") +
  rremove("x.ticks") +
  rremove("legend")

# 3. Align the two plots and then overlay them.
aligned_plots <- align_plots(phist,pdensity,align="vh",axis="lr")
ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])

enter image description here

但似乎无论我如何设置align的{​​{1}}参数,两个y都无法很好地对齐。如何对齐两个 y 轴的 0 点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)