0

I am doing exploratory data analysis and trying to generate multiple histograms and boxplots for each column in my dataframe.

Here is the function that I tried to write:

iterate_boxplot <- function(df, y) {
  for (i in ncol(df)) {
   print(ggplot(df, aes(x=1, y=df[ ,i])) +
           geom_boxplot()+
           labs(
             title = "Boxplot of 'colnames(df[ ,i])'",
             y = "'colnames(df[ ,i])'")+
  Sys.sleep(2)
  )
}
  }
> iterate_boxplot(cont_ptchar_b)
Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Error in is.finite(x) : default method not implemented for type 'list'

The "cont_ptchar_b" object is a tibble of 4 double type columns.

Many many thanks for your help from a novice R user.

JanTsa
  • 1
  • 1
  • Use `paste` in `title` i.e. `title = paste("Boxplot of ", names(df)[i])` and also use `y = .data[[i]]` in `aes` – akrun Jul 10 '22 at 20:46
  • @akrun Thank you for your help, I am still getting errors. I ran the function with your edits, and got the following message: Error in `.data[[4L]]`: ! Must subset the data pronoun with a string, not an integer. Run `rlang::last_error()` to see where the error occurred. Called from: signal_abort(cnd, .file) – JanTsa Jul 12 '22 at 02:34
  • Sorry, you need `.data[[names(df)[i]]]` – akrun Jul 12 '22 at 17:16

0 Answers0