tags:

views:

660

answers:

7

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

+8  A: 

Try using par before you plot.

 par(mfrow = c(2, 1))
Shane
A: 

If you want the 2 plots in separate windows or files you can select new devices before calling each plot command. See:

?Devices

And,

?dev.cur

Michael Schneider
oh ok , thanks for all the great suggestions guys, learned something new again .
phpdash
+9  A: 

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 ~ .)
Richie Cotton
A: 

An alternative answer is to assign the plot as an object, then you can display it when 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
PaulHurleyuk
This does not work. `plot` returns `NULL`. You cannot save the details of the graph this way. If you use `grid`-based graphics (e.g. `lattice`), what you suggest is possible, e.g. `p1 <- xyplot(y~x); print(p1)`.
Richie Cotton
+1  A: 

You could also try the layout command:

Try layout(1:2)

plot(A)    
plot(B)
CG Nguyen
A: 

try command "x11()" before each plot, here's an example: x11() plot(1:10) x11() plot(rnorm(10))

This will lead to different plot windows. You can add "par" command to any of these x11() windows and get more variety of plots, i.e. 4 plots in one window while a big plot in another window.

Rakesh Parhar
A: 

try command "x11()" before each plot, here's an example: x11() plot(1:10) x11() plot(rnorm(10))

This will lead to different plot windows. You can add "par" command to any of these x11() windows and get more variety of plots, i.e. 4 plots in one window while a big plot in another window.

Rakesh Parhar