r中多列for循环中的箱线图

Boxplot in for-loop over multiple columns in r

我有一个包含 7 列的数据集,我想用第一列绘制后面的 6 列。如果一个人完成,一切正常,但显然我在循环时错过了一些东西。 这是我的数据框:

colnames(df) <- c("real", "est1", "est2", "est3", "est4", "est5", "est6")

head(df)
  real         est1         est2         est3        est4        est5         est6
1    6 1.040217e-05 7.693853e-05 0.0006782929 0.002676282 0.033385059 0.9631730251
2    6 1.065455e-05 7.880501e-05 0.0006947352 0.002740934 0.034161665 0.9623132055
3    5 1.037427e-03 7.607541e-03 0.0624143732 0.185340034 0.536009785 0.2075908392
4    1 2.345527e-01 4.855757e-01 0.2374464964 0.032691816 0.008846185 0.0008870667
5    5 3.506084e-04 2.585847e-03 0.0222474072 0.079120851 0.458854341 0.4368409455
6    3 1.710639e-03 1.247417e-02 0.0978889632 0.250555703 0.500355545 0.1370149767

和代码

boxplot( est1 ~ real, data=df, main="Estimated Probability for Category 1 Given the Real Categories")

工作正常,但如果我做与循环完全相同的事情,它不会:

looper <- c("est1", "est2", "est3", "est4", "est5", "est6") #to get the column names to loop over

counter <- 0     # for the boxplot's title

for (i in all_of(looper)){
  counter <- counter +1
boxplot( i ~ real, data=df, 
        main = paste("Estimated Probability for Category",counter,"Given the Real Categories")
        )
}

我想这与使用 i 的方式有关,我尝试了 "i" 和一个 `,但我总是会遇到以下错误之一:

对于i:Fehler in stats::model.frame.default(formula = i ~ real, data = eval.m1_sim) : Variablenlängen sind unterschiedlich (gefunden für 'real')

For "i" or ` : Fehler in terms.formula(formula, data = data) : 模型形式中的 ungültiger 术语

我错过了什么?

您可以通过列号:

# random example data as no reproducible example was given
df <- data.frame(
  real = sample(1:4, 20, TRUE),
  one = runif(20),
  two = runif(20),
  three = runif(20))
)

# graphics paramaters so we see all at once
par(mfrow = c(3,1), mar = c(2, 2, 1, 1))

# the easiest way is through column numbers
for(column in 2:4)
  boxplot(df[[column]] ~ df$real)

另一个选项:

library(tidyverse)
df %>%
  pivot_longer(-real) %>%
  mutate(real = factor(real)) %>%
  ggplot(aes(real, value)) + 
  geom_boxplot() + 
  facet_wrap(~name)