当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()

输出:
withoutJitter



withJitter

范例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()

输出:
withoutJitterFactor

withJitterFactor




相关用法


注:本文由纯净天空筛选整理自utkarsh_kumar大神的英文原创作品 Adding Noise to a Numeric Vector in R Programming – jitter() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。