gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:34:50 26 4
gpt4 key购买 nike

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

Normal distributions第一个情节有这段代码:

  ggplot(test, aes(y=key,x=value)) + 
geom_path()+
coord_flip()

第二个有下面这个:

  ggplot(test, aes(x=value, fill=key)) +
geom_density() +
coord_flip()

当我们阅读有关正态分布的统计书籍时,经常会看到这种多重分布图。到目前为止,我得到的最有用的链接是这个 here .

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

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()

非常感谢

最佳答案

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

Ridgeline plots are partially overlapping line plots that create the impression of a mountain range. They can be quite useful for visualizing changes in distributions over time or space.

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

添加中线:

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

模拟地毯:

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

reprex package 创建于 2018-10-16 (v0.2.1.9000)

关于r - 同一图中的多个分布——使用 ggplot2 中的 geom_density 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52845746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com