R Multiple Plots

In this article, you will learn to use par() function to put multiple graphs in a single plot by passing graphical parameters mfrow and mfcol.

Sometimes we need to put two or more graphs in a single plot.


R par() function

We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed.

The par() function helps us in setting or inquiring about these parameters. For example, you can look at all the parameters and their value by calling the function without any argument.

>par()
$xlog
[1] FALSE
...
$yaxt
[1] "s"
$ylbias
[1] 0.2

You will see a long list of parameters and to know what each does you can check the help section ?par. Here we will focus on those which help us in creating subplots.

Graphical parameter mfrow can be used to specify the number of subplot we need.

It takes in a vectorof form c(m, n) which divides the given plot into m*n array of subplots. For example, if we need to plot two graphs side by side, we would have m=1 and n=2. Following example illustrates this.

>max.temp    # a vector used for plotting
Sun Mon Tue Wen Thu Fri Sat
22 27 26 24 23 26 28
par(mfrow=c(1,2)) # set the plotting area into a 1*2 array
barplot(max.temp, main="Barplot")
pie(max.temp, main="Piechart", radius=1)

This same phenomenon can be achieved with the graphical parameter mfcol.

The only difference between the two is that, mfrow fills in the subplot region row wise while mfcolfills it column wise.

Temperature <- airquality$Temp
Ozone <- airquality$Ozone
par(mfrow=c(2,2))
hist(Temperature)
boxplot(Temperature, horizontal=TRUE)
hist(Ozone)
boxplot(Ozone, horizontal=TRUE)

Same plot with the change par(mfcol = c(2, 2)) would look as follows. Note that only the ordering of the subplot is different.


More Precise Control

The graphical parameter fig lets us control the location of a figure precisely in a plot.

We need to provide the coordinates in a normalized form as c(x1, x2, y1, y2). For example, the whole plot area would be c(0, 1, 0, 1) with (x1, y1) = (0, 0) being the lower-left corner and (x2, y2) = (1, 1) being the upper-right corner.

Note: we have used parameters cex to decrease the size of labels and mai to define margins.

# make labels and margins smaller
par(cex=0.7, mai=c(0.1,0.1,0.2,0.1))
Temperature <- airquality$Temp
# define area for the histogram
par(fig=c(0.1,0.7,0.3,0.9))
hist(Temperature)
# define area for the boxplot
par(fig=c(0.8,1,0,1), new=TRUE)
boxplot(Temperature)
# define area for the stripchart
par(fig=c(0.1,0.67,0.1,0.25), new=TRUE)
stripchart(Temperature, method="jitter")

The numbers assigned to fig were arrived at with a hit-and-trial method to achieve the best looking plot.

R Multiple Plots的更多相关文章

  1. R2—《R in Nutshell》 读书笔记(连载)

    R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...

  2. R笔记(1):formula和Formula

    #####开一个新的系列.关于R的一些笔记,就是遇到过的一些问题的简单整理.可能很基本,也可能没什么大的用处,作为一个记录而已.------------------------------------ ...

  3. R实战 第七篇:网格(grid)

    grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...

  4. R:ggplot2数据可视化——进阶(2)

    Part 2: Customizing the Look and Feel, 更高级的自定义化,比如说操作图例.注记.多图布局等  # Setup options(scipen=999) librar ...

  5. 使用Python一步一步地来进行数据分析总结

    原文链接:Step by step approach to perform data analysis using Python译文链接:使用Python一步一步地来进行数据分析--By Michae ...

  6. matlab安装和入门

    下载iso镜像: ISO镜像下载地址链接: http://pan.baidu.com/s/1i31bu5J 密码: obo1 单独破解文件下载链接: http://pan.baidu.com/s/1c ...

  7. Notes for GGplot2: Getting started with ggplot2

    Alpha-ma 2016/10/7 1 Introduction of GGplot2 ggplot2 is an R package for producing statistical, or d ...

  8. 学习笔记之Bokeh

    Welcome to Bokeh — Bokeh 0.12.16 documentation https://bokeh.pydata.org/en/latest/ Bokeh is an inter ...

  9. python数据图形化—— matplotlib 基础应用

    matplotlib是python中常用的数据图形化工具,用法跟matlab有点相似.调用简单,功能强大.在Windows下可以通过命令行 pip install matplotlib 来进行安装. ...

随机推荐

  1. 割点 —— Tarjan 算法

    由于对于这一块掌握的十分不好,所以在昨天做题的过程中一直困扰着我,好不容易搞懂了,写个小总结吧 qwq~ 割点 概念 在无向连通图中,如果将其中一个点以及所有连接该点的边去掉,图就不再连通,那么这个点 ...

  2. xftp实现本地与服务器的文件上传下载(windows)

    背景: Jemter环境搭建,需上传下载服务器文件到aws服务器上,由于secureCRT的局限性它只支持pub格式的密钥,不支持pem格式密钥,xshell是支持pem格式的,所以尝试安装xshel ...

  3. 多个List 或 Array 进行 合并

    1.调用 var aaa = new List<string>() { "0" }; var a1 = new List<string>() { " ...

  4. C# 使用大漠插件, 源码在Github和码云 ..希望对大家有所帮助

    c# 使用大漠插件. 完成 类似 按键精灵的 功能. 方法 注释 正在慢慢的 完善中 目录 仓库 github 码云 准备 效果图 如何运行 1. 注册 大漠dll 到com (资源在 源码 DLL ...

  5. 关于Lombok的认识及其应用(一)

    目录 1.Lombok的介绍 2.Lombok的安装 3.Lombok实现原理分析 4.Lombok使用方法 4.1.@Data注解 4.2.@Getter/@Setter注解 1.Lombok的介绍 ...

  6. Jquery的显示与隐藏

    $(selector).hide(speed,callback); $(selector).show(speed,callback); 可选的 speed 参数规定隐藏/显示的速度,可以取以下值:&q ...

  7. 【Python 代码】生成hdf5文件

    import random from PIL import Image import numpy as np import os import h5py from PIL import Image L ...

  8. NoSql数据库Redis系列(3)——Redis数据持久化(RDB)

    大家都知道 Redis 是一个内存数据库,所谓内存数据库,就是将数据库中的内容保存在内存中,这与传统的MySQL,Oracle等关系型数据库直接将内容保存到硬盘中相比,内存数据库的读写效率比传统数据库 ...

  9. Mysql sql_mode设置 timestamp default 0000-00-00 00:00:00 创建表失败处理

    往数据库里创建新表的时候报错: [Err] 1067 - Invalid default value for 'updateTime' DROP TABLE IF EXISTS `passwd_res ...

  10. 【转】反编译获取任何微信小程序源码(完)

    一.前言最近在学习微信小程序开发,半个月学习下来,很想实战一下踩踩坑,于是就仿写了一个阿里妈妈淘宝客小程序的前端实现,过程一言难尽,差不多两周时间过去了,发现小程序的坑远比想象的要多的多!!在实际练手 ...