# Positioning charts with fig and fin

February 9, 2009
By

(This article was first published on ProgrammingR, and kindly contributed to R-bloggers)

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:

 # create data sim.data <- cbind(replicate(5,runif(8,min=0, max=100))) 

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)) 

 #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)) 

 #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) 

 #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.

The post Positioning charts with fig and fin appeared first on ProgrammingR.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,