R Plots Side By Side

broken image


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 x ncols plots that are filled in by row. mfcol=c(nrows, ncols) fills in the matrix by columns.

# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main='Scatterplot of wt vs. mpg')
plot(wt,disp, main='Scatterplot of wt vs disp')
hist(wt, main='Histogram of wt')
boxplot(wt, main='Boxplot of wt')

Any ggplots side-by-side (or n plots on a grid) The function grid.arrange in the gridExtra package will combine multiple plots; this is how you put two side by side. Require (gridExtra) plot1 qplot (1) plot2 qplot (1) grid.arrange (plot1, plot2, ncol=2). BoxPlot: Boxplot is a plot which is used to get a sense of data spread of one variable.The plot displays a box and that is where the name is derived from. The top line of box represents third quartile, bottom line represents first quartile and middle line represents median.

click to view

# 3 figures arranged in 3 rows and 1 column
attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)

click to view

  • Any ggplots side-by-side (or n plots on a grid) The function grid.arrange in the gridExtra package will combine multiple plots; this is how you put two side by side. Require (gridExtra) plot1.
  • How to plot side-by-side Plots with ggplot2 in R. How to plot side-by-side Plots with ggplot2 in R. Like we are creating two plots of the dataset 'iris'.

The layout( ) function has the form layout(mat) where
mat is a matrix object specifying the location of the N figures to plot.

# One figure in row 1 and two figures in row 2
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

click to view

Optionally, you can include widths= and heights= options in the layout( ) function to control 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 1 and two figures in row 2
# row 1 is 1/3 the height of row 2
# column 2 is 1/4 the width of the column 1
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
widths=c(3,1), heights=c(1,2))
hist(wt)
hist(mpg)
hist(disp)

click to view

See help(layout) for more details.

R ggplot multiple plots

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='Car Weight',
ylab='Miles Per Gallon')
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext('Enhanced Scatterplot', side=3, outer=TRUE, line=-3)

click to view

To understand this graph, think of the full graph area as going from (0,0) in the lower left corner to (1,1) in the upper right corner. The format of the fig= parameter is a numerical vector of the form c(x1, x2, y1, y2). The first fig= sets up the scatterplot going from 0 to 0.8 on the x axis and 0 to 0.8 on the y axis. The top boxplot goes from 0 to 0.8 on the x axis and 0.55 to 1 on the y axis. I chose 0.55 rather than 0.8 so that the top figure will be pulled closer to the scatter plot. The right hand boxplot goes from 0.65 to 1 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 have to experiment to get it just right.

fig= starts a new plot, so to add to an existing plot use new=TRUE.

You can use this to combine several plots in any arrangement into one graph.

To Practice

Try the free first chapter of this interactive data visualization course, which covers combining plots.

Dot plot in R also known as dot chart is an alternative to bar charts, where the bars are replaced by dots. A simple Dot plot in R can be created using dotchart function.

Plots

Syntax of dotchart() function in R for Dot plot:

R Forest Plot Side By Side

dotchart (NumericVector, cex = 1, col = 'black', labels = NULL, main = NULL, pch = 1, sub = NULL, xlab = NULL)

Side By Side Boxplots R

NumericVectorNumeric vector to be plotted
cexplot scaling factor(size) . More the value of cex, more the plot size will be
colcolour of the dot
labelsA vector containing the label names for each plotted value.
mainTitle of the dot chart
pchnumeric value which decides the type of plot … if pch=1 then dot, pch=2 then triangle, pch=3 then ‘+'
subsubtitle of the dot chart
xlabx axis label

Example of dot plot in R

We will use the PlantGrowth data set to depict an example of R dot plot

the above dotchart() function takes up numeric vector as first argument and plots the red dots with labels and title. So the output will be

R Plots Side By Side

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='Car Weight',
ylab='Miles Per Gallon')
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext('Enhanced Scatterplot', side=3, outer=TRUE, line=-3)

click to view

To understand this graph, think of the full graph area as going from (0,0) in the lower left corner to (1,1) in the upper right corner. The format of the fig= parameter is a numerical vector of the form c(x1, x2, y1, y2). The first fig= sets up the scatterplot going from 0 to 0.8 on the x axis and 0 to 0.8 on the y axis. The top boxplot goes from 0 to 0.8 on the x axis and 0.55 to 1 on the y axis. I chose 0.55 rather than 0.8 so that the top figure will be pulled closer to the scatter plot. The right hand boxplot goes from 0.65 to 1 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 have to experiment to get it just right.

fig= starts a new plot, so to add to an existing plot use new=TRUE.

You can use this to combine several plots in any arrangement into one graph.

To Practice

Try the free first chapter of this interactive data visualization course, which covers combining plots.

Dot plot in R also known as dot chart is an alternative to bar charts, where the bars are replaced by dots. A simple Dot plot in R can be created using dotchart function.

Syntax of dotchart() function in R for Dot plot:

R Forest Plot Side By Side

dotchart (NumericVector, cex = 1, col = 'black', labels = NULL, main = NULL, pch = 1, sub = NULL, xlab = NULL)

Side By Side Boxplots R

NumericVectorNumeric vector to be plotted
cexplot scaling factor(size) . More the value of cex, more the plot size will be
colcolour of the dot
labelsA vector containing the label names for each plotted value.
mainTitle of the dot chart
pchnumeric value which decides the type of plot … if pch=1 then dot, pch=2 then triangle, pch=3 then ‘+'
subsubtitle of the dot chart
xlabx axis label

Example of dot plot in R

We will use the PlantGrowth data set to depict an example of R dot plot

the above dotchart() function takes up numeric vector as first argument and plots the red dots with labels and title. So the output will be

Dot plot in R for groups:

Suppose if we want to create the different dot plots for different group of the same data set, how to do that? For Example PlantGrowth data set have 3 groups ctrl, trt1 and trt2. Lets demonstrate how to differentiate among these 3 groups while plotting a dot chart

First subset the groups and assign different colour for different groups. Then with the help of dotchart() function use the argument, groups= PlantGrowth$group and color= pg$color as shown above

So the output will be





broken image