gpt4 book ai didi

r - r : duplicate faceted geom_density and geom_density_ridges results 中的 ggplot

转载 作者:行者123 更新时间:2023-12-03 14:38:00 40 4
gpt4 key购买 nike

带有小平面的 ggplot2::geom_density()ggridges::geom_density_ridges() 的默认设置产生略有不同的曲线。如何修改其中一种或另一种的平滑技术以产生相同的结果?

library(tidyverse)
library(ggridges)

# standard density with facet by cyl ----
mtcars %>%
ggplot(aes(x = mpg)) +
geom_density(fill = "gray") +
facet_wrap(. ~ factor(cyl, levels = c(8, 6, 4)), ncol = 1) +
theme_minimal()

# density ridge with y = cyl ----
mtcars %>%
ggplot(aes(x = mpg, y = factor(cyl))) +
geom_density_ridges() +
theme_minimal()
#> Picking joint bandwidth of 1.38

reprex package 创建于 2019-04-04 (v0.2.1)

最佳答案

您可以使用 geom_density() 使用的相同统计数据。

library(tidyverse)
library(ggridges)

# standard density with facet by cyl ----
mtcars %>%
ggplot(aes(x = mpg)) +
geom_density(fill = "gray") +
facet_wrap(. ~ factor(cyl, levels = c(8, 6, 4)), ncol = 1) +
theme_minimal()

# density ridge with y = cyl ----
mtcars %>%
ggplot(aes(x = mpg, y = factor(cyl))) +
geom_density_ridges(stat = "density", aes(height = stat(density))) +
theme_minimal()

reprex package 创建于 2019-04-04 (v0.2.1)

或者,您可以获取 geom_density_ridges() 报告的带宽并在 geom_density() 中使用它(此处,bw = 1.38) .

library(tidyverse)
library(ggridges)

# density ridge with y = cyl ----
mtcars %>%
ggplot(aes(x = mpg, y = factor(cyl))) +
geom_density_ridges() +
theme_minimal()
#> Picking joint bandwidth of 1.38

# standard density with facet by cyl ----
mtcars %>%
ggplot(aes(x = mpg)) +
geom_density(fill = "gray", bw = 1.38) +
facet_wrap(. ~ factor(cyl, levels = c(8, 6, 4)), ncol = 1) +
theme_minimal()

reprex package 创建于 2019-04-04 (v0.2.1)

最后两个图看起来略有不同,因为它们具有不同的 x 轴限制。

关于r - r : duplicate faceted geom_density and geom_density_ridges results 中的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55523105/

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