Closed MaryamEO closed 1 year ago
Hi --- without knowing the underlying data, it's hard to tell what might be wrong. If you prepare a reprex that generates a chart with the problem you're having I'd be happy to help!
closing due to lack of reprex --- feel free to re-open if you provide a reprex
I think I have the same or a similar problem as @MaryamEO, but I'm not sure whether 'ggdist' is expected to work like that or whether I have missed an already existing solution.
The problem is that plotting densities or histograms of variables with very different ranges results in very different scaling of the resulting density/histograms. This is of course no surprise because density is no probability.
However when creating a facetted plot with scales = "free"
, I had expected that the densities would all get scaled to the maximum density in each panel, but they do not; they are rather scaled to the maximum overall density (just as when setting scales = "fixed"
):
library(ggplot2)
library(posterior)
#> This is posterior version 1.2.2
#>
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#>
#> mad, sd, var
set.seed(4545)
x <-
tibble::tibble(
x =
posterior::rvar_rng(rnorm, n = 4, mean = 0, sd = c(0.0001, 0.5, 2, 40), ndraws = 1000),
id = seq_along(x)
)
# facetted plot with four sets of samples from normal distributions with very different x axis scaling
x |>
ggplot() +
ggdist::stat_slab(mapping = aes(xdist = x)) +
facet_wrap( ~ id, scales = "free")
# same excluding the first group
x |>
dplyr::filter(id != 1) |>
ggplot() +
ggdist::stat_slab(mapping = aes(xdist = x)) +
facet_wrap( ~ id, scales = "free")
Created on 2024-01-04 by the reprex package (v2.0.1)
A possible workaround is to manually compute the maximum density and pass as scale
to stat_slab()
:
x |>
dplyr::mutate(
scale =
purrr::map_dbl(seq_along(x), function(i) {
max(stats::density(attr(x[[i]], "draws")[, 1])$y)
}),
scale = max(scale)/scale
) |>
ggplot() +
ggdist::stat_slab(mapping = aes(xdist = x, scale = scale)) +
facet_wrap( ~ id, scales = "free")
But as mentioned, I'm not sure whether this is how stat_slab()
should behave when using facet_wrap()
with scales = "free"
Well, I should've read the documentation better first. I'm sorry for that!
library(ggplot2)
library(posterior)
#> This is posterior version 1.2.2
#>
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#>
#> mad, sd, var
set.seed(4545)
x <-
tibble::tibble(
x =
posterior::rvar_rng(rnorm, n = 4, mean = 0, sd = c(0.0001, 0.5, 2, 40), ndraws = 1000),
id = seq_along(x)
)
x |>
ggplot() +
ggdist::stat_slab(mapping = aes(xdist = x), normalize = "panels") +
facet_wrap( ~ id, scales = "free")
Created on 2024-01-04 by the reprex package (v2.0.1)
No worries that's a bit of the docs folks often miss! I have plans to make it easier to find (#205)
I have been trying to combine facet_wrap() with stat_halfeye() in my plot. While the box plots are correctly organized in the facets, the density plots are not shown.
The line of codes that I have been using is mentioned below!
fig <- ggplot(Distribution, aes( y = sum, x = AF)) + ggdist::stat_halfeye( aes(color = Phenotype, fill = after_scale(alpha(color, 0.4))), adjust = .5, width = .6, .width = 0, justification = -.4, point_color = NA) + geom_boxplot( aes(color = Phenotype, color = after_scale(alpha(color, 0.4)), fill = after_scale(desaturate(lighten(color, .8), .4))), width = .3, )+ facet_wrap(~as.factor(category), nrow = 3)