微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使密度和直方图 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 举报,一经查实,本站将立刻删除。