How to separate two graphs in R?

Whenever I run this code, the first graph simply overwrites the previous one. Is there no way in R to split to get two graphs?

plot(pc) title(main='abc',xlab='xx',ylab='yy') plot(pcs) title(main='sdf',xlab='sdf',ylab='xcv') 
+23
r plot
Nov 26 '09 at 1:20
source share
6 answers

If you just want two different build windows to open at the same time, use dev.new , for example.

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

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

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

If you want to try something more advanced, you can take a look at trellis graphics or ggplot, both of which are great for creating conditional graphs (graphs in which different subsets of data are displayed in different frames).

Grid 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) 

Ggplot example. (First you need to download ggplot from CRAN.)

 library(ggplot2) qplot(x, y, data = dfr, facets = grp ~ .) #or equivalently ggplot(dfr, aes(x, y)) + geom_point() + facet_grid(grp ~ .) 
+38
Nov 26 '09 at 12:06
source share

Try using par before you start.

  par(mfrow = c(2, 1)) 
+14
Nov 26 '09 at 3:13
source share

You can also try the build command:

Try layout(1:2)

 plot(A) plot(B) 
+4
Mar 07 '10 at 20:51
source share

try the x11() command before each chart, here is an example:

 x11() plot(1:10) x11() plot(rnorm(10)) 

This will lead to different chart windows. You can add the "par" command to any of these x11() windows and get more varied graphics, i.e. 4 graphics in one window, and a large plot in another window.

+2
Aug 16 '10 at 15:01
source share

If you want 2 graphics in separate windows or files, you can select new devices before calling each schedule command. Cm.:

? Devices

and

? Dev.cur

0
Nov 26 '09 at 8:04
source share

An alternative answer is to designate the plot as an object, then you can display it whenever you want, i.e.

 abcplot<-plot(pc) title(main='abc',xlab='xx',ylab='yy') sdfplot<-plot(pcs) title(main='sdf',xlab='sdf',ylab='xcv') abcplot # Displays the abc plot sdfplot # Displays the sdf plot abcplot # Displays the abc plot again 
0
Nov 27 '09 at 17:58
source share



All Articles