繁体   English   中英

用ggplot2绘制移位的t分布

[英]Plot shifted t-distribution with ggplot2

我正在尝试使用ggplot2绘制平均值= 3和df = 1.5的t分布的密度曲线。 但是,它应该在3左右对称,所以我不能使用noncentrality参数。

ggplot(data.frame(x = c(-4, 10)), aes(x = x)) +
stat_function(fun = dt, args = list(df = 1.5))

有没有一种方法可以简单地沿x轴移动分布?

您还可以为转移的t分布创建自定义函数:

custom <- function(x) {dt(x - 3, 1.5)}
ggplot(data.frame(x = c(-4, 10)), aes(x = x)) +
    stat_function(fun = custom)

一个简单的解决方案是只更改标签:

ggplot(data.frame(x = c(-4, 10)), aes(x = x)) +
    stat_function(fun = dt, args = list(df = 1.5)) + 
    scale_x_continuous(breaks = c(0, 5, 10), labels = c(3, 8, 13))

metRology软件包中还有一个dt.scaled函数,除了df之外,还可以指定均值和小数位数。

相关代码:

dt.scaled <- function(x, df, mean = 0, sd = 1, ncp, log = FALSE) {
        if (!log) stats::dt((x - mean)/sd, df, ncp = ncp, log = FALSE)/sd
        else stats::dt((x - mean)/sd, df, ncp = ncp, log = TRUE) - log(sd)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM