Home > Blockchain >  Boxplot in for-loop over multiple columns in r
Boxplot in for-loop over multiple columns in r

Time:11-15

I have a dataset with 7 columns and want to plot the 6 latter each with the first one. 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:

For 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

What am I missing?

CodePudding user response:

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)

CodePudding user response:

Another option:

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