r - geom_密度用facet_wrap创建扁平线

标签 r ggplot2

我尝试用箱线图绘制密度线,但我只得到一条与数据分布不相似的扁平线。我错过了什么?

library(tidyverse)
as_tibble(iris) %>%
  pivot_longer(!Species, names_to = "Measure", values_to = "Value") %>%
  ggplot(aes(Value, Measure)) +
  geom_density() +
  geom_boxplot(width = .25, position = position_nudge(y = -.15), outlier.shape = NA) +
  facet_wrap(~ Species, strip.position = "right", nrow = 3)

创建于 2022 年 8 月 24 日 reprex v2.0.2

最佳答案

您可以使用ggridges中的geom_密度_ridges ,它根据提供的数据计算密度,然后将其绘制如下:

library(tidyverse)
library(ggridges)
as_tibble(iris) %>%
  pivot_longer(!Species, names_to = "Measure", values_to = "Value") %>%
  ggplot(aes(Value, Measure)) +
  geom_density_ridges() +
  geom_boxplot(width = .25, position = position_nudge(y = -.15), outlier.shape = NA) +
  facet_wrap(~ Species, strip.position = "right", nrow = 3)  
#> Picking joint bandwidth of 0.0883
#> Picking joint bandwidth of 0.152
#> Picking joint bandwidth of 0.166

创建于 2022 年 8 月 24 日 reprex v2.0.2

geom_密度对于显示平滑直方图很有用。

关于r - geom_密度用facet_wrap创建扁平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73475234/

相关文章:

r - ggplot2强制y轴从原点开始并 float y轴上限

r - 仅绘制某些箱线图的抖动点

r - 如何在 ggplot2 中调整颜色形状组合的图例?

r - 受范数不等式约束的二次函数最小化

R:调整 ggplot2 中的比例颜色渐变

performance - 按列排序或矩阵的 top-n

r - stat_summary计算失败, 'what'必须是字符串或函数

r - 自动读取R中折线图的值

R:从列表中删除空格

r - 在 ggplot 中使用多层美学