1

So I have a function where I create scatter plots for a list of variables. I would like to title those scatter plots and do that in the same function. How may I reference a variable name when I iterate through a list? Is there something like plot(sectors[[i]],l, xlab= name(sectors[[i]])...

plot_correlation_index <- function(a,b,c,d,e,f,g,h,i,j,k,l){
  sectors <- list(a,b,c,d,e,f,g,h,i,j,k)
  for(i in 1:length(sectors)) {plot(sectors[[i]],l)}
}
biglig
  • 55
  • 6
  • As you have it, `sectors` *doesn't* have names. [See here for discussion and solution](https://stackoverflow.com/q/16951080/903061) to that aspect of the problem. If you did have a named list, you could use `names(sectors)[i]`. – Gregor Thomas May 05 '20 at 20:51
  • the function takes variables as the arguments. I'll try that, thanks – biglig May 05 '20 at 20:57
  • This is complicated by being in a function---and I could use some clarification. Do you the names to be a, b, c, or do you want them to be whatever the user passes in? E.g., if the user calls `plot_correlation_index(a = x, b = y, c = z)`, do you want the labels to be a,b, c or x, y, z? – Gregor Thomas May 05 '20 at 20:57
  • The function uses variables as the arguments and objects in the list. I am doing a bunch of correlation plots and just trying to iterate them through – biglig May 05 '20 at 20:59
  • I don't understand what you mean by "variables as the arguments and objects in the list". Could you shorten the example (surely `a, b, c` is enough), provide some sample input, show how you want to call the function, and describe the result you want? Perhaps use the built-in `mtcars` data as sample data? – Gregor Thomas May 05 '20 at 21:03
  • Gregor, they'd be x,y,z – biglig May 05 '20 at 21:05
  • Our variables are lists of numbers mats <- c(1,2,3,4,5,6,7,8,9,10,11), fins <- c(2,4,6,...) ,djia <- c(3,4,3,4,3,4,5...) Then I would use plot_correlation_index(mats,fins,inds,...,djia) to make a correlation plot between mats,djia; fins,djia, and so on. I put those in a list to iterate through, maybe it could've been done better. Then I want to label each corr plot with the proper mats,djia or fins,djia so I can tell which one is which. Does this clear up my problem? Thank you for helping – biglig May 05 '20 at 21:13

1 Answers1

1

I would suggest something like this. It's a bit more involved, but switching to ... allows for a variable number of arguments to make things much more flexible. It will plot all the arguments except the last one on the x axis, and the last one on the y axis.

plot_correlation_index = function(...) {
  argnames = unlist(lapply(as.list(sys.call())[-1], deparse))
  args = list(...)
  n_args = length(args)
  for (i in 1:(n_args - 1)) {
    plot(args[[i]], args[[n_args]], xlab = argnames[i], ylab = argnames[n_args])
  }
}


mats <- 1:11
fins <- 1:11 * 2
djia <- 1:11 + rnorm(11)
plot_correlation_index(mats, fins, djia)

plot_correlation_index(mtcars$mpg, mtcars$cyl)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294