How Do I Draw Two Graphs In One Plot In R
Combining Plots
R makes it easy to combine multiple plots into one overall graph, using either the
par( ) or layout( ) function.
With the par( ) function, you can include the option mfrow=c( nrows, ncols ) to create a matrix of nrows 10 ncols plots that are filled in by row. mfcol=c( nrows, ncols ) fills in the matrix past columns.
# 4 figures arranged in 2 rows and two columns
attach(mtcars)
par(mfrow=c(two,2))
plot(wt,mpg, principal="Scatterplot of wt vs. mpg")
plot(wt,disp, chief="Scatterplot of wt vs disp")
hist(wt, principal="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
click to view
# 3 figures bundled in 3 rows and 1 column
attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
click to view
The layout( ) function has the grade layout( mat ) where
mat is a matrix object specifying the location of the N figures to plot.
# One figure in row i and two figures in row 2
attach(mtcars)
layout(matrix(c(1,1,ii,3), 2, 2, byrow = Truthful))
hist(wt)
hist(mpg)
hist(disp)
click to view
Optionally, you can include widths= and heights= options in the layout( ) function to command the size of each figure more precisely. These options have the form
widths= a vector of values for the widths of columns
heights= a vector of values for the heights of rows.
Relative widths are specified with numeric values. Absolute widths (in centimetres) are specified with the lcm() function.
# One figure in row one and 2 figures in row two
# row 1 is one/3 the height of row 2
# column 2 is 1/four the width of the column i
adhere(mtcars)
layout(matrix(c(1,ane,2,three), ii, two, byrow = TRUE),
widths=c(3,1), heights=c(1,two))
hist(wt)
hist(mpg)
hist(disp)
click to view
See help(layout) for more details.
Creating a figure arrangement with fine control
In the following example, two box plots are added to scatterplot to create an enhanced graph.
# Add boxplots to a scatterplot
par(fig=c(0,0.8,0,0.8), new=TRUE)
plot(mtcars$wt, mtcars$mpg, xlab="Machine Weight",
ylab="Miles Per Gallon")
par(fig=c(0,0.8,0.55,1), new=True)
boxplot(mtcars$wt, horizontal=Truthful, axes=Faux)
par(fig=c(0.65,ane,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=Fake)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
click to view
To understand this graph, think of the total graph area as going from (0,0) in the lower left corner to (1,1) in the upper correct corner. The format of the fig= parameter is a numerical vector of the course c(x1, x2, y1, y2). The first fig= sets upward the scatterplot going from 0 to 0.8 on the x axis and 0 to 0.8 on the y centrality. The superlative boxplot goes from 0 to 0.viii on the x axis and 0.55 to 1 on the y axis. I chose 0.55 rather than 0.viii then that the meridian figure will exist pulled closer to the besprinkle plot. The right manus boxplot goes from 0.65 to ane on the x axis and 0 to 0.8 on the y axis. Again, I chose a value to pull the right hand boxplot closer to the scatterplot. You accept to experiment to get it just right.
fig= starts a new plot, so to add to an existing plot use new=TRUE.
You tin can apply this to combine several plots in any arrangement into i graph.
To Practice
Endeavor the gratuitous starting time chapter of this interactive data visualization grade, which covers combining plots.
How Do I Draw Two Graphs In One Plot In R,
Source: https://www.statmethods.net/advgraphs/layout.html
Posted by: scottmusur1939.blogspot.com

0 Response to "How Do I Draw Two Graphs In One Plot In R"
Post a Comment