Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay histogram and histogram border in ggplot

Tags:

r

ggplot2

I'd like to overlay a histogram border over the histogram, but they're not in the right location

library(tidyverse)
data("iris")

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

returns

enter image description here

How do I align the border with the histogram?

like image 399
tomw Avatar asked Feb 21 '20 19:02

tomw


1 Answers

it can be done by specifying the binwidth and then setting the 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

like image 165
Mouad_Seridi Avatar answered Oct 24 '22 00:10

Mouad_Seridi