I am plotting in ggplot2
densities for multiple groups that each have a different support. For this reason, I would like the density lines to stop at the min/max of the respective groups, to convey visually the difference in supports. I can do this with ggvis
, see:
However, I am not able to do this nicely with geom_density()
:
- By default (
trim=FALSE
)geom_density()
will extend the lines of the density at 0, so that all densities overlap at 0, makes difficult to compare support - Setting
trim=TRUE
,geom_density()
does not extend the lines outside of the respective support, yet will trim the lines even over the y axis.
How can I mimic the ggvis
plot with ggplot::geom_density
?
Code
library(tidyverse)
library(ggvis)
#>
#> Attaching package: 'ggvis'
#> The following object is masked from 'package:ggplot2':
#>
#> resolution
library(patchwork)
df <- tibble(x1=truncnorm::rtruncnorm(500, -1, 1),
x2=truncnorm::rtruncnorm(500, -1.5, 1.5),
x3=rnorm(500)) %>%
gather(variable, value, everything())
pl_gg_trim <- ggplot(df, aes(x=value, color=variable))+
geom_density(trim=TRUE) +
ggtitle("trim=TRUE: lines don't go to 0")
pl_gg_notrim <- ggplot(df, aes(x=value, color=variable))+
geom_density()+
ggtitle("trim=FALSE: hard to understand support of values")
pl_ggvis <- ggvis(df, ~value, fill = ~variable) %>%
group_by(variable) %>%
layer_densities()
#does not work with reprex: pl_ggvis
pl_gg_notrim/pl_gg_trim
Created on 2021-03-31 by the reprex package (v1.0.0)
geom_density(aes(fill = variable), alpha = 0.3)
in ggplot2, so what is it exactly you're looking for? - teunbrand