1

我有一个带有数值变量和因子变量的数据框,如下所示:

set.seed(123)
df <- data.frame(
  numbers = c(rnorm(50, 3), runif(50)),
  levels = sample(LETTERS[1:5], 100, replace = T)
)

我想做的是一个条形图,它绘制df$numbers插入代表每个级别平均值的垂直线段线。df$levels

stripchart(df$numbers ~ df$levels, method = "jitter")

显然,我可以分别为每个级别插入均值行,例如:

segments(x0 = mean(df$numbers[df$levels=="A"]), y0 = 1-0.3, y1 = 1+0.3, col = "red" )

对于所有其他级别,依此类推,如果您有多个级别,这会很乏味。所以我试过这个for循环:

for(i in seq(unique(df$levels))){
  segments(x0 = mean(df$numbers[df$levels==i]),
           y0 = i - 0.3,
           y1 = i + 0.3,
           col = "red", lty = 3, lwd = 2)
}

但这不会打印任何东西(也不会引发错误)。插入均值段的最干净和最简单的代码是什么?

4

1 回答 1

1

由于“级别”列是factor,用于levels获取factor“un1”的级别,然后遍历唯一元素的序列,获取mean“数字”的levels列是唯一值以创建segments

un1 <- levels(df$levels)
for(i in seq_along(un1)){
 segments(x0 = mean(df$numbers[df$levels==un1[i]]),
       y0 = i - 0.3,
       y1 = i + 0.3,
       col = "red", lty = 3, lwd = 2)
}

在此处输入图像描述

- 检查mean

with(df, tapply(numbers, levels, FUN = mean))
#      A        B        C        D        E 
#1.390202 1.541655 2.086605 2.377122 1.663159 
于 2019-11-04T18:33:19.180 回答