在ggplot中叠加直方图和直方图边框

tom*_*omw 5 r ggplot2

我想在直方图上叠加一个直方图边框,但它们不在正确的位置

library(tidyverse)
data("iris")

iris %>% 
  ggplot(
    aes(Sepal.Length)
  ) +
  geom_histogram(
    alpha = .5
  ) +
  stat_bin(geom="step") +
  facet_wrap(
    ~Species, ncol = 1
  )
Run Code Online (Sandbox Code Playgroud)

返回

在此处输入图片说明

如何将边框与直方图对齐?

Mou*_*d_S 5

可以通过指定binwidth然后设置breaks

library(tidyverse)
data("iris")

iris %>% 
  ggplot( aes(Sepal.Length)) +
  geom_histogram(alpha = .5, binwidth = .1) +
  stat_bin(geom="step", breaks = seq(3,8, .1)) +
  facet_wrap( ~Species, ncol = 1)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明