R:类型为 ="h"的 plot() 函数错误表示小数字(对于 "lwd"的较大值)

标签 r visualization probability-distribution

我正在尝试生成一个显示二项式 (10, 0.3) 分布概率的图。

我想在 base R 中执行此操作。

下面的代码是我想出的最好的,

plot(dbinom(1:10, 10, 0.3), type="h", lend=2, lwd=20, yaxs="i")

我对上述代码的问题是小数字得到不成比例的大条。 (见下文)例如 P(X = 8) = 0.00145,但图中的高度看起来约为 0.025。

Binomial(10, 0.3) Distribution

这似乎是通过想要更宽的条形图而创建的人工制品,如果删除 lwd = 20 参数,您会得到很小的条形图,但它们的高度似乎具有代表性。

最佳答案

我认为问题出在您对 lend(行尾)参数的选择。 'round' (0) 和 'square' (2) 选项适用于当您想要在段末尾之外进行一点额外扩展时,例如这样相邻的段就可以很好地连接起来,例如如果您绘制的线段应该是连接线的一部分(参见下面的示例)。

f <- function(le) plot(dbinom(1:10, 10, 0.3), 
        type="h", lend = le, lwd=20, yaxs="i", main = le)
par(mfrow=c(1,3))
invisible(lapply(c("round", "butt", "square"), f))

enter image description here

“round”、“butt”和“square”也可以指定为(不太便于内存)0、1 和 2 ...

x <- 1:5; y <- c(1,4,2,3,5)
f2 <- function(le) {
   plot(x,y, type ="n", main = le)
   segments(x[-length(x)], y[-length(x)], x[-1], y[-1],
        lwd = 20, lend  = le)
}
par(mfrow=c(1,3))
invisible(lapply(c("round", "butt", "square"), f2))

enter image description here

在这里您可以看到圆形端盖运行良好,“butt”和“square”都有问题。 (我不能随便想到“正方形”的用例,但我确定存在一个……)有一个很好的线图参数描述here (虽然它也没有建议用例......)

关于R:类型为 ="h"的 plot() 函数错误表示小数字(对于 "lwd"的较大值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74452941/

相关文章:

r - 创建分类变量

R 和 ggplot2 : how do I connect the dots for a line chart and polar coordinates?

r - 调整 R 中的图形图例

reshape 表格: year vs monthly data

r - 匹配R正则表达式中的字母

javascript - 谷歌饼图显示小切片外部的切片文本

visualization - 如何在 Mapbox、Openstreetmap 中可视化等距 map

performance - 如何生成随机 "enough"的随机整数?

python - 如何使用python将对数概率转换为0到1值之间的简单概率