我试图用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轴移动分布?
您还可以为移位的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)
}