After constructing a histogram I'd like to add an upper boundary/outline to my plot. I don't want to use geom_bar
or geom_col
because I don't want the vertical boundaries for each bin.
My attempts have included using geom_histogram
and stat_bin(geom = "bin")
, however the bins don't align.
I've adjusted parameters within each geom (bins
, binwidth
, center
, boundary
) and haven't been able to align these distributions. There have been similar questions on SO (Overlaying geom_points on a geom_histogram or stat_bin) but none seem to have a similar problem to mine or offer a solution.
Here is a case where my geom layers don't align:
set.seed(2019)
library(ggplot2)
library(ggthemes)
df <- data.frame(x = rnorm(100),
y = rep(c("a", "b"), 50))
p <- df %>%
ggplot(aes(x, fill = y)) +
geom_histogram() +
facet_wrap(vars(y)) +
theme_fivethirtyeight() +
guides(fill = F)
This is plot p
, my base histogram:
p + stat_bin(geom = "step")
I desire a plot where these two geoms align. I've tested a variety of dummy data and this continues to be an issue. Why don't these geoms naturally align? How can I adjust either of these layers to align? Is there a better alternative than combining histogram and stat bin to achieve my desired plot?