Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Two graphs using a single function in r

Tags:

plot

r

i am trying to implement a function to plot two time series plots using a single function but it returns only one here is code what is wrong with it

visualize <- function(x) {

    x<-data.frame(x)

    x1 <- x[x$chr == 1, ]
    x2 <- x[x$chr != 1, ]

    t1<-data.frame("range"=1:nrow(x1))
    t2<-data.frame("range"=1:nrow(x2))

    t1$testsample_first<-exp(x1$testSample1)
    t1$testsample_second<-exp(x1$testSample2)


    t2$testsample_first<-exp(x2$testSample1)
    t2$testsample_second<-exp(x2$testSample2)

    dygraph(t1);dygraph(t2)
 }


(visualize(scon))

It is plotting second one only .I tried to implement by calling a second function in first but same result.

like image 870
Prayalankar Avatar asked Mar 07 '16 17:03

Prayalankar


People also ask

How do I return two plots in R?

So, if you want to return multiple things, you can always put results into a list and return that instead. So you'll have something like return(list(plotA = plotA, plotB = plotB)) or similar. You can then use output from this list as you would any other output.

How do you plot multiple curves on the same graph in R?

To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.

How do I put two Ggplots together?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.


1 Answers

You need to return the two objects together in a list:

list(plot(t1), plot(t2))
like image 189
Reid Minto Avatar answered Oct 11 '22 11:10

Reid Minto