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

调整Beta(1/2,1/2)分布绘图适配x=0.05至1.0区间

调整Beta(1/2, 1/2)分布绘图适配x=0.05到1.0区间

我正尝试调整Beta(1/2, 1/2)分布的绘图,使其适配x=0.05到1.0之间的垂直区域。以下R代码已接近目标效果,但需要让U型曲线完全贴合该区间,同时接受曲线偏离0.5中心的情况。

当前代码

library(ggplot2)

# 设置形状参数
shape1 <- .5
shape2 <- .5

# 设置上下限
lower <- 0.05
upper <- 0.99888

# 计算0.05到接近1区间内的PDF值
df <- data.frame(x = seq(lower, upper, length.out = 1000), 
                 y = dbeta(seq(lower, upper, length.out = 1000), shape1, shape2))

# 绘制PDF,自定义x轴刻度,隐藏y轴元素
ggplot(df, aes(x = x, y = y)) + 
  geom_line() +
  xlab(expression(kappa[j])) + ylab("") +
  scale_x_continuous(limits = c(0, 1), expand = c(0, 0), 
                     breaks = seq(0, 1, 0.5), labels = c("0.0", "0.5", "1.0")) +
  theme_classic() +
  theme(axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank()) +
  geom_vline(xintercept = 0.05, color = "black") +
  geom_vline(xintercept = 1, color = "black") +
  labs(title = "")

当前生成的绘图中,U型曲线左端已对齐x=0.05,但右端距离x=1的垂直线仍有空隙,未完全贴合目标区间。

修改后的代码

要让曲线完全贴合x=0.05到1.0的区间,只需调整数据生成的上限至更接近1的位置,并优化曲线平滑度:

library(ggplot2)

shape1 <- .5
shape2 <- .5

lower <- 0.05
# 调整上限至更接近1,避免dbeta(1, ...)返回无穷大
upper <- 0.9999

df <- data.frame(x = seq(lower, upper, length.out = 2000),  # 增加点数提升曲线平滑度
                 y = dbeta(seq(lower, upper, length.out = 2000), shape1, shape2))

ggplot(df, aes(x = x, y = y)) + 
  geom_line() +
  xlab(expression(kappa[j])) + ylab("") +
  scale_x_continuous(limits = c(0, 1), expand = c(0, 0), 
                     breaks = seq(0, 1, 0.5), labels = c("0.0", "0.5", "1.0")) +
  theme_classic() +
  theme(axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank()) +
  geom_vline(xintercept = 0.05, color = "black") +
  geom_vline(xintercept = 1, color = "black") +
  labs(title = "")

修改说明

  • 将upper设为0.9999,让曲线右端尽可能贴近x=1的垂直线,同时规避dbeta(1, 0.5, 0.5)返回无穷大导致的绘图异常。
  • 把length.out增加到2000,提升曲线平滑度,让边缘贴合更自然。
  • 保持x轴显示范围为0到1,曲线严格从x=0.05开始,完全适配目标垂直区间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 03:05:20