【问题标题】:How do I use the parameter 'bw=sj' in R如何在 R 中使用参数“bw=sj”
【发布时间】:2020-11-02 13:30:18
【问题描述】:

我们被告知要使用给定的数据制作直方图和线条。我可以制作我认为正确的直方图。然而,我们被告知在密度函数中使用 bw='sj'。我不明白我将如何使用它。 我尝试将它放在 hist() 函数中,因为我认为它是一个参数,但是我收到一条错误消息:

警告信息: 1:在 plot.window(xlim, ylim, "", ...) 中:"bw" 不是图形参数 2:在标题中(main = main,sub = sub,xlab = xlab,ylab = ylab,...): “bw”不是图形参数 3:在轴(1,...)中:“bw”不是图形参数 4:在axis(2,...)中:“bw”不是图形参数

这是我处理 R 中问题的代码的一部分。

# histogram 1
rdi4p -> data_shhs[,'rdi4p']


hist(rdi4p  ,probability=TRUE,col=rgb(0,0,1,1/4),breaks=30,
         xlab="rdi4p",
         main="Histogram 1",col.axis="blue")

lines(x=density(x= rdi4p),type="l",col="blue",lwd=3)

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    当然,我没有要处理的数据(特别是我们需要知道 rdi4psj 是什么让这完全可重现),所以我会为这些变量:

    set.seed(1)                     # Make example reproducible
    rdi4p <- rnorm(1000)            # Vector of 1000 samples from normal distribution
    sj    <- diff(range(rdi4p))/30  # 1/30 of the range of vector rdi4p
    

    现在我们使用您的代码绘制直方图:

    hist(rdi4p, probability = TRUE, col = rgb(0, 0, 1, 1/4), breaks = 30,
         xlab = "rdi4p", main = "Histogram 1", col.axis = "blue")
    

    然后我们添加该行。请注意,我们必须将参数 bw = sj 传递给 density 函数,该函数本身位于对 lines 的调用中:

    lines(x = density(x = rdi4p, bw = sj), type = "l", col = "blue", lwd = 3)
    

    【讨论】:

    • 我明白了,我对如何使用它感到困惑,但我现在明白了。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2018-12-19
    • 2011-05-22
    • 2021-06-15
    相关资源
    最近更新 更多