rfor-loopquantile

Creating qqnorm plots over mulitple with correct column name


I have made this for loop to make create qqnorms for all specified columns. However it doesn't output the names correctly. I need the column name to align with the correct plot.

par(mfrow = c(3,2))
loop.vector <- 3:7 # designate columns for qqnorms
colName <- names(Final_data[loop.vector]) # gets column name
for (i in loop.vector) {
    x <- Final_data[, i]
    qqnorm(x, main = paste(colName))
}

Outputs:

enter image description here

This is what I need:

enter image description here


Solution

  • There is a typo in your loop. Might be easier to use

    par(mfrow = c(3, 2))
    lapply(names(mtcars)[3:7], \(i) qqnorm(mtcars[[i]], main = i))
    

    where we use mtcars as toy data.