如何在R中分离两个图? [英] How to separate two plots in R?

查看:230
本文介绍了如何在R中分离两个图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行此代码时,第一个图就会简单地覆盖前一个图. 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')

推荐答案

如果只想同时打开两个不同的绘图窗口,请使用dev.new,例如

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)

如果要在同一窗口中绘制两个图,则如Shane所述,设置mfrow参数.

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)

如果您想尝试一些更高级的方法,则可以看一下点阵图形或ggplot,这两种图形都非常适合创建条件图(在不同帧中出现不同数据子集的图表).

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

一个晶格示例:

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示例. (您需要先从CRAN下载ggplot.)

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

这篇关于如何在R中分离两个图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆