How can I create a shaded histogram in ggplot2?

Viewed 179

Here is an example in base R

hist(diamonds$carat,
     col="black",
     density=25,
     angle=60)

enter image description here

I can't make the ggplot version work and I can't find anything in the www:

library(ggplot2)
ggplot(diamonds, aes(carat)) +
  geom_histogram() + theme_bw()

Background: In most cases I don't print colors, so I would prefer to encode in style rather than fill color.

1 Answers

Here's a reprex that shows one approach using ggpattern:

devtools::install_github("coolbutuseless/ggpattern")
library(ggpattern)
library(ggplot2)

ggplot(data = diamonds, aes(x = carat)) +
  stat_bin(geom = "bar_pattern",
           breaks = 0:10 * 0.5,
           fill = "white",
           color = "black", 
           pattern_fill = "gray50",
           pattern_angle = 60,
           pattern_density = 0.3,
           pattern_spacing = 0.01,
           pattern_key_scale_factor = 1) +
  theme_classic()

enter image description here

Related