Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate two plots in R?

Tags:

Whenever I run this code , the first plot would simply overwrite the previous one. Isnt there a way in R to separate to get two plots ?

plot(pc) title(main='abc',xlab='xx',ylab='yy')  plot(pcs) title(main='sdf',xlab='sdf',ylab='xcv') 
like image 738
phpdash Avatar asked Nov 26 '09 01:11

phpdash


People also ask

How do you split a plot in R?

The par() function allows to set parameters to the plot. The mfrow() parameter allows to split the screen in several panels. Subsequent charts will be drawn in panels. You have to provide a vector of length 2 to mfrow() : number of rows and number of columns.

What does PAR () do in R?

The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.

How do I plot on top of another plot in R?

To overlay a line plot in the R language, we use the lines() function. The lines() function is a generic function that overlays a line plot by taking coordinates from a data frame and joining the corresponding points with line segments.

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.


2 Answers

If you just want to see two different plotting windows open at the same time, use dev.new, e.g.

plot(1:10) dev.new() plot(10:1) 

If you want to draw two plots in the same window then, as Shane mentioned, set the mfrow parameter.

par(mfrow = c(2,1)) plot(1:10) plot(10:1) 

If you want to try something a little more advanced, then you can take a look at lattice graphics or ggplot, both of which are excellent for creating conditioned plots (plots where different subsets of data appear in different frames).

A lattice example:

library(lattice) dfr <- data.frame(   x   = rep(1:10, 2),    y   = c(1:10, 10:1),    grp = rep(letters[1:2], each = 10) ) xyplot(y ~ x | grp, data = dfr) 

A ggplot example. (You'll need to download ggplot from CRAN first.)

library(ggplot2) qplot(x, y, data = dfr, facets = grp ~ .) #or equivalently ggplot(dfr, aes(x, y)) + geom_point() + facet_grid(grp ~ .) 
like image 180
Richie Cotton Avatar answered Oct 02 '22 10:10

Richie Cotton


Try using par before you plot.

 par(mfrow = c(2, 1)) 
like image 35
Shane Avatar answered Oct 02 '22 10:10

Shane