【问题标题】:Jitter points by different amounts based on condition根据条件不同数量的抖动点
【发布时间】:2016-08-15 12:20:10
【问题描述】:

我有一个包含离散 X 轴值和大量 Y 值的数据集。我还有一个单独的向量,用于测量 X 轴值的不确定性;这种不确定性在 X 轴上变化。我想抖动我的 X 轴值与这个不确定性度量成比例的量。使用循环执行此操作很容易但很麻烦;我正在寻找一个有效的解决方案。

可重现的例子:

#Create data frame with discrete X-axis values (a)
dat <- data.frame(a = c(rep(5, 5), rep(15,5), rep(25,5)), 
                  b = c(runif(5, 1, 2), runif(5, 2, 3), runif(5, 3, 4)))

#Plot raw, unjittered data
plot(dat$b ~ dat$a, data = dat, col = as.factor(dat$a), pch = 20, cex = 2)

#vector of uncertainty estimates
wid_vec <- c(1,10,3)

#Ugly manual jittering, not feasible for large datasets but 
#produces the desired result
dat$a_jit <- c(jitter(rep(5, 5), amount = 1), 
                jitter(rep(15, 5), amount = 10), 
                jitter(rep(25, 5), amount = 3))

plot(dat$b ~ dat$a_jit, col = as.factor(dat$a), pch = 20, cex = 2)

#Ugly loop solution, also works

newdat <- data.frame()
a_s <- unique(dat$a)

for (i in 1:length(a_s)){
  subdat       <- dat[dat$a == a_s[i],]
  subdat$a_jit <- jitter(subdat$a, amount = wid_vec[i])
  newdat <- rbind(newdat, subdat)
}

plot(newdat$b ~ newdat$a_jit, col = as.factor(newdat$a), pch = 20, cex = 2)

#Trying to make a vectorized solution, but this of course does not work.

jitter_custom <- function(x, wid){
  j <- x + runif(length(x), -wid, wid)
  j
}

#runif() does not work this way, this is shown to indicate the direction 
#I've been attempting

基本上,我需要按条件拆分 dat,调用 wid_vec 向量中的相关条目,然后通过根据 wild_vec 值修改 dat 条目来创建一个新列。听起来应该有一个优雅的 dplyr 解决方案,但它现在让我望而却步。

感谢所有建议!

【问题讨论】:

    标签: r plot visualization jitter


    【解决方案1】:

    作为替代

    set.seed(1)
    dat$a_jit <- c(jitter(rep(5, 5), amount = 1), 
                    jitter(rep(15, 5), amount = 10), 
                    jitter(rep(25, 5), amount = 3))
    

    你可以的

    set.seed(1)
    x <- with(dat, jitter(a, amount=setNames(c(1,10,3), unique(a))[as.character(a)]))
    

    结果是一样的:

    identical(x, dat$a_jit)
    # [1] TRUE
    

    如果您希望警告消失,您可以将suppressWarnings() 包裹在jitter(...) 周围,或者使用类似with(dat, mapply(jitter, x=a, amount=setNames(c(1,10,3), unique(a))[as.character(a)])) 的内容。

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2023-01-08
      • 1970-01-01
      相关资源
      最近更新 更多