R Plots Par

admin
R Plots Par Average ratng: 6,1/10 3449 votes
  1. R Plot Parabola
  2. Par Mfrow C 1 1
  3. R Plot Parameters
  4. R Plot Par(new=true)

R offers several ways to spatially orient multiple graphs in a single graphing space. The layout() function and mfrow/mfcol parameter settings are adequate solutions for many tasks and allow the graphing space to be broken up into tabular or matrix-based arrangements. For more fine grained manipulation, the fig and fin parameter settings are available. This article illustrates the capabilities and use of fig and fin.

First we’ll create some simulation data to work with:

The par (mfrow) function is handy for creating a simple multi-paneled plot, while layout should be used for customized panel plots of varying sizes. Browse other questions tagged r plot or ask your own question. The Overflow Blog The Loop: Our Community & Public Platform strategy & roadmap for Q1 2021. Customize the titles using par function. Note that, the R par function can be used to change the color, font style and size for the graph titles. The modifications done by the par function are called ‘permanent modification’ because they are applied to all the plots generated under the current R session. Read more on par by clicking. Math, programming, ecology, and more. In a previous post, I showed how to keep text and symbols at the same size across figures that have different numbers of panels.The figures in that post were ugly because they used the default panel spacing associated with the mfrow argument of the par function.


# create data
sim.data

Plots

The code above results in a matrix object with eight rows and three columns.

The fig and fin parameters affect the same graphing elements via different units. The fig parameter takes normalized device coordinates (NDC) and fin takes dimensions in inches of the device region. Because the fig units are generally more user friendly, I will use it in the examples below; however, selecting equivalent dimensions using the fin would have an identical effect. Similar to other functions that use NDC to define graphing space, fig takes a four item vector wherein positions one and three define, in percentages of the device region, the starting points of the x and y axes, respectively, while positions two and four define the end points. The default fig setting is (0, 1, 0, 1) and uses the entire device space. The default fig setting is (0, 1, 0, 1) and uses the entire device space. The graph below illustrates the default settings of fig.


# graph cases by first column using default fig
# settings of 0 1 0 1 (the full device width and height)
par(mar=c(2, 2, 1, 1), new = FALSE, cex.axis = .6, mgp = c(0, 0, 0))


#open plot
plot(c(0,100), c(-1,1), type = 'n', ylab = ', yaxt = 'n', xlab = ')
points(sim.data[,1], replicate(8, 0), pch = 19, col = 1:8, cex = 1.5)
# add center reference line
abline(0,0)
legend('bottomright', fill = c(1:8), legend = c(1:8), ncol = 4)

To make the horizontal dimensions of the graph smaller or to move the graph left or right, adjust the starting and ending x coordinates, given by the first and second positions of the fig value vector. To make the vertical dimensions of the graph smaller or to move the graph up or down, adjust the staring and ending y coordinates given in the third and fourth positions as below.


# decrease horizontal span
par(fig=c(0, 1, .2, .8))

ParR Plots Par


#open plot
plot(c(0,100), c(-1,1), type = 'n', ylab = ', yaxt = 'n', xlab = ')
points(sim.data[,1], replicate(8, 0), pch = 19, col = 1:8, cex = 1.5)
# add center reference line
abline(0,0)
legend('bottomright', fill = c(1:8), legend = c(1:8), ncol = 4)

It is possible to resize and move a single graph to any spatial orientation on the graphing device using the approach above. Additionally, you can also use this method to add multiple graphs of various sizes to a single device:


# place graph one in the bottom left
par(fig=c(0, .25, 0, .25), mar=c(2,.5,1,.5), mgp=c(0, 1, 0))

R Plot Parabola


#open plot
plot(c(0,100), c(-1,1), type = 'n', ylab = ', yaxt = 'n', xlab = ')
points(sim.data[,1], replicate(8, 0), pch = 19, col = 1:8)
# add center reference line
abline(0,0)


# place graph two in the top right
# set graphing parameters for next plot and set new parameter to TRUE
par(fig=c(.75, 1, .75, 1), new = TRUE)


#open plot
plot(c(0,100), c(-1,1), type = 'n', ylab = ', yaxt = 'n', xlab = ')
points(sim.data[,2], replicate(8, 0), pch = 19, col = 1:8)
# add center reference line
abline(0,0)


# place main graph in the center
# set graphing parameters for next plot and set new parameter to TRUE
par(fig=c(.25, .75, .25, .75), new = TRUE)

R Plots Par


#open plot
plot(c(0,100), c(-1,1), type = 'n', ylab = ', yaxt = 'n', xlab = ')
points(sim.data[,3], replicate(8, 0), pch = 19, col = 1:8, cex = 1.5)
# add center reference line
abline(0,0)
legend('bottomright', fill = c(1:8), legend = c(1:8), ncol = 4)

For simplicity I have mostly avoided labels and titles in these graphs; however they can be added and manipulated as they would be without the use of fig or fin.

This article was one of several this blog has done on graphics and visualization; you may also be interested in:

Other related topics:

  • Web Scraping when data isn’t available via an API wrapper:
    • JSON based scraping (AJAX sites, API’s)
Create a multi-paneled plotting window. The par(mfrow) function is handy for creating a simple multi-paneled plot, while layout should be used for customized panel plots of varying sizes.
  • mfrow – A vector of length 2, where the first argument specifies the number of rows and the second the number of columns of plots.
layout(mat, widths=rep(1, ncol(mat)), heights=rep(1, ncol(mat)))

Par Mfrow C 1 1

  • mat – A matrix describing the panel layout, where the numbers describe the order in which to add the plots. A zero entry is interpreted as don’t plot anything here.
  • widths – The widths of the panel columns.
  • heights – The heights of the panel rows.

R Plot Parameters

The set.seed makes the code reproducible, and the png function was used to save each of the plots to a file.

R Plot Par(new=true)

Tip. There are plenty of other plotting parameters that may be of interest. A few others were introduced in the par(mar, mgp, las) post.