Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

How to separate two plots in R?

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ~ .)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...