You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggdist包中实现ROPE区间可视化及后验密度占比标注?

Solution to Add ROPE Interval, Coloring, and Proportion Labels

Here's a modified implementation that meets all three requirements:

Step 1: Precompute ROPE Proportions

First, calculate the percentage of posterior draws within the [-0.25, 0.25] ROPE for each group combination:

# Calculate proportion of draws inside ROPE for each group
rope_prop <- compCat %>%
  group_by(Species, SepalLength_cat) %>%
  summarize(
    rope_pct = scales::percent(mean(.epred >= -0.25 & .epred <= 0.25)),
    .groups = "drop"
  )

Step 2: Modified ggplot Code

Choose either the density slab (smoother) or histogram (matching your original style) option below:

Option 1: Density Slab

compCat %>%
  ggplot(aes(x = .epred, y = SepalLength_cat)) +
  # Slab with fill split by ROPE membership
  stat_slabinterval(
    aes(fill = after_stat(ifelse(x >= -0.25 & x <= 0.25, "Inside ROPE", "Outside ROPE"))),
    point_interval = median_hdi,
    .width = c(.95),
    slab_color = "gray30",
    interval_color = "black"
  ) +
  # Vertical lines marking ROPE boundaries
  geom_vline(xintercept = c(-0.25, 0.25), color = "red", linetype = "dashed") +
  # Add ROPE proportion labels
  geom_text(
    data = rope_prop,
    aes(x = 0, y = SepalLength_cat, label = rope_pct),
    color = "darkred",
    vjust = -0.5,
    size = 3.5
  ) +
  # Customize scales and theme
  scale_fill_manual(values = c("Inside ROPE" = "lightgreen", "Outside ROPE" = "skyblue")) +
  scale_thickness_shared() +
  facet_grid(. ~ Species) +
  labs(
    y = "Sepal Length comparison",
    x = "Sepal Width (in mm)",
    fill = ""
  ) +
  theme_minimal() +
  theme(
    legend.position = "top",
    strip.text = element_text(face = "bold")
  )

Option 2: Histogram (Matching Original Code)

compCat %>%
  ggplot(aes(x = .epred, y = SepalLength_cat)) +
  # Histogram with fill split by ROPE membership
  stat_histinterval(
    aes(fill = after_stat(ifelse(x >= -0.25 & x <= 0.25, "Inside ROPE", "Outside ROPE"))),
    point_interval = median_hdi,
    .width = c(.95),
    color = "gray30",
    interval_color = "black"
  ) +
  # Vertical lines marking ROPE boundaries
  geom_vline(xintercept = c(-0.25, 0.25), color = "red", linetype = "dashed") +
  # Add ROPE proportion labels
  geom_text(
    data = rope_prop,
    aes(x = 0, y = SepalLength_cat, label = rope_pct),
    color = "darkred",
    vjust = -0.5,
    size = 3.5
  ) +
  # Customize scales and theme
  scale_fill_manual(values = c("Inside ROPE" = "lightgreen", "Outside ROPE" = "skyblue")) +
  scale_thickness_shared() +
  facet_grid(. ~ Species) +
  labs(
    y = "Sepal Length comparison",
    x = "Sepal Width (in mm)",
    fill = ""
  ) +
  theme_minimal() +
  theme(
    legend.position = "top",
    strip.text = element_text(face = "bold")
  )

Key Features Explained

  • ROPE Interval: geom_vline adds clear red dashed lines for the [-0.25, 0.25] boundaries.
  • Colored ROPE Area: The after_stat conditional in the fill aesthetic splits the distribution into distinct colors for inside/outside the ROPE.
  • Proportion Labels: Precomputed rope_prop data frame provides the percentage of posterior draws inside the ROPE, placed above each category for readability.
  • Retained Original Elements: Keeps the median point and 95% HDI interval from your initial code, along with faceting by species.

内容的提问来源于stack exchange,提问作者llewmills

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 08:29:53