- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 478字
- 2021-08-05 17:30:30
Creating multiple plot matrix layouts
In this recipe, you will learn how to present more than one graph in a single image. Pairs plots are one example, which we saw in the last recipe, but here, you will learn how to include different types of graphs in each cell of a graph matrix.
How to do it...
Let's say we want to make a 2 x 3 matrix of graphs, made of two rows and three columns of graphs. We use the par()
command as follows:
par(mfrow=c(2,3)) plot(rnorm(100),col="blue",main="Plot No.1") plot(rnorm(100),col="blue",main="Plot No.2") plot(rnorm(100),col="green",main="Plot No.3") plot(rnorm(100),col="black",main="Plot No.4") plot(rnorm(100),col="green",main="Plot No.5") plot(rnorm(100),col="orange",main="Plot No.6")

How it works...
The par()
command is by far the most important function to customize graphs in R. It is used to set and query many graphical arguments (hence the name), which control the layout and appearance of graphs.
Note that we need to issue the par()
command before the actual graph commands. When you first run the par()
command, only a blank graphics window appears. The par()
command sets the argument for any subsequent graphs made. The mfrow
argument is used to specify how many rows and columns of graphs we wish to plot. The mfrow
argument takes values in the form of a vector of length 2
: c(nrow,ncol)
. The first number specifies the number of rows and the second specifies the number of columns. In our preceding example, we wanted a matrix of two rows and three columns, so we set mfrow
to c(2,3)
.
Note that there is another argument, mfcol
, similar to mfrow
, that can also be used to create multiple plot layouts. The mfcol
argument also takes a two-value vector that specifies the number of rows and columns in the matrix. The difference is that mfcol
draws subsequent figures by columns, rather than by rows as mfrow
does. So, if we used mfcol
instead of mfrow
in the earlier example, we will get the following plot:

There's more...
Let's look at a practical example where a multiple plot layout would be useful. Let's read the dailymarket.csv
example file, which contains data on the daily revenue, profits, and number of customer visits for a shop:
market<-read.csv("dailymarket.csv",header=TRUE)
Now, let's plot all the three variables over time in a plot matrix with the graphs stacked over one another:
par(mfrow=c(3,1)) plot(market$revenue~as.Date(market$date,"%d/%m/%y"), type="l", #Specify type of plot as l for line main="Revenue", xlab="Date", ylab="US Dollars", col="blue") plot(market$profits~as.Date(market$date,"%d/%m/%y"), type="l", #Specify type of plot as l for line main="Profits", xlab="Date", ylab="US Dollars", col="red") plot(market$customers~as.Date(market$date,"%d/%m/%y"), type="l", #Specify type of plot as l for line main="Customer visits", xlab="Date", ylab="Number of people", col="black")

The preceding graph is a good way to visualize variables with different value ranges over the same time period. It helps in identifying where the trends match each other and where they differ.
See also
We will explore more examples and uses of multiple plot layouts in later chapters of the book.
- 計(jì)算機(jī)組裝·維護(hù)與故障排除
- 計(jì)算機(jī)應(yīng)用與維護(hù)基礎(chǔ)教程
- 硬件產(chǎn)品經(jīng)理成長(zhǎng)手記(全彩)
- 平衡掌控者:游戲數(shù)值經(jīng)濟(jì)設(shè)計(jì)
- 電腦維護(hù)365問
- SiFive 經(jīng)典RISC-V FE310微控制器原理與實(shí)踐
- 筆記本電腦維修實(shí)踐教程
- IP網(wǎng)絡(luò)視頻傳輸:技術(shù)、標(biāo)準(zhǔn)和應(yīng)用
- Instant Website Touch Integration
- ActionScript Graphing Cookbook
- 3D打印:Geomagic Design X5.1 逆向建模設(shè)計(jì)實(shí)用教程
- Arduino+3D打印創(chuàng)新電子制作2
- 基于STM32的嵌入式系統(tǒng)應(yīng)用
- Hands-On Explainable AI(XAI) with Python
- Proxmox VE部署與管理指南