0

I am making a function call in R. The function returns a list and in it has a call to plot(). From this one call, I need to record the plot as an object and store the list as a separate object. I need to store the plot because I later give it to ggarrange() along with other plots. I must store both outputs in one call of the function because the function runs permutations. As a result, it will produce a slightly different output each time. Therefore, in order for the data in the list to match the plot, the call can only be made once.

The line of code below is what I am currently using that successfully stores the plot as ggplot object. It does not store the list.

my_plot <- as.ggplot(~(my_function(input1,input2, permutations=1000)))

The code below will return the list, but not save the plot.

my_list <- my_function(input1,input2, permutations=1000)

Does anyone know a way of accomplishing what I am trying to do?

biosorcery
  • 41
  • 4
  • How about `my_list <- my_function(input1,input2, permutations=1000); my_plot <- last_plot()`? If you'll put up a sample function, I'll happily test. (It would be useful to know *exactly* what the function does to plot. Since you're referencing `ggplot`, presumably is *doesn't* use `plot()`. And plots won't be printed inside a function by default, so maybe it does `print(ggplot() + ...)`? But if it does something with grobs (which you've tagged) or `arrange.grid`, etc., it might not be so simple. – Gregor Thomas Aug 13 '19 at 18:31
  • Agree with Joran though, modifying the function sounds like the easiest solution. – Gregor Thomas Aug 13 '19 at 18:33
  • The function was written by someone else and is very complicated. It will create a variety of different plots depending on the inputs. Furthermore, in addition to making the call to plot, it will make other calls to add additional lines to the plot. For instance: plot(0, 0, type="n", xlim=c(0, length(signature)+1), ylim=c(base.sig, top.bar), axes=axes, xlab=xlab, ylab=ylab, ...) abline(h=0, col="grey") pos <- which.max(abs(es1)) lines(c(pos, pos), c(0, es1[pos]), col="grey") lines(es1, lwd=2) – biosorcery Aug 13 '19 at 18:48
  • So unfortunately, I'm not sure if I could successfully modify the function using as.ggplot. – biosorcery Aug 13 '19 at 18:49

0 Answers0