R Multiple Plots
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 mfcol
fills 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的更多相关文章
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
- R笔记(1):formula和Formula
#####开一个新的系列.关于R的一些笔记,就是遇到过的一些问题的简单整理.可能很基本,也可能没什么大的用处,作为一个记录而已.------------------------------------ ...
- R实战 第七篇:网格(grid)
grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...
- R:ggplot2数据可视化——进阶(2)
Part 2: Customizing the Look and Feel, 更高级的自定义化,比如说操作图例.注记.多图布局等 # Setup options(scipen=999) librar ...
- 使用Python一步一步地来进行数据分析总结
原文链接:Step by step approach to perform data analysis using Python译文链接:使用Python一步一步地来进行数据分析--By Michae ...
- matlab安装和入门
下载iso镜像: ISO镜像下载地址链接: http://pan.baidu.com/s/1i31bu5J 密码: obo1 单独破解文件下载链接: http://pan.baidu.com/s/1c ...
- 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 ...
- 学习笔记之Bokeh
Welcome to Bokeh — Bokeh 0.12.16 documentation https://bokeh.pydata.org/en/latest/ Bokeh is an inter ...
- python数据图形化—— matplotlib 基础应用
matplotlib是python中常用的数据图形化工具,用法跟matlab有点相似.调用简单,功能强大.在Windows下可以通过命令行 pip install matplotlib 来进行安装. ...
随机推荐
- 在新浪SAE上搭建微信公众号的python应用
微信公众平台的开发者文档https://www.w3cschool.cn/weixinkaifawendang/ python,flask,SAE(新浪云),搭建开发微信公众账号http://www. ...
- Windows异常的分发处理流程
根据异常来源,一般分硬件异常和软件异常,它们处理的流程大致一样,下面简单讲一下. 如果是硬件异常,CPU会根据中断类型号转而执行对应的中断处理程序.CPU会在IDT中查找对应的函数来处理,各个异常处理 ...
- Webstorm 2019最新激活码
Webstorm 2019激活码(有效期至2020年6月5日) K6IXATEF43-eyJsaWNlbnNlSWQiOiJLNklYQVRFRjQzIiwibGljZW5zZWVOYW1lIjo ...
- C二维字符数组的使用及如何获取二维数组的总行数和总列数!
#include <stdio.h> #include <stdlib.h> int main(){ char str[][30] = {"zhangsan" ...
- redis系列(五):搭建redis-cluster集群
1.为什么要用redis-cluster a.并发要求 redis官方声称可以达到10万每秒,但是如果业务需要每秒100万条呢?b.数据量太大 一台服务器的内存正常是16-256G,如果业务需要500 ...
- Go程序员面试算法宝典-读后感1
这本书是讲解Go语言程序员面试笔试真题的书籍,讲的还不错,值得一看. 计算机技术博大精深,日新月异………………大神们疯狂的更新着技术,(我就更新,不服打我呀)虽然换汤不换药,又有几个人能精通基础,再延 ...
- Oraclecloud创建时没有上传公钥的解决办法
https://blogs.oracle.com/cloud-infrastructure/recovering-opc-user-ssh-key-on-oracle-cloud-infrastruc ...
- 常用app分类
西瓜视频 今日头条(极速版) 喜马拉雅 扫描全能王 蜻蜓FM 每天影视 抖音 小读 樊登读书 微信读书 懒人听书 京东 找靓机 拼多多 淘宝 小米有品 当当 什么值得买 小米商城 淘票票 懂车帝 小红 ...
- 作业要求2018092609-2 选题 Scrum立会报告+燃尽图 05
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8678 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...
- Perl看完这个,再不敢说自己会玩贪吃蛇
某天闲逛时看见一副动图: 真的是非常贪吃,各种拐弯各种吃,感觉十分有趣. 用Perl来实现自动吃满,蓄谋已久,之前的字符贪吃蛇.深度优先算法.A*算法,都是为此篇做铺垫. 那么,怎样让蛇不吃到自己呢? ...