1

I would like to write a function to create plots (in order to create multiple plots without listing the design settings every time). The pirateplot function that I use requires columnnames and a dataframe as input, which causes problems.

My not-working code is:

pirateplot_default <- function(DV,IV,Dataset) {
  plot <- pirateplot(formula = DV ~ IV,
             data = Dataset,
             xlab = "Solution")
  return(plot)
}
  • I have tried "as.name" (saw that here) but it did not work.
  • using data[DV] is no option because the pirateplot function requires a different notation
  • I know that there are similar questions here,here,here, and this probably qualifies as duplicate for more skilled programmers, but I did not manage to apply the solutions at other questions to my problem, so hoping for help.
Kastany
  • 427
  • 1
  • 5
  • 16

1 Answers1

3

Here is an example

pirateplot_default <- function(DV,IV,Dataset) {
  tmp=as.formula(paste0(DV,"~",paste0(IV,collapse="+")))
  plot <- pirateplot(formula = tmp,
                     data = Dataset,
                     xlab = "Solution")
  return(plot)
}

pirateplot_default("mpg",c("disp","cyl","hp"),mtcars)
user2974951
  • 9,535
  • 1
  • 17
  • 24
  • Thanks so much, didn't occur to me that I could manipulate the formula as a whole. Works perfectly. And thank you for adding the flexibility in case there are multiple independent variables (IV). – Kastany Aug 30 '19 at 10:00