提问人:ElizaBeso000 提问时间:11/10/2023 更新时间:11/10/2023 访问量:16
将箱与stat_histinterval垂直对齐
Align bins vertically with stat_histinterval
问:
我正在尝试创建一个多面图,显示同一面中的多个组密度分布和直方图,但我在自定义直方图上的条柱时遇到了问题,直方图是使用 创建的。我更改了图格宽度以在图()之间保持一致,但请注意,在左下角,图格具有不同的对齐方式。我知道有对齐参数,但它们似乎没有垂直工作。有人可以帮我将箱与起始值或中心值对齐吗?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")
答:
1赞
Matthew Kay
12/16/2023
#1
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()
评论
breaks = seq(min(data$wt), max(data$wt), by = 0.2)