绘图使用ggplot2移动t分布

时间:2017-02-03 14:23:58

标签: r ggplot2

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

#!/usr/bin/perl
use strict;
use warnings;
my ($id, $name, @ips);

while(<DATA>){
    chomp;
    if ($. == 1){
        $id = $_;   
    }
    elsif ($. == 2){
        $name = $_;
    }
    else{
        push @ips, $_;  
    }
}

print "$id\n";
print "$name\n";
print join ",", @ips;

__DATA__
id: 123456789
name: anytownusa
1.2.3.4/32
5.6.7.8/32

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

3 个答案:

答案 0 :(得分: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 :(得分: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))

答案 2 :(得分: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)
}