R语言 向数字向量添加噪声 – jitter()函数
在R编程中,抖动意味着向一个数字向量对象添加少量的随机噪声。在这篇文章中,我们将学习如何使用 jitter() 函数,并创建一个图来显示它们。
语法:jitter(x, factor)
参数:
x: 代表数字向量
factor: 代表因子规格的数字值
例子1:
# Define numeric vectors
x <- round(runif(1000, 1, 10))
y <- x + rnorm(1000, mean = 0, sd = 5)
# output to be present as PNG file
png(file="withoutJitter.png")
# Plotting without jitter function
plot(x, y, xlim = c(0, 11),
main = "Without Jitter Function")
# saving the file
dev.off()
x_j <- jitter(x)
# output to be present as PNG file
png(file="withJitter.png")
# Plotting with jitter function
plot(x_j, y, xlim = c(0, 11),
main = "With Jitter Function")
# saving the file
dev.off()
输出:
例2:有大的因子值
# Define numeric vectors
x <- round(runif(1000, 1, 10))
y <- x + rnorm(1000, mean = 0, sd = 5)
# output to be present as PNG file
png(file="withoutJitterFactor.png")
# Plotting without jitter function
plot(x, y, xlim = c(0, 11),
main = "Without Jitter Function")
# saving the file
dev.off()
x_j <- jitter(x, factor = 2)
# output to be present as PNG file
png(file="withJitterFactor.png")
# Plotting with jitter function
plot(x_j, y, xlim = c(0, 11),
main = "With Jitter Function and Large Factor")
# saving the file
dev.off()
输出: