标记密度图

时间:2017-10-24 12:32:19

标签: r plot labels

在下图中,如何仅沿x轴打印标准偏差值?

R打印5,7,10,13,15,19,20,22,25。

x <- rnorm(3e3, 16, 3)
plot(density(x), type = "l", lwd = 2, col = 254,
main = "Random Variable X", xact = "n")
axis(side = 1, at = c(7, 10, 13, 19, 22, 25),
labels = c("7","10","13","19", "22", "25"))
abline(v = 16, lwd = 2)
abline(v = c(7, 10, 13, 19, 22, 25), col = "darkgreen")

谢谢..

2 个答案:

答案 0 :(得分:1)

这样的事情:

x <- rnorm(3e3, 16, 3);
plot(
    density(x), 
    type = "l", lwd = 2, col = 254,
    main = "Random Variable X", xaxt = "n");
axis(
    side = 1, 
    at = mean(x) + seq(-3, 3, by = 1) * sd(x),
    labels = seq(-3, 3, by = 1));

enter image description here

x轴显示区域中的sd单位[-3σ,+3σ]。

答案 1 :(得分:1)

这是另一种方法。

同样的另一个变体是删除x <- scale(x)并拥有y <- dnorm(x, mean(x), sd(x))。我想很容易看出我做了什么。

此方法与使用density(x)之间的区别在于如何估算密度。

如果您知道参数中的分布,在这种情况下的正态分布,并且可以估计最大似然估计mean(x), sd(x),您可以使用它们来估计密度。

set.seed(42)
rnorm(3e3, 16, 3)

# normalize N(0, 1)
x <- scale(x)
# order so plotting is easy
x <- x[order(x)]
# density 
y <- dnorm(x)
plot(x, y, type = "l", lwd = 2, col = 254, ylab = "density", 
    main = "Random Variable X")

enter image description here