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. (Python) SOAP Web Service (HTTP POST)

    功能很强大,可惜只有试用 https://www.example-code.com/python/soap_web_service.asp

  2. VS - Paginated

    BootstrapPagination.cshtml @model PaginationModel <div class="pagination"> <ul> ...

  3. am335x using brctl iptables dhcpcd make multi wan & multi lan network(十五)

    构建多LAN口多WAN口动态网络 [目的] 在AM335X定制动态网络功能,如下所示,在系统当中有两个以太网口,有4G模块,有wifi芯片8188eu支持AP+STA功能. [实验环境] 1.  Ub ...

  4. 洛谷 P2791 幼儿园篮球题

    洛谷 P2791 幼儿园篮球题 https://www.luogu.org/problemnew/show/P2791 我喜欢唱♂跳♂rap♂篮球 要求的是:\(\sum_{i=0}^kC_m^iC_ ...

  5. 通过 frp 实现群晖的 drive 远端同步

    通过 frp 实现 drive 同步 其实其他的也类似, 只是指定 tcp 的端口不一致就可以. frp 实现的外网端口映射"肥肠"方便. 也推广下 frp 的地址 frp-git ...

  6. .netFramework 升级NetCore 问题汇总及解决方案

    升级版本: NetCore sdk 2.2.108 .AspNetCore 2.2.0.EFCore 2.2.6 所有程序引用均从NuGet上下载,并支持NetCore 问题: 问题1:No coer ...

  7. PHP无法使用curl_init()函数

    主要针对在Ubuntu16.04搭建CRMEB环境时,监测环境会出现一个curl_init问题,这时只需执行如下命令即可解决: sudo apt-get install php-curl

  8. mysql命令行修改密码

    1.以管理员身份打开cmd窗口2.进入安装mysql的bin目录.3.命令net start mysql开启服务4.mysql -u root -p 管理员登陆5.输入旧密码后进入数据库6.输入ALT ...

  9. 深度学习面试题29:GoogLeNet(Inception V3)

    目录 使用非对称卷积分解大filters 重新设计pooling层 辅助构造器 使用标签平滑 参考资料 在<深度学习面试题20:GoogLeNet(Inception V1)>和<深 ...

  10. Android分区解释

    让我们从Android手机和平板电脑的标准内存分区列表开始.分区有:/boot/system/recovery/data/cache/misc 此外,有SD卡的分区./sdcard/sd-ext 请注 ...