同一图中的多个分布-使用ggplot2中的geom_density函数

Lui*_*uis 3 r ggplot2 tidyverse

我认为我非常接近完成此代码,但是我在这里遗漏了一些东西。我想像这样将两个图“组合”为一个图:

正态分布 第一个情节具有以下代码:

  ggplot(test, aes(y=key,x=value)) + 
  geom_path()+
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

第二个是下面这个:

  ggplot(test, aes(x=value, fill=key)) +
  geom_density() +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

当我们了解正态分布时,这种多重分布图通常会在统计手册中看到。到目前为止,我最有用的链接就是这里的链接

请使用此代码重现我的问题:

library(tidyverse)
test <- data.frame(key = c("communication","gross_motor","fine_motor"),
                   value = rnorm(n=30,mean=0, sd=1))
ggplot(test, aes(x=value, fill=key)) +
  geom_density() +
  coord_flip()

ggplot(test, aes(y=key,x=value)) + 
  geom_path(size=2)+
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

非常感谢

Tun*_*ung 5

您可能对ggridges软件包中的山脊线图感兴趣。

脊线图是部分重叠的线图,可产生山脉的印象。它们对于可视化时间或空间分布的变化非常有用。

library(tidyverse)
library(ggridges)

set.seed(123)
test <- data.frame(
  key = c("communication", "gross_motor", "fine_motor"),
  value = rnorm(n = 30, mean = 0, sd = 1)
)

ggplot(test, aes(x = value, y = key)) + 
  geom_density_ridges(scale = 0.9) + 
  theme_ridges() +
  NULL
#> Picking joint bandwidth of 0.525
Run Code Online (Sandbox Code Playgroud)

添加中线:

ggplot(test, aes(x = value, y = key)) +
  stat_density_ridges(quantile_lines = TRUE, quantiles = 2, scale = 0.9) +
  coord_flip() +
  theme_ridges() +
  NULL
#> Picking joint bandwidth of 0.525
Run Code Online (Sandbox Code Playgroud)

模拟地毯:

ggplot(test, aes(x = value, y = key)) + 
  geom_density_ridges(
    jittered_points = TRUE,
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = '|', point_size = 3, point_alpha = 1, alpha = 0.7,
  ) +
  theme_ridges() +
  NULL
#> Picking joint bandwidth of 0.525
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.2.1.9000)创建于2018-10-16