使密度的底部和直方图的 y 轴对齐?
Make the bottom of the density and histogram y axes aligned?
我在同一个图上绘制密度和直方图我可以做如下:
set.seed(1)
ex=rnorm(4000 , 120 , 30)
hist(ex, col="#00AFBB", prob=TRUE, breaks=100)
lines(density(ex), col="#E7B800")
但我不想将概率设置为 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]])
但是align_plots
的align
参数怎么设置都好像两个y都对齐不好。如何对齐两个y轴的0点?
我认为您可以通过对 scale_y_continuous()
使用相同的 expand
值来使轴对齐。在下面的代码中,我复制了你第二次用 mult=c(0,0.05)
调用它来替换第一个计数轴。我不知道生成的密度和直方图具有相同的“面积”,但它们看起来很接近。
set.seed(1)
ex=rnorm(4000 , 120 , 30)
hist(ex, col="#00AFBB", prob=TRUE, breaks=100)
lines(density(ex), col="#E7B800")
library(ggpubr)
library(cowplot)
phist <- gghistogram(
ex,
# rug = TRUE,
color = "#00AFBB",
bins=100,
# add_density = TRUE,
) +
# formatted this to be the same as the left axis, except on right
# same expansion multiple
scale_y_continuous(expand = expansion(mult = c(0, 0.05)), 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]])
我在同一个图上绘制密度和直方图我可以做如下:
set.seed(1)
ex=rnorm(4000 , 120 , 30)
hist(ex, col="#00AFBB", prob=TRUE, breaks=100)
lines(density(ex), col="#E7B800")
但我不想将概率设置为 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]])
但是align_plots
的align
参数怎么设置都好像两个y都对齐不好。如何对齐两个y轴的0点?
我认为您可以通过对 scale_y_continuous()
使用相同的 expand
值来使轴对齐。在下面的代码中,我复制了你第二次用 mult=c(0,0.05)
调用它来替换第一个计数轴。我不知道生成的密度和直方图具有相同的“面积”,但它们看起来很接近。
set.seed(1)
ex=rnorm(4000 , 120 , 30)
hist(ex, col="#00AFBB", prob=TRUE, breaks=100)
lines(density(ex), col="#E7B800")
library(ggpubr)
library(cowplot)
phist <- gghistogram(
ex,
# rug = TRUE,
color = "#00AFBB",
bins=100,
# add_density = TRUE,
) +
# formatted this to be the same as the left axis, except on right
# same expansion multiple
scale_y_continuous(expand = expansion(mult = c(0, 0.05)), 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]])