Reputation: 101
The default settings for ggplot2::geom_density()
with a facet and ggridges::geom_density_ridges()
yield slightly different curves. How can I modify the smoothing technique in one or the other to yield the same result?
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
Created on 2019-04-04 by the reprex package (v0.2.1)
Upvotes: 3
Views: 794
Reputation: 17790
You can use the same stat that geom_density()
uses.
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()
Created on 2019-04-04 by the reprex package (v0.2.1)
Alternatively, you can take the bandwidth that geom_density_ridges()
reports and use it in geom_density()
(here, 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()
Created on 2019-04-04 by the reprex package (v0.2.1)
The final two plots look slightly different because they have different x axis limits.
Upvotes: 1