简体   繁体   English

r中多列的for循环中的Boxplot

[英]Boxplot in for-loop over multiple columns in r

I have a dataset with 7 columns and want to plot the 6 latter each with the first one.我有一个包含 7 列的数据集,并且想用第一个绘制后 6 个。 If done for one, everything works, but apparently I miss something when looping.如果为一个完成,一切正常,但显然我在循环时错过了一些东西。 Here is my dataframe:这是我的数据框:

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

and the code和代码

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

works fine, but if I do the exactly same as a loop, it doesn't:工作正常,但如果我执行与循环完全相同的操作,则不会:

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 suppose it has to do with the way i is used, I tried "i" and also with one ` and I would always get one of the following errors:我想这与i的使用方式有关,我尝试了"i"和一个 `,但我总是会遇到以下错误之一:

For i : Fehler in stats::model.frame.default(formula = i ~ real, data = eval.m1_sim) : Variablenlängen sind unterschiedlich (gefunden für 'real')对于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 Term in Modellformel对于"i"或` : Fehler in terms.formula(formula, data = data) : ungültiger Term in Modellformel

What am I missing?我错过了什么?

You could go via column numbers:您可以通过列号:

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

Another option:另外一个选项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM