在 ggplot 中仅绘制多个直方图的轮廓以清晰可视化

SiD*_*SiD 1 r ggplot2

下面是生成两个直方图的代码。但由于重叠严重,很难清楚地看到每个分布。

我怎样才能只绘制每个直方图的较粗轮廓并删除垂直线/条。

在此输入图像描述

理想的直方图应该是这样的 -

在此输入图像描述

library(tidyverse)

A <- rnorm(n = 1000, mean = 0, sd = 1)
B <- rnorm(n = 1000, mean = 0.1, sd = 1.3)

tbl <- tibble(A, B) %>%
  # pivot longer
  pivot_longer(cols = everything(),
               names_to = "Distributions",
               values_to = "values")

ggplot(data = tbl,
       aes(x = values,
           col = Distributions,
           fill = Distributions)) + 
  geom_histogram(bins = 50,
                 alpha = 0.3,
                 position = "identity") + 
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

teu*_*and 5

您可以将无边界直方图与geom_step()轮廓图层结合起来。

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1

A <- rnorm(n = 1000, mean = 0, sd = 1)
B <- rnorm(n = 1000, mean = 0.1, sd = 1.3)

tbl <- tibble(A, B) %>%
  # pivot longer
  pivot_longer(cols = everything(),
               names_to = "Distributions",
               values_to = "values")

ggplot(data = tbl,
       aes(x = values,
           col = Distributions,
           fill = Distributions)) + 
  geom_step(
    stat = "bin", bins = 50,
    direction = "mid"
  ) +
  geom_histogram(
    bins = 50, alpha = 0.3, colour = NA,
    position = "identity"
  )
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v2.0.1)于 2021-09-24 创建