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