如何在ggplot2中创建阴影直方图?

时间:2020-10-09 12:21:05

标签: r ggplot2

这是基数R中的一个例子

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

enter image description here

我无法使ggplot版本正常工作,也无法在www:

中找到任何内容
library(ggplot2)
ggplot(diamonds, aes(carat)) +
  geom_histogram() + theme_bw()

背景:在大多数情况下,我不打印颜色,所以我宁愿使用样式编码,也不愿填充颜色。

1 个答案:

答案 0 :(得分:4)

以下是使用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