I was creating histograms with ggplot2
in R
whose bins are separated with colors and noticed one thing. When the bins of a histogram are separated by colors with fill
option, the density value of the histogram turns funny.
Here is the data.
set.seed(42)
x <- rnorm(10000,0,1)
df <- data.frame(x=x, b=x>1)
This is a histogram without fill
.
ggplot(df, aes(x = x)) +
geom_histogram(aes(y=..density..))
This is a histogram with fill
.
ggplot(df, aes(x = x, fill=b)) +
geom_histogram(aes(y=..density..))
You can see the latter is pretty crazy. The left side of the bins is sticking out. The density values of the bins of each color are obviously wrong.
I thought over this issue for a while. The data can't be wrong for the first histogram was normal. It should be something in ggplot2
or geom_histogram
function. I googled "geom_histogram density fill" and couldn't find much help.
I want the end product to look like:
- Separated by colors as you see in the second histogram
- Size and shape identical to the first histogram
- The vertical axis being density
How would you deal with issue?