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

5
我想在直方图上覆盖一个直方图边框,但它们的位置不正确。
library(tidyverse)
data("iris")

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

返回值

在此输入图片描述

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


1
如果您使用geom_histogram(alpha = 0.5, color = "blue"),那么这是否符合您的要求(我只添加了color = "blue")。 - steveb
@steveb -- 不,那并不能解决问题。 - tomw
我的建议忘了加上你应该删除 stat_bin("step"),这将为你提供直方图框周围的边框,而不需要尝试使用步进函数。我需要澄清一下,你是想在框上加一个边框还是移动 stat_bin - steveb
我想要整个分布外面只有一个边框,而不是直方图中每个条形上都有单独的边框。也就是说,将步骤向左移动。 - tomw
好的,现在我明白了。 - steveb
1个回答

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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接