绘图使用ggplot2移动t分布

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

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

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

有没有办法简单地沿x轴移动分布?

r ggplot2
3个回答
4
投票

您还可以为移位的t分布制作自定义函数:

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

1
投票

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

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))

0
投票

在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)
}
© www.soinside.com 2019 - 2024. All rights reserved.