将 bin 与 stat_histinterval 垂直对齐

问题描述 投票:0回答:1

我正在尝试创建一个多面图,显示同一面上的多个组密度分布和直方图,但我在自定义直方图上的分箱时遇到问题,这些直方图是使用

stat_histinterval
创建的。我已将图块宽度更改为一致(
breaks = breaks_fixed(width = .2)
),但请注意,在左下角,图块具有不同的对齐方式。我知道
stat_histinterval
有对齐参数,但它们似乎不能垂直工作。有人可以帮助我将垃圾箱与起始值或中心值对齐吗?

这是我的代码:

library(ggplot2)
library(ggdist)

data <- mtcars
data$cyl <- as.factor(data$cyl)
ggplot(data, aes(y = wt , col = cyl, group = cyl, fill = cyl))+
  stat_halfeye(
    slab_color = "gray45",
    alpha = .2
  ) +
  stat_histinterval(
    slab_type = "histogram",
    slab_color = "gray45",
    side = "left",
    alpha = .2,
    outline_bars = TRUE,
    breaks = breaks_fixed(width = .2)
  ) +
  facet_grid(vs ~ am) +
  labs(x = "Density")
r ggplot2 histogram facet-grid ggdist
1个回答
0
投票
无论几何图形是水平还是垂直,

align
都应该起作用。在你的例子中,这是有效的:

library(ggplot2)
library(ggdist)

data <- mtcars
data$cyl <- as.factor(data$cyl)
ggplot(data, aes(y = wt , col = cyl, group = cyl, fill = cyl))+
  stat_halfeye(
    slab_color = "gray45",
    alpha = .2
  ) +
  stat_histinterval(
    slab_color = "gray45",
    side = "left",
    alpha = .2,
    outline_bars = TRUE,
    breaks = breaks_fixed(width = .2),
    align = align_boundary(at = 0)
  ) +
  facet_grid(vs ~ am) +
  labs(x = "Density")

(我也将

slab_type = "histogram"
从您的通话中删除为
stat_histinterval()
,因为它已被弃用)。

© www.soinside.com 2019 - 2024. All rights reserved.