1

i'm new to R. I'm trying to boxplot the data (df, list) in each sub-list using lapply. I have written this function:

group.box <- function(x) {
   lapply(X = x, FUN = boxplot)
}

Running it on the list that contains 6 sub-lists gives me 6 individual boxplot graph (6 separated graphs) and this text:

$sublist1
NULL

$sublist2
NULL

$sublist3
NULL

...

I tried to combine these graphs into one picture with 6 graphs:

par(mfrow=c(2,3))
group.box(data)
dev.off()

But then I only get the text (as displayed above) with no graphs. I thought maybe I should just export these 6 graphs into one pdf file.

Thank you!

Keity
  • 143
  • 1
  • 10
  • Welcome to R :-). Could you please provide some more information about the data you want to plot, so that you have [created a fully reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This will make it easier to help. Depending on the amount of data use `dput(yourdata)` or `dput(head(yourdata))`. – Manuel Bickel Nov 20 '17 at 12:48
  • Thank you for your reference and suggestion, after running `dput(data)` and `dput(head(data))` which contained a large amount of data in various types that I don't think would be helpful. I usually try to bring maximum information. thank you again! :) – Keity Nov 21 '17 at 11:43
  • 1
    Ok, I see. Since the types included in the data are relevant for many tasks and functions you might also try `str(yourdata)`, which gives information about the structure of your object. Might still be too much information, but you can give it a try, simply wanted to add this for the purpose of learning. – Manuel Bickel Nov 21 '17 at 12:33
  • OK, it is very much appreciated :) – Keity Nov 22 '17 at 09:24

1 Answers1

1

You could try

data <- data.frame(a = rnorm(100), b = rnorm(100), c = rnorm(100), d = rnorm(100), e = rnorm(100), f = rnorm(100))

group.box <- function(x, plot_row, plot_col) {
    quartz()
    par(mfrow=c(plot_row,plot_col))
    lapply(X = x, FUN = boxplot)
}

group.box(data, 2,3)

You can of course use png(...) or pdf(...) etc. instead of quartz()

tobiasegli_te
  • 1,413
  • 1
  • 12
  • 18
  • Thank you so much! I just want to add, that after the `lapply` function within the `group.box` function, a `dev.off()` command is necessary. – Keity Nov 21 '17 at 11:25
  • Only if you write to a file (png/pdf...), if you open a quartz window you probably want it to remain open. If you found my answer helpful you can [accept it](https://stackoverflow.com/help/someone-answers) – tobiasegli_te Nov 21 '17 at 13:31